2012-09-13 30 views
7

我有表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 

這裏,ActiveInactive領域是基於01

Status場的總和這意味着在古吉拉特邦State,那裏three0發生,但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行作爲單個查詢。

我該怎麼做?

+0

過濾器在子查詢中的行,檢查我的回答如下。 –

+0

[Count and having query](http:// stackoverflow。com/questions/12345670/count-and-having-query) – hims056

回答

1

我想這可能會解決你的問題。 我不是100%確定的語法,但如果你可以試試看,並讓我知道如果它不起作用,我可以嘗試爲你調整它。我們的想法是使用Group By創建派生表,以消除您不想要的重複項,然後外部查詢與原始查詢相同 - 只需在派生表上。

SELECT Distinct 
    X.State, 
    X.District, 
    SUM(Case when X.Status=0 then 1 else 0 end) AS Active, 
    SUM(Case when X.Status=1 then 1 else 0 end) AS InActive 
FROM 
    (Select State, District, StationCode, Status 
    From Station_Master 
    Group By State,District, StationCode, Status) as X 
GROUP BY X.State, X.District 
4

您可以唯一地過濾子查詢中的行。嘗試,

SELECT DISTINCT state, district, 
     SUM(CASE WHEN status = 0 THEN 1 ELSE 0 END) Active, 
     SUM(CASE WHEN status = 1 THEN 1 ELSE 0 END) InActive 
FROM (
     SELECT DISTINCT state, district, StationCode, status 
     FROM Station_Master 
    ) a 
GROUP BY state, district 

SQLFiddle Demo

0

首先檢索(州,區,狀態)獨特的元組,然後在它上面運行聚集查詢,例如:

SELECT 
    a.state, 
    a.District, 
    SUM(
     Case 
     when a.Status=0 then 1 
     else 0 end 
     ) AS Active, 
    SUM(
     Case 
      when a.Status=1 then 1 
      else 0 
     end 
     ) AS InActive 
from 
    (
     SELECT DISTINCT 
      state, 
      district, 
      status 
     FROM 
      Station_Master 
    ) as a 
group by a.state, a.District 
+0

它將返回「活動」列與古吉拉特邦和塔盧卡Banaskantha區,但我需要2,因爲古吉拉特邦有3個活動計數,但有兩行所以它將被視爲一個,所以它將返回爲2。 –

2

您應該使用DISTINCT子句或子查詢GROUP BY條款從之前獲取的數據:

使用DISTINCT條款:

SELECT DISTINCT state, district, 
     SUM(CASE WHEN status = 0 THEN 1 ELSE 0 END) Active, 
     SUM(CASE WHEN status = 1 THEN 1 ELSE 0 END) InActive 
FROM (
     SELECT DISTINCT state, district, StationCode, status 
     FROM Station_Master 
    ) A 
GROUP BY state, district; 

使用GROUP BY條款:

SELECT 
    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 
(
    SELECT state,District,StationCode,Status 
    FROM Station_Master 
    GROUP BY state, District, Stationcode,Status 
) A 
GROUP BY state, District; 

See this SQLFiddle

我還在表中添加了幾條記錄來檢查this SQLFiddle.中的查詢並且工作正常。

1

請使用以下SQL:

declare @temptable table 
(
Id int, 
state nvarchar(250), 
District nvarchar(250), 
StationCode nvarchar(250), 
Status bit 
) 
Insert into @temptable values (1,'Gujarat','Banaskantha','12345',0) 
Insert into @temptable values (2,'Gujarat','Banaskantha','12345',0) 
Insert into @temptable values (3,'M.P.','Bhopal','22315',1) 
Insert into @temptable values (4,'Gujarat','Banaskantha','12349',0) 
Insert into @temptable values (5,'Gujarat','Banaskantha','12345',1) 

select tbl.state,tbl.District,SUM(cast(tbl.Status as int)) as Active ,(Count(*) - 
    SUM(cast(tbl.Status as int))) as InActive 
from (select distinct state,District,StationCode,Status from @temptable)as tbl 
group by tbl.state,tbl.District