I have table
ID State District StationCode Status
---------------------------------------------------------------
1 Gujarat Banaskantha 12345 0
2 Gujarat Banaskantha 12345 0
3 M.P. Bhopal 22315 1
4 Gujarat Banaskantha 12349 0
5 Gujarat Banaskantha 12345 1
I need result like
State District Active InActive
-----------------------------------------------
Gujarat Banaskantha 2 1
M.P. Bhopal 0 1
Here , Active and Inactive fields are sum of Status fields based on 0 or 1
That means here State for Gujarat, There three times 0 occured , but two duplicate rows for StationCode - 12345. It means it will be considered as One.
I have query like below
select distinct
state,
District,
SUM(
Case
when Status=0 then 1
else 0 end
) AS Active,
SUM(
Case
when Status=1 then 1
else 0
end
) AS InActive
from
Station_Master
group by state, District
But I am unable to count duplicate StationCode row as Single.
How can I do that ?
I think this would probably address your problem.
I am not 100% sure of the syntax but if you can try it out and let me know if it doesnt work, i can try tweak it for you. The idea is to create a derived table using Group By to eliminate the duplicates that you dont want first and then the outer query is same as your original query – just on the derived table instead.