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
Related
can somebody help me solving and explaining how i can get the following done? :
In my MySQL query i want to select all entries, where the forwarded_to_cordroom value is 0, and in the next row i want to have all where the value is 1, basically i could create 2 identical queries, where the only difference would be the WHERE clause (where forwarded_to_cordroom = 1 , where forwarded_to_cordroom = 0) , and i thought about doing this in one query, but getting the following error with what ive tried:
SELECT
COUNT(DISTINCT o.order_id) as count,
(SELECT o.forwarded_to_cordroom WHERE o.forwarded_to_cordroom = 1)
FROM
`orders_articles` o
LEFT JOIN orders oo ON
o.order_id = oo.order_id
WHERE
(
oo.finished_order_date IS NULL OR oo.finished_order_date >= '2021-09-27'
) AND oo.order_date <= '2021-09-27'
Results in :
#1140 - In aggregated query without GROUP BY, expression #2 of SELECT list contains nonaggregated column 'o.forwarded_to_cordroom'; this is
incompatible with sql_mode=only_full_group_by
I have also tried changing the subselect in various ways (with and without joins etc.) but without success, always the same error.
I'd prefer not turning this mode off, I think that would not be the purpose and that I can fix my query with some help.
Best Regards
Use conditional aggregation:
SELECT COUNT(DISTINCT o.order_id) AS count,
COUNT(CASE WHEN o.forwarded_to_cordroom = 1 THEN 1 END) AS count_1,
COUNT(CASE WHEN o.forwarded_to_cordroom = 0 THEN 1 END) AS count_0
FROM orders_articles AS o
LEFT JOIN orders AS oo ON o.order_id = oo.order_id
WHERE ...
I have a query like so:
SELECT COUNT(t.ID),
CASE WHEN t.Area = "Downtownn"
THEN "Downtown"
ELSE t.Area END as "Area"
FROM MyTable t
GROUP BY Area
Now this kinda works, if I change the name of the AS and reference that in the group by everything works as expected, however because Area is an existing column the Group by gets me the wrong results, is there away override this column?
You can put the query in a subquery, then do the grouping in the main query.
SELECT Count, Area
FROM (
SELECT COUNT(t.ID) AS Count,
CASE WHEN t.Area = 'Downtownn'
THEN 'Downtown'
ELSE t.Area END as "Area"
FROM MyTable t
) AS x
GROUP BY Area
Or you can do the grouping in the subquery by giving it a different alias, then rename it in the main query.
SELECT c AS Count, a AS Area
FROM (
SELECT COUNT(t.ID) AS c,
CASE WHEN t.Area = 'Downtownn'
THEN 'Downtown'
ELSE t.Area END as a
FROM MyTable t
GROUP BY a
) AS x
Note that using aliases in GROUP BY is a MySQL extension in the first place, so the first solution above is the way you'd do it in standard SQL (or repeat the CASE expression in GROUP BY).
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
This bellow query will result the post wise sum of like
SELECT
tblPost.Post,
SUM(tblPost.LikeCount),
CASE WHEN tblPost.Time
BETWEEN (SELECT
CONVERT(VARCHAR(10),
DATEADD(DD,DATEDIFF(DD 0,GETDATE()),-60),120)) AND CONVERT(date,GETUTCDATE())
THEN 'Last 60 Days'
ELSE 'More Than 1 Year'
END AS"date type"
FROM tblPost
INNER JOIN tblProfile ON (tblProfile.ID=tblPost.UID)
INNER JOIN tblWatchList ON (tblWatchList.ID=tblProfile.UID)
WHERE dbo.tblPost.Time BETWEEN (SELECT
CONVERT(VARCHAR(10),
DATEADD(DD,DATEDIFF(DD,0,GETDATE()), -60),120))AND CONVERT(date,GETUTCDATE())
GROUP BY tblPost.Post,tblPost.Time
This is my query and it is working fine but I want to rewrite this. How can I describe it here... in my query I am having two GROUP BY clauses (tblPost.Post,tblPost.Time) and exactly here I'm getting a problem. I want to rewrite this query such as a way that I can group my result only by tblPost.Post
Please help me.
Your WHERE clause already eliminates the two options you've presented for tblPost.Time - you explicitly state you're only ever going to retrieve "Last 60 days" so why are you bothering to have a whole CASE statement in the query?
And you're joining tables that aren't even represented. So start by cleaning your query up, use some aliases, and drop what you don't need:
SELECT P.Post, SUM(P.LikeCount)
FROM dbo.tblPost P
WHERE P.Time BETWEEN (SELECT CONVERT(VARCHAR(10), DATEADD(DD,DATEDIFF(DD,0,GETDATE()), -60),120)) AND CONVERT(date, GETUTCDATE())
GROUP BY P.Post
So I have a list of items organized by date in two different categories, when switching the categories I sometimes run into an error that does not let the item go into the correct placement. The thing that is messing up the query is what I want to place in a case/if statement becuase it is only needed if there is an item with the same date, anyother time it throws off the whole query. Here is what I have, granted i know that that case does not work where it is or how it is, please work with me.
SELECT CASE WHEN COUNT(*)=0 THEN (SELECT MAX(Rotate)+1 FROM Table1 WHERE Vol=1)
ELSE MIN(o.Rotate) END as nRotate FROM Table1 o INNER JOIN Table2 s ON o.SID=s.ID
WHERE s.Date >='7/30/2004' And s.ID<>100 And o.Vol=1 and
Case s.DATE
When '7/30/2004' then s.Sales>'Doe, Jane'
End
You don't need case:
WHERE s.Date >='2004-07-30' And s.ID <> 100 And o.Vol = 1 and
(s.date <> '2004-07-30' or s.Sales > 'Doe, Jane')