You must Sign In to post a response.
  • Category: SQL Server

    Compare rows in the same table and group the data

    Hi,

    I have a table in below format,

    ID Name Date Code Price
    1 A 1/1/2015 A1 10
    2 A 1/2/2015 A1 20
    3 B 1/3/2015 B1 10
    4 A 1/4/2015 A1 5
    5 A 1/5/2015 A1 5
    6 A 1/6/2015 A1 5
    7 B 1/7/2015 B1 40
    8 B 1/8/2015 B2 100
    9 A 1/9/2015 A2 500

    Excepted output
    Name start Date End Date Code Price
    A 1/1/2015 1/2/2015 A1 30
    B 1/3/2015 1/3/2015 B1 10
    A 1/4/2015 1/6/2015 A1 15
    B 1/7/2015 1/8/2015 B2 140
    A 1/9/2015 1/9/2015 A2 500
  • #762975
    Hi,

    You may use PIVOT Table in SQL Server, that might be helpful to you to fix this. Refer below link to know how to implement the PIVOT table "sqlhints.com/2014/03/10/pivot-and-unpivot-in-sql-server/"

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #763216
    Hi,

    select ID,Name,Date,Code,Price from Table1 T1

    inner join

    inner join Table1 T2

    on T1.ColumnName1=T2.ColumnName1 where ColumnName1 Not in(select ColumnName1 from Table1

    group by ID,Name,Date,Code,Price having count(*)>1)

    Hope this will help

    Sriram.R


Sign In to post your comments