SQL Clone
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
SSC-Enthusiastic
SSC-Enthusiastic (198 reputation)SSC-Enthusiastic (198 reputation)SSC-Enthusiastic (198 reputation)SSC-Enthusiastic (198 reputation)SSC-Enthusiastic (198 reputation)SSC-Enthusiastic (198 reputation)SSC-Enthusiastic (198 reputation)SSC-Enthusiastic (198 reputation)

Group: General Forum Members
Points: 198 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
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26990 Visits: 41297
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 Guru
SSC Guru (504K reputation)SSC Guru (504K reputation)SSC Guru (504K reputation)SSC Guru (504K reputation)SSC Guru (504K reputation)SSC Guru (504K reputation)SSC Guru (504K reputation)SSC Guru (504K reputation)

Group: General Forum Members
Points: 504220 Visits: 44229
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.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Michael T2
Michael T2
SSC-Enthusiastic
SSC-Enthusiastic (198 reputation)SSC-Enthusiastic (198 reputation)SSC-Enthusiastic (198 reputation)SSC-Enthusiastic (198 reputation)SSC-Enthusiastic (198 reputation)SSC-Enthusiastic (198 reputation)SSC-Enthusiastic (198 reputation)SSC-Enthusiastic (198 reputation)

Group: General Forum Members
Points: 198 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