sql - Why doesn't my query return correct results? -
i have used these temp table return total no of solved cases , total number of pending cases same table grouped district e.g.
district totalsolvedcases totalpendingcases 3 1 b 8 6 c 7 1
i have done doesn't return correct result
select * #table1 ( select count(cases.pk_cases_caseid) totalcases, districts.districtname cases inner join concernedoffices on concernedoffices.pk_concernedoffices_id = cases.fk_concernedoffices_cases_concernedofficeid inner join districts on districts.pk_districts_districtid = concernedoffices.fk_districts_concernedoffices_districtid inner join casehearings on casehearings.fk_cases_casehearings_caseid = cases.pk_cases_caseid casehearings.isclosingdate = 1 group districts.districtname ) d select * #table2 ( select count(cases.pk_cases_caseid) totalpedningcases, districts.districtname cases inner join concernedoffices on concernedoffices.pk_concernedoffices_id = cases.fk_concernedoffices_cases_concernedofficeid inner join districts on districts.pk_districts_districtid = concernedoffices.fk_districts_concernedoffices_districtid inner join casehearings on casehearings.fk_cases_casehearings_caseid = cases.pk_cases_caseid casehearings.isclosingdate = 0 group districts.districtname ) d select #table1.totalcases totalsolvedcases, #table2.totalpedningcases, #table1.districtname #table1 inner join #table2 on #table2.districtname = #table1.districtname group #table1.totalcases, #table2.totalpedningcases, #table1.districtname
you need 1 select
, use case
expressions conditional counting:
select count(case when casehearings.isclosingdate = 1 1 end) totalcases, count(case when casehearings.isclosingdate = 0 1 end) totalpedningcases, districts.districtname cases inner join concernedoffices on concernedoffices.pk_concernedoffices_id = cases.fk_concernedoffices_cases_concernedofficeid inner join districts on districts.pk_districts_districtid = concernedoffices.fk_districts_concernedoffices_districtid inner join casehearings on casehearings.fk_cases_casehearings_caseid = cases.pk_cases_caseid group districts.districtname
Comments
Post a Comment