如何在sql中对具有相同值的行进行分组?

时间:2014-05-28 06:24:16

标签: sql sql-server sql-server-2008 group-by row

如何在sql中对具有相同值的行进行分组?

data1 123  12/03/2009
      124  15/09/2009
data2 333  02/09/2010
      323  02/11/2010
      673  02/09/2014
      444  05/01/2010

3 个答案:

答案 0 :(得分:4)

试试这个

DECLARE @temp TABLE(col1 varchar(20),col2 int, col3 varchar(20))
insert into @temp values ('data1', 123 , '12/03/2009'),('data1', 124 , '15/09/2009'),
                        ('data2 ',333  ,'02/09/2010'),('data2 ',323 , '02/11/2010'),
                        ('data2 ',673 , '02/09/2014'),('data2',444 , '05/01/2010')

SELECT 
    (CASE rno WHEN 1 THEN col1 ELSE '' END )AS col1,
    col2,
    col3
FROM
(                   
    SELECT 
        ROW_NUMBER() OVER(PARTITION BY Col1 ORDER BY col2) AS rno,
        col1,col2,col3
    FROM @temp
) As temp

这给出了以下输出

col1    col2    col3
---------------------------------
data1   123 12/03/2009
        124 15/09/2009
data2   323 02/11/2010
        333 02/09/2010
        444 05/01/2010
        673 02/09/2014

PARTITION BY正在使用给定列名对数据进行分组,并根据order by在该组中生成行号。

以下是SQL Fiddle

我根据提供的架构创建了另一个小提琴。fiddle2

答案 1 :(得分:0)

我假设您有多个与以下查询相同的记录。要选择不同的,您可以使用GROUP BYDISTINCT,如下所示:

使用GROUP BY:

with datatab as
(
select 'data1' dataa,  123 num,   '12/03/2009' datee from dual union all
select 'data1' dataa,  123 num,   '12/03/2009' datee from dual union all
select 'data1' dataa,  123 num,   '12/03/2009' datee from dual union all
select 'data1' dataa,  124 num,   '15/09/2009' datee from dual union all
select 'data2' dataa,  333 num,   '02/09/2009' datee from dual union all
select 'data2' dataa,  323 num,   '02/11/2010' datee from dual union all
select 'data2' dataa,  673 num,   '02/09/2014' datee from dual union all
select 'data2' dataa,  444 num,   '05/01/2010' datee from dual 

)
select dataa, num, datee from datatab 
group by dataa, num, datee
order by dataa;

使用DISTINCT:

with datatab as
(
select 'data1' dataa,  123 num,   '12/03/2009' datee from dual union all
select 'data1' dataa,  123 num,   '12/03/2009' datee from dual union all
select 'data1' dataa,  123 num,   '12/03/2009' datee from dual union all
select 'data1' dataa,  124 num,   '15/09/2009' datee from dual union all
select 'data2' dataa,  333 num,   '02/09/2009' datee from dual union all
select 'data2' dataa,  323 num,   '02/11/2010' datee from dual union all
select 'data2' dataa,  673 num,   '02/09/2014' datee from dual union all
select 'data2' dataa,  444 num,   '05/01/2010' datee from dual 

)
select distinct dataa, num, datee from datatab 
order by dataa;

对于两个查询,ORIGINAL DATA是:

dataa    | num   | datee
------------------------------
data1    | 123    | 12/03/2009
data1    | 123    | 12/03/2009
data1    | 123    | 12/03/2009
data1    | 124    | 15/09/2009
data2    | 333    | 02/09/2009
data2    | 323    | 02/11/2010
data2    | 673    | 02/09/2014
data2    | 444    | 05/01/2010

查询OUTPUT DATA是:

dataa    | num   | datee
------------------------------
data1    | 123    | 12/03/2009
data1    | 124    | 15/09/2009
data2    | 333    | 02/09/2009
data2    | 323    | 02/11/2010
data2    | 673    | 02/09/2014
data2    | 444    | 05/01/2010

修改 请参阅由Nithesh创建的小提琴:http://sqlfiddle.com/#!3/4e3e80/9

此查询也将获得不同的记录:

SELECT 
(CASE rno WHEN 1 THEN EndorsementId ELSE '' END )AS col1,
*
FROM
(                   
SELECT 
    ROW_NUMBER() OVER(PARTITION BY EndorsementId ORDER BY PolicyNumber) AS rno,
    *
FROM (select distinct * from [endorsement]  ) a
) As temp1

希望它有所帮助。!

答案 2 :(得分:-1)

您可以使用以下内容:

Select col1, col2, col3 From tblName
Group By col1

希望它可以帮到你