Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Query help needed - Like a Pivot with generic headings?


Query help needed - Like a Pivot with generic headings?

Author
Message
Michael T2
Michael T2
Grasshopper
Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)

Group: General Forum Members
Points: 22 Visits: 90
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?
J Livingston SQL
J Livingston SQL
Hall of Fame
Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)

Group: General Forum Members
Points: 3456 Visits: 33045
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
and remember....every day is a school day

Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45307 Visits: 39934
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.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Michael T2
Michael T2
Grasshopper
Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)

Group: General Forum Members
Points: 22 Visits: 90
Thanks JLS , thats exactly what i wanted.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search