Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SQLserver 9 Cross tab query

Posted on 2014-12-10
1
Medium Priority
?
77 Views
Last Modified: 2015-02-26
Greeting,
I have a select stmt(select NUM,Week_Day,InOUT from TBL1) which give me the data in the following format.
NUM     Week_Day     InOut
0001       MONDAY     8-16
0001       TUESDAY     8-15
0001       WEDNESDAY 9-4
0002       FRIDAY    9-14
I  need to have a query which give me the following output
NUM    MONDAY   TUESDAY  WEDNESDAY  THURSDAY   FRIDAY
0001     8-16             8-15           9-4
0002                                                                                     9-14
0
Comment
Question by:mrong
1 Comment
 
LVL 25

Accepted Solution

by:
chaau earned 2000 total points
ID: 40492635
You need to use PIVOT for this:

SELECT NUM, [MONDAY], [TUESDAY], [WEDNESDAY], [THURSDAY], [FRIDAY]
FROM 
(SELECT NUM,Week_Day,InOUT from TBL1) p
PIVOT
(
MAX(InOUT)
FOR Week_Day IN
([MONDAY], [TUESDAY], [WEDNESDAY], [THURSDAY], [FRIDAY])
) AS pvt
ORDER BY NUM

Open in new window

0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
Loops Section Overview
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question