I need to write a SQL query which can fetch data and can b exported in XL sheet
I need to write a SQL query which can fetch data and can b exported in XL sheet.
Scenario
There are two tables A and B. B is a child of A and there is one to many relation (Means B can have many children of A). Now B has a column C which can have only two v开发者_高级运维alues 1 or 2.
Requirement
I need to show in report - For each record of table A, count of "number of child records having value 1 in column C of table B", Count of "number of Child records having value 2 in column C of table B" and count "total number of child records in column C".
Something like this perhaps:
select A.PK,
sum(case when B.C = 1 then 1 else 0 end) as TotalC1,
sum(case when B.C = 2 then 1 else 0 end) as TotalC2,
count(B.C) as TotalChildren
from A
inner join B
on A.PK = B.FK_to_A
group by A.PK
select
A.PK,
sum(case when B.C = 1 then 1 else 0 end) as CountOf1,
sum(case when B.C = 2 then 1 else 0 end) as CountOf2,
count(B.C) as TotalCount
from
A
inner join
B on A.FK = B.PK
group by
A.PK
精彩评论