Techspirit
Techspirit

Reputation: 15

Need help with a SQL query that combines adjacent rows into a single row

I need a solution to a problem that has the table structure as listed below. Input

1 1/1/2009 Product1
2 2/2/2009 Product2
3 3/3/2009 Product3
4 4/4/2009 Product4
5 5/5/2009 Product5

Output

1 1/1/2009 2/2009 Product1
2 3/3/2009 4/4/2009 Product3
3 5/5/2009            Product5

I tried using CTE. But was not very sucessful in extracting the second row value. Appreciate any help. Thanks.

Upvotes: 0

Views: 2367

Answers (3)

Sarath Chandra
Sarath Chandra

Reputation: 141

You can actually create a dummy column initially and create a cte.

Then, use cte and join them on the dummy key and the row number or a series which simulates sequential numbers.

Then, filter in the dataset to display only odd numbered rows

create table dbo.test
(
    id integer,
    currdate varchar(20), -- just to keep simple, not to mess with dates, took this as string
    productCode varchar(20)
);


insert into test values (1, '1/1/2009', 'product1');
insert into test values (2, '2/2/2009', 'product2');
insert into test values (3, '3/3/2009', 'product3');
insert into test values (4, '4/4/2009', 'product4');
insert into test values (5, '5/5/2009', 'product5');


with ctes as
(
    select
        't' as joinKey,
        id, -- serves as rownum or else create another using row_num() over partition by and order by
        currdate,
        productCode     
    from test
)
select
    c1.id,
    c1.currdate,
    c1.productCode,
    c2.id,
    c2.currdate,
    c2.productCode
from
(
    select
        joinKey,
        id,
        currdate,
        productCode
    from
        ctes
)c1,
(
    select
        joinKey,
        id,
        currdate,
        productCode
    from
        ctes
)c2
where c1.joinKey = c2.joinKey
and c1.id + 1 = c2.id
and c1.id % 2 = 1

The result is as shown below:

id  currdate    productCode id  currdate    productCode
1   1/1/2009    product1    2   2/2/2009    product2
3   3/3/2009    product3    4   4/4/2009    product4

Upvotes: 0

John Fisher
John Fisher

Reputation: 22717

I don't know whether Russ' answer helps you at all. Here is a link to an article that explains how to add row numbers to the results of a query. (Search for "row_number" to find the most likely example.)

Once you have a query numbering the rows properly, you should be able to throw that into a CTE, then select from it twice -- once for odd numbers, then again for even numbers. Have each result return the even numbered value for joining (odd - 1 = even). At that point, you can join the results of the queries and get two products on one row.

Upvotes: 1

Russ Bradberry
Russ Bradberry

Reputation: 10865

You are looking for PIVOT: http://msdn.microsoft.com/en-us/library/ms177410.aspx
Here is a best shot with the info you gave, I do something similar in one of my apps. You may need to use a dynamic SQL query if the pivot values change.

SELECT *
    FROM (SELECT [Date]
        ,[Product] 
      FROM [Values]
   PIVOT (Max([Date])
          FOR [Product]
           IN ('put date ranges here')) pvt

here is what mine looks like, this will allow for a set of different values. This is used in a form builder to retrive the values of user input

--//Get a comma delimited list of field names from the field table for this form
DECLARE @FieldNames varchar(max)
SELECT @FieldNames = COALESCE(@FieldNames + ', ', '') + '[' + CAST([FieldName] AS varchar(max)) + ']'
  FROM [Fields]
 WHERE [FormID] = @FormID

--//create a dynamic sql pivot table that will bring our 
--//fields and values together to look like a real sql table
DECLARE @SQL varchar(max)
SET @SQL = 
 'SELECT *
    FROM (SELECT [Values].[RowID]
            ,[Fields].[FieldName]
            ,[Values].[Value] 
            FROM [Values]
      INNER JOIN [Fields] ON [Fields].[FieldID] = [Values].[FieldID]
           WHERE [Fields].[FormID] = ''' + @FormID + ''') p
   PIVOT (Max([Value])
      FOR [FieldName]
      IN (' + @FieldNames + ')) pvt'

--//execute our sql to return the data
EXEC(@SQL)

Upvotes: 1

Related Questions