[Solved] How to group rows with same value in sql? [closed]


Try this

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

This gives the following output

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 is grouping the data with the given column name, and a row number is generated in that group based on the order by.

Here is the SQL Fiddle

I have created another fiddle based on the schema provided .fiddle2

12

solved How to group rows with same value in sql? [closed]