Solved

SQLserver 9 Cross tab query

Posted on 2014-12-10
1
51 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 24

Accepted Solution

by:
chaau earned 500 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

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

760 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now