I want to get a total sum of the results of my "total tested" "failed" and "passed" colums. I would usually handle these aggregates in SSRS, but I don't have that option at this time.
My current query:
select s.school_code, s.name, count(cd.Test_ID) as [Tested],
sum(case when cd.result = 'fail' then 1 Else 0 End) as 'Failed'
,sum(case when cd.result = 'pass' then 1 Else 0 End) as 'Passed'
FROM
[psi_db_8amSnapshot].[dbo].[Candidate_Data] cd
join [psi_db_8amSnapshot].[dbo].account_school s on s.school_id = cd.school_id
where s.School_code in
(
'1001',
'1002',
'1003' ,
'1004' ,
'1005' ,
'1006' ,
'1007' ,
'1008' ,
'1016' ,
'1009' ,
'1010' ,
'1012' ,
'1013' ,
'1014' ,
'1015'
)
and cd.[date] between '01-01-2016' and '05-01-2017' and cd.TestName = 'MN Dental Assistant State Licensure Examination'
group by s.school_code, s.name, test_id
I am looking to get a total off all the values in my three aggregate columns. So a sum of Tested, which should = 640, sum of passed = 327, sum of failed = 313.
I think you are looking for group by grouping sets as below:
Just replace your group by as below:
group by grouping sets ((s.school_code), (s.name), (test_id))
I would probably use with statements:
with NumberOfFailedResults as
(
Select count(cd.result) as NumberOfFailedResults from [psi_db_8amSnapshot].[dbo].[Candidate_Data] as cd where cd.Result = 'Failed'
),
NumberOfPassedResults as
(
Select count(cd.result) as NumberOfPassedResults from [psi_db_8amSnapshot].[dbo].[Candidate_Data] as cd where cd.Result = 'Passed'
)
Select NumberOfFailedResults, NumberOfPassedResults, ...
This should work for you.
select Q.school_code, Q.name, SUM(Q.Tested), SUM(Q.Failed), SUM(Q.Passed) from (
select s.school_code, s.name, count(cd.Test_ID) as [Tested],
sum(case when cd.result = 'fail' then 1 Else 0 End) as 'Failed'
,sum(case when cd.result = 'pass' then 1 Else 0 End) as 'Passed'
FROM
[psi_db_8amSnapshot].[dbo].[Candidate_Data] cd
join [psi_db_8amSnapshot].[dbo].account_school s on s.school_id = cd.school_id
where s.School_code in
(
'1001',
'1002',
'1003' ,
'1004' ,
'1005' ,
'1006' ,
'1007' ,
'1008' ,
'1016' ,
'1009' ,
'1010' ,
'1012' ,
'1013' ,
'1014' ,
'1015'
)
and cd.[date] between '01-01-2016' and '05-01-2017' and cd.TestName = 'MN Dental Assistant State Licensure Examination'
group by s.school_code, s.name, test_id)Q
GROUP BY Q.School_Code, Q.Name
Related
I have tried CTE combining two table in my below query and getting the Invalid Object error but it exists .Can Anyone guide me?
;WITH t1
AS (
SELECT DepId
, COUNT(EmpId) AS TotalHeadCount
FROM Emploee
WHERE (datepart(yyyy, DOJ) BETWEEN 2005 AND 2017)
AND STATUS = 0
GROUP BY DepId
)
, t2
AS (
SELECT DepId
, COUNT(EmpId) AS NewJoinees
FROM Emploee
WHERE (DATEPART(yyyy, DOJ) = 2017)
AND (DATEPART(mm, DOJ) = 01)
AND datepart(mm, DOJ) >= 12
AND STATUS = 0
GROUP BY DepId
)
, t3
AS (
SELECT Tobehired AS TOBEHIRED
, OpenPosition AS OPENPOSITION
, STATUS
FROM Employee1
)
SELECT t1.DepId
, CASE
WHEN TotalHeadCount IS NULL
THEN '0'
ELSE TotalHeadCount
END AS TotalHeadCount
, CASE
WHEN NewJoinees IS NULL
THEN '0'
ELSE NewJoinees
END AS NewJoinees
, Tobehired
, OpenPosition
, STATUS
FROM t1
FULL JOIN t2
ON t1.DepId = t2.DepId
FULL JOIN t3
ON t1.DepId = t3.DepId
There's a time and a place for use of Common table expressions. I don't think this was one of them... Complex joins with nested subqueries, recursion, refactoring for maintainability. A two table join doesn't seem to fit the bill in my opinion.
SELECT E.DEPID
, sum(case when datepart(yyyy,E.doj) between 2005 and 2017 and status =0 then 1 else 0 end as TotalHeadCount
, sum(case when E.doj >= cast('2017-01-12' as datetime) then 1 else 0 end as NewJoinees
, E1.Tobehired
, E1.OpenPosition
, E1.STATUS
FROM Employee E
LEFT JOIN Emplyee1 E1
on E.DepID = E1.DepID
GROUP BY E.DEPID,
, E1.Tobehired
, E1.OpenPosition
, E1.STATUS
I am trying to execute the following query and getting a error.
but when i tried to use commented lines its work. but not working with contains function.
select
2015-min(c.Year)+1 as experiance
, sum (case when not c.JudgementID_FK is null then 1 else 0 end ) as reported_Judgements
, sum( case
when
(
Contains( c.Result , 'ACCEPT')
or
Contains( c.Result , 'ALLOW' )
or
contains (c.Result , 'Grant')
-- (
-- c.Result = 'Accepted' or c.Result = 'Allowed'
-- or c.Result='allowed' or c.Result='accepted'
-- or c.Result ='bail allowed(accepted)'
-- or c.Result = 'admitted'
-- or c.Result = 'Accepted.'
-- or c.Result = 'accepted.'
-- )
-- and
--cl.LawyerOf = 'Petitioner'
)
then 1 else 0 end) as win_cases
, sum (case when c.JudgementID_FK is null then 1 else 1 end ) as total_Cases
from CaseTLS c, CaseLawyer cl , Lawyer l
Where
c.CaseId = cl.CaseId
and cl.ComputerCode = l.ComputerCode
group by l.computercode
order by l.computercode
According to this documentation "CONTAINS is a predicate used in the WHERE clause" and you are trying to use it in the SELECT clause.
Try using PATINDEX instead:
select
2015-min(c.Year)+1 as experiance
, sum (case when not c.JudgementID_FK is null then 1 else 0 end ) as reported_Judgements
, sum( case
when
(
PATINDEX( '%ACCEPT%', UPPER(c.Result)) > 0
or
PATINDEX( '%ALLOW%', UPPER(c.Result)) > 0
or
PATINDEX( '%GRANT%', UPPER(c.Result)) > 0
)
then 1 else 0 end) as win_cases
, sum (case when c.JudgementID_FK is null then 1 else 1 end ) as total_Cases
from CaseTLS c, CaseLawyer cl , Lawyer l
Where
c.CaseId = cl.CaseId
and cl.ComputerCode = l.ComputerCode
group by l.computercode
order by l.computercode
I have the below issues that I would like to address:
Is there a way to simplify the Total column?
The bottom row reads as null, I would like that to be "Total" as well.
Is it better to ROLLUP the way I have it, ROLLUP((status))? Or this is exactly same as ROLLUP(status)?
Below is my query:
SELECT
status AS "ROW LABELS",
COUNT(case when source = 'INTERNET' THEN 1 end) AS "INTERNET",
COUNT(case when source = 'SALES' THEN 1 end) AS "SALES",
COUNT(case when source = 'REP' THEN 1 end) AS "REP",
COUNT(case when source = 'COM' THEN 1 end) AS "COM",
(COUNT(case when source = 'INTERNET' THEN 1 end) +
COUNT(case when source = 'SALES' THEN 1 end) +
COUNT(case when source = 'REP' THEN 1 end) +
COUNT(case when source = 'COM' THEN 1 end)
) AS Total
FROM
SOMETABLE
GROUP BY ROLLUP((status))
order by 1;
Below is my data:
If by "simplify" you mean "make the calculation more succinct", then yes. The following will work:
COUNT(CASE WHEN source IN ('INTERNET', 'SALES', 'REP', 'COM') THEN 1 END)
Simply use nvl to convert the NULL to a value: NVL(status, 'TOTAL') AS row_labels
ROLLUP( (status) ) is the same as ROLLUP( status )
i want to pivot table but i get some error about this image
this is my code
SELECT ARCBG_Abbrev
,ARCIM_Code
,SUM(CASE WHEN TAR_Code = 'O' THEN ITP_Price
ELSE 0 END) AS O
,SUM(CASE WHEN TAR_Code = 'I' THEN ITP_Price ELSE 0 END)
AS I
,SUM(CASE WHEN TAR_Code = 'F' THEN ITP_Price ELSE 0 END)
AS F
FROM SYSTEM.VS_OrderItem
WHERE (ARCIM_Code = '010004')
GROUP BY ARCBG_Abbrev, ARCIM_Code
i try to distinct column to check value in column is same in every row where ARCIM_Code = 010004 .it's the same value in column ARCBG_Abbrev. Can you check my pivot code plascc. thank you
on the assumption it's an ODBC driver limitation, perhaps nesting the case expressions as a subquery will help.
SELECT
ARCBG_Abbrev
, ARCIM_Code
, SUM(O) AS O
, SUM(I) AS I
, SUM(F) AS F
FROM (
SELECT
ARCBG_Abbrev
, ARCIM_Code
, CASE
WHEN TAR_Code = 'O' THEN
ITP_Price
ELSE
0
END AS O
, CASE
WHEN TAR_Code = 'I' THEN
ITP_Price
ELSE
0
END AS I
, CASE
WHEN TAR_Code = 'F' THEN
ITP_Price
ELSE
0
END AS F
FROM SYSTEM.VS_OrderItem
) AS SQ
WHERE ARCIM_Code = '010004'
GROUP BY
ARCBG_Abbrev
, ARCIM_Code
This question already has answers here:
Reason for Column is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause [duplicate]
(4 answers)
Closed 8 years ago.
I keep receiving this error
Column 'vStockSerialsTemp.Id' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause. ALTER VIEW vStockSerials as
SELECT Id
, StockOid
, WarehouseOid
, serial
, StockCode
, ActionPrice
, StockName
, StockTitle
, Warehouse
, SlipDate
, ActionType
, SlipType
FROM vStockSerialsTemp
UNION ALL
SELECT Id
, StockOid
, WarehouseOid
, serial
, StockCode
, sum(CASE WHEN ActionType = 'Income' THEN ActionPrice ELSE -ActionPrice END) as ActionPrice
, StockName
, StockTitle
, Warehouse
, SlipDate
, SlipType
, 'Balance' as ActionType
FROM vStockSerialsTemp
GROUP BY
serial
HAVING sum(CASE WHEN ActionType = 'Expense' THEN ActionPrice ELSE -ActionPrice END) <> 0
--ORDER BY
-- ActionType DESC
GO
In your second query of the UNION ALL you aggregate ActionPrice, thus you need to GROUP BY on your other fields (except ActionType as this is not a field from a table):
SELECT Id,StockOid,WarehouseOid,serial,StockCode,ActionPrice,StockName,
StockTitle,Warehouse,SlipDate,ActionType,SlipType
FROM
vStockSerialsTemp
UNION ALL
SELECT
Id,StockOid,WarehouseOid,serial,StockCode,
sum(CASE WHEN ActionType = 'Income'
THEN ActionPrice ELSE -ActionPrice END) as ActionPrice,
StockName,StockTitle,Warehouse,SlipDate,SlipType,
'Balance' as ActionType
FROM
vStockSerialsTemp
GROUP BY
Id,StockOid,WarehouseOid,serial,StockCode,StockName,StockTitle,Warehouse,
SlipDate,SlipType
HAVING
sum(CASE WHEN ActionType = 'Expense'
THEN ActionPrice ELSE -ActionPrice END) <> 0
you can aggregate only by "serial" using partition:
SELECT Id,StockOid,WarehouseOid,serial,StockCode,ActionPrice,StockName,StockTitle,Warehouse,SlipDate,ActionType,SlipType
FROM
vStockSerialsTemp
UNION ALL
SELECT * from
(SELECT
Id,StockOid,WarehouseOid,serial,StockCode,
sum(CASE WHEN ActionType = 'Income' THEN ActionPrice ELSE -ActionPrice END) OVER(PARTITION BY serial) as ActionPrice,
StockName,StockTitle,Warehouse,SlipDate,SlipType,
'Balance' as ActionType) x WHERE ActionPrice<>0
--ORDER BY
-- ActionType DESC
GO