Solved

SQL pivot query

Posted on 2014-04-02
2
252 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

759 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

18 Experts available now in Live!

Get 1:1 Help Now