私はテーブルを持っています
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の合計ですStatus01
つまりState、ここグジャラート語では が発生しましたが、 の行がthree重複しています。とみなされるということです。0twoStationCode - 12345One
以下のようなクエリがあります
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行をシングルとしてカウントできません。
どうやってやるの ?