How to edit a tuple of this query - mysql

I have written a query with 3 columns which stand for Name, HoursRegistered & HoursCompleted. After doing all my query selection i get the right table I want. But, my column of HoursRegistered & HoursCompleted have some null values in them. I want to replace NULL with 0.Now, I know I have to use UPDATE. But, wherever I use UPDATE, it throws different errors and can't figure out where to use update. Any suggestion is appreciated. Thanks
SELECT CONCAT(FirstName," ",LastName) as Name, SUM(SUBQUERY.completed)
as HoursCompleted,SUM(SUBQUERY.registered) as HoursRegistered from
(select Distinct e.EmployeeID,e.FirstName, e.LastName,
case when t.Status='COMPLETED' then c.CreditHours else null end as completed,
case when t.Status='REGISTERED' then c.CreditHours else null ends as registered
from employee e left join Transcript t
on e.EmployeeID = t.EmployeeId
left join class c
on t.ClassId=c.ClassId
group by e.EmployeeID,c.CreditHours ,e.FirstName, e.LastName,t.Status)
as SUBQUERY group by Employeeid,LastName
ORDER BY LastName ASC
The error that I get when I run this query is

Related

Sub Query take too much time to get rows

I'm trying to find some records based on the tags they have. In order to find out which tags a record has I add them to the result using a subquery. To find out which results should be returned I added a having statement at the end of the query. But something tells me this is not the best way. Is there any other way to do it?
Here is the query that takes too much time to execute.
SELECT c.id,c.currentBalance,
(SELECT running_balance from vrCorporateLedger WHERE company_id=c.id
ORDER by id DESC LIMIT 1) AS ledgerBalance
FROM company AS c
WHERE c.vrCorporate='YES'
AND c.deleted_at IS NULL
HAVING currentBalance > ledgerBalance
This should do the trick:
SELECT c.id,c.currentBalance,
vrcl.running_balance ledgerBalance
FROM company AS c
INNER JOIN vrCorporateLedger vrcl
ON vrcl.id = c.company_id
WHERE c.vrCorporate='YES'
AND c.deleted_at IS NULL
AND currentBalance > vrcl.running_balance

Write a query to print the employee name and UIN in each line. If no UIN is present print NULL in its place. The order of output does not matter

There are two tables EMPLOYEES and EMPLOYEE_PAN. We have to print employee NAME from EMPLOYEES table and UIN from EMPLOYEE_PAN table. ID is in both the tables used as a primary key. If no UIN is present in EMPLOYEE_PAN table then we have to print NULL instead of UIN. UIN is an integer.
I have tried this code. I am facing an error in the case when statement part. I am facing a problem in case statements when JOIN is used in the query. Please let me know how to use CASE statement with JOIN.
SELECT UIN, NAME
FROM EMPLOYEE_PAN JOIN EMPLOYEES
ON EMPLOYEES.ID = EMPLOYEE_PAN.ID
CASE WHEN EMPLOYEE_PAN.UIN != 0 THEN EMPLOYEE_PAN.UIN END ;
You need left join
SELECT EMPLOYEE_PAN.UIN, EMPLOYEES.NAME
FROM EMPLOYEES
LEFT JOIN EMPLOYEE_PAN ON EMPLOYEES.ID = EMPLOYEE_PAN.ID
left join return null value for not matching keys
Use a case statement in SELECT clause
SELECT
CASE WHEN EMPLOYEE_PAN.UIN != 0 THEN EMPLOYEE_PAN.UIN ELSE "NULL" END AS UIN, NAME
FROM
EMPLOYEE_PAN JOIN EMPLOYEES
ON EMPLOYEES.ID = EMPLOYEE_PAN.ID;
Note: I consider no UIN present in the table means UIN value is 0 (from your query )

NOT LIKE on mysql query

I have a big problem understanding why a query works when using LIKE in query and not working when using NOT LIKE.
I will post query below:
select DISTINCT (mails), name
from disposable
JOIN (
SELECT DISTINCT (mail) as mails,
CONCAT(toys.firstname, ' ' , toys.lastname) as name
FROM toys2
join toys ON toys.userid = toys2.id
where ( (toyid = '27' or toyid = '29')
and status != 'Sold'
and toys.regdate >= '2017-01-01'
)
) as tab
WHERE tab.mails LIKE CONCAT('%#', disposable.email)
I think what you want is something more like the following. Note that I simplified the schema a bit so as to do a bit less work for the SQL Fiddle.
SELECT c.email, c.name
FROM customer c LEFT JOIN disposable d
ON SUBSTR(c.email, INSTR(c.email, '#')+1, LENGTH(c.email)-INSTR(c.email, '#')) = d.email
WHERE d.email IS NULL;
Basically, here you're getting the domain of the customer and matching it to the entry in the disposable table. The final WHERE clause uses IS NULL to determine the customer email addresses that are not disposable - use IS NOT NULL to find the ones that are.
Hope this helps.

Query Is Not Summing As I Need

I am trying to write a CTE Query and I am way before a "New" title for CTE Queries. But I feel I am fairly close to getting the end game that I am after. My query works perfect until I throw in the CTE and even after including the CTE it still works perfect just gives each individual instance as opposed to the SUM like I need. What should I alter in my syntax so that the query only produces the SUM as I need?
;With CTE
As
(
SELECT
BadgeNum
,NameOnFile
,SUM((CONVERT(decimal(18,6),pyrll.hoursworked))) AS [Hours]
FROM
masterpayroll pyrll
Group By
BadgeNum,NameOnFile
)
SELECT
,SUM(pyrll.[Hours]) As [Hours Worked This Week]
,pyrll.NameOnFile As [Employee Name]
,COUNT(case when pf.arrest_status in ('Final', 'Complete',) And pf.supervisorSignoff IS NOT NULL THEN pf.ID else null end)
,COUNT(case when pf.arrest_status in ('Pending', 'Incomplete', 'On Hold') THEN pf.ID else null end)
FROM personelFiles pf
INNER JOIN CTE pyrll
ON pf.ID = pyrll.BadgeNum
WHERE pf.officerName Like 'Gat%'
GROUP BY pyrll.[Employee Name], pyrll.[Hours Worked This Week]
EDIT ---Top Data Set is what is returned from query - bottom data set is what I want to see returned.
EDIT # 2 - If their is a better way to write the query to still produce the desired result of the 2nd data set in my image below I am up for that as well!
You have aggregate column alias name in the group by, you need only the employee name
Change your group by clause from
GROUP BY pyrll.[Employee Name], pyrll.[Hours Worked This Week]
To
GROUP BY pyrll.NameOnFile

SubQuery Join Failed

I am trying to find out the missing record in the target. I need the employee whose record are missing.
Suppose I have input source as
1,Jack,type1,add1,reg3,..,..,..,
2,Jack,type2,add1,reg3,..,,.,..,
3,Jack,type3,add2,reg4,..,.,..,.,
4,Rock,,,,,,,,
and I have output as
1,Jack,type1,add1,reg3,..,..,..,
4,Rock,,,,,,,,
I have 1000 numbers of rows for other employees and in target i don't have any duplicate records.
I need the employee who are present in source and target having different occurance
means for e.g in above sample data I have 3 entries of jack and 1 entry of Rock in source
and in target I have only on entry of Jack and one for Rock
I am running below query and required output is Jack,3
How can I get it. I am getting error in below query
select A.EMP_NUMBER,A.CNT1
from
(select EMP_NUMBER,count(EMP_NUMBER) as CNT1
from EMPLOYEE_SOURCE
group by EMP_NUMBER ) as A
INNER JOIN
(select B.EMP_NUMBER,B.CNT2
from (select EMP_NUMBER,count(EMP_NUMBER) as CNT2
from EMPLOYEE_TARGET
group by EMP_NUMBER )as B )
ON (A.EMP_NUMBER = B.EMP_NUMBER)
where A.CNT1 != B.CNT2
Please help.
Why don't get the employee that have different number of rows in the two table when grouped by their name (I suppose Emp_Number is the field that contain the name if that what the query in the question return)
SELECT s.Emp_Number, Count(s.Emp_Number)
FROM EMPLOYEE_SOURCE s
LEFT JOIN EMPLOYEE_TARGET t ON s.Emp_Number = t.Emp_Number
GROUP BY s.Emp_Number
HAVING Count(s.Emp_Number) != Count(t.Emp_Number)
It would be really helpful if you specified the exact error you get.
If this is you actual query there are two things: There's no alias name for the 2nd Derived Table (btw, you don't need it at all) and at least in Teradata !=is not valid, this is SQL and not C.
select A.EMP_NUMBER,A.CNT1
from
(
select EMP_NUMBER,count(EMP_NUMBER) as CNT1
from EMPLOYEE_SOURCE
group by EMP_NUMBER
) as A
INNER JOIN
(
select EMP_NUMBER,count(EMP_NUMBER) as CNT2
from EMPLOYEE_TARGET
group by EMP_NUMBER
) as B
ON (A.EMP_NUMBER = B.EMP_NUMBER)
where A.CNT1 <> B.CNT2
If an employee is missing in the 2nd table you might have to use an Outer Join as Serpiton suggested and add an additional WHERE-condition:
where A.CNT1 <> B.CNT2
or b.CNT2 IS NULL