SQL将重复行计为单个

时间:2012-09-13 04:59:38

标签: 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字段是基于Status0

1字段的总和

对于古吉拉特邦来说,这意味着Statethree次出现0two,但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

但我无法将重复的{{1}}行计为单个。

我该怎么做?

5 个答案:

答案 0 :(得分: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

答案 1 :(得分: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.中的查询并且工作正常。

答案 2 :(得分: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

答案 3 :(得分: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

答案 4 :(得分:0)

首先检索(State,District,Status)的不同元组,然后在它上面运行聚合查询,如下所示:

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