Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Query help needed - Like a Pivot with generic headings? Expand / Collapse
Author
Message
Posted Saturday, April 13, 2013 1:45 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, April 15, 2013 10:51 AM
Points: 22, Visits: 84
CREATE TABLE [dbo].[mike_inventory](
[itemid] [int] NULL,
[location] [varchar](50) NULL

insert into mike_inventory values (10131,'Row A')
insert into mike_inventory values (10131,'Row B')
insert into mike_inventory values (10132,'Row B')
insert into mike_inventory values (10132,'Row C')
insert into mike_inventory values (10133,'Row A')
insert into mike_inventory values (10133,'Row D')
insert into mike_inventory values (10134,'Row A')
insert into mike_inventory values (10134,'Row E')

I want my output to look like this

Itemid                           Location1                       Location2                   Location3
--------------------------------------------------------------------------------------------
10131 ROW A ROW B
10132 ROW B ROW C
10133 ROW A ROW D
10134 ROW A ROW E




Is this possible?
Post #1442051
Posted Saturday, April 13, 2013 2:47 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 4:50 PM
Points: 1,853, Visits: 18,249
Maybe something along these lines will help



;WITH cte AS
(
SELECT itemid ,
location ,
ROW_NUMBER()OVER( PARTITION BY itemid ORDER BY itemid) RN
FROM mike_inventory
)
SELECT itemid,
MAX(CASE WHEN rn = 1 THEN LOCATION ELSE NULL END) AS LOC1,
MAX(CASE WHEN RN = 2 THEN LOCATION ELSE NULL END) AS LOC2
FROM CTE
GROUP BY itemid
ORDER BY itemid



__________________________________________________________________
you can lead a user to data....but you cannot make them think !
__________________________________________________________________
Post #1442054
Posted Saturday, April 13, 2013 10:20 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 7:28 PM
Points: 36,613, Visits: 31,064
To see how the query that JLS wrote works, please see the following article.
http://www.sqlservercentral.com/Forums/Topic935472-392-1.aspx


To learn how to do the same in a dynamic condition where the column names aren't always known at design time, please see the following article.
http://www.sqlservercentral.com/articles/Crosstab/65048/


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1442082
Posted Monday, April 15, 2013 10:52 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, April 15, 2013 10:51 AM
Points: 22, Visits: 84
Thanks JLS , thats exactly what i wanted.
Post #1442413
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse