我有桌子
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
我需要像这样的结果
State District Active InActive
-----------------------------------------------
Gujarat Banaskantha 2 1
M.P. Bhopal 0 1
在这里,Active
和字段是基于或Inactive
的字段的总和Status
0
1
这意味着这里State
对于古吉拉特邦,曾经three
发生0
过,但是two
对于StationCode - 12345
. 这意味着它将被视为One
.
我有如下查询
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
但我无法将重复StationCode
行计为单行。
我怎样才能做到这一点 ?