我有表SQL計數重複的行單
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
領域是基於0
或1
Status
場的總和這意味着在古吉拉特邦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
行作爲單個查詢。
我該怎麼做?
過濾器在子查詢中的行,檢查我的回答如下。 –
[Count and having query](http:// stackoverflow。com/questions/12345670/count-and-having-query) – hims056