Solved

SQL pivot query

Posted on 2014-04-02
2
254 Views
Last Modified: 2014-04-02
I need to convert the following into a pivot output to display AccountID by months but I'm stuck on the format of pivot having not used it before.

Can someone help please? I'm sure it's simple.

Thanks

SELECT MONTH(MD.DateAdded) AS 'MonthSent', MD.AccountID, A.Organisation, COUNT(MD.MessageDataID) AS 'Messages Sent'
FROM  TA_BUSINESS.dbo.tbl_MESSAGES_DATA AS MD WITH (NOLOCK)
LEFT JOIN TA_BUSINESS.dbo.tbl_ACCOUNTS AS A WITH (NOLOCK)
   ON MD.AccountID = A.AccountID 
WHERE  
	(MONTH(MD.DateAdded) BETWEEN 1 AND 3)
	AND YEAR(MD.DateAdded) = 2014
	AND (MD.MessageTypeID = 1 OR MD.MessageTypeID = 4 OR MD.MessageTypeID = 5)
	AND MD.IsMessageIncoming = 0
	AND A.ResellerID = 2007

GROUP BY MD.AccountID, A.Organisation, MONTH(MD.DateAdded)
ORDER BY MD.AccountID

Open in new window

0
Comment
Question by:RossDagley1
2 Comments
 
LVL 48

Accepted Solution

by:
PortletPaul earned 500 total points
ID: 39972085
Here's one way:
SELECT
      MD.AccountID
    , A.Organisation
    , count(case when MONTH(MD.DateAdded) = 1 then MD.MessageDataID end) AS 'Messages Sent Month 1'
    , count(case when MONTH(MD.DateAdded) = 2 then MD.MessageDataID end) AS 'Messages Sent Month 2'
    , count(case when MONTH(MD.DateAdded) = 3 then MD.MessageDataID end) AS 'Messages Sent Month 3'
FROM TA_BUSINESS.dbo.tbl_MESSAGES_DATA AS MD WITH (NOLOCK)
      LEFT JOIN TA_BUSINESS.dbo.tbl_ACCOUNTS AS A WITH (NOLOCK)
            ON MD.AccountID = A.AccountID
WHERE (MONTH(MD.DateAdded) BETWEEN 1 AND 3)
      AND YEAR(MD.DateAdded) = 2014
      AND (MD.MessageTypeID = 1
      OR MD.MessageTypeID = 4
      OR MD.MessageTypeID = 5)
      AND MD.IsMessageIncoming = 0
      AND A.ResellerID = 2007

GROUP BY
      MD.AccountID
    , A.Organisation
ORDER BY
      MD.AccountID

Open in new window

One doesn't have to use "pivot" to arrive at pivoted data.
0
 
LVL 2

Author Comment

by:RossDagley1
ID: 39972092
Thank you. That's exactly what I needed, and I can see how you've done it. Great example. Thanks!
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

In this article I will describe the Detach & Attach 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.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

912 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

17 Experts available now in Live!

Get 1:1 Help Now