Solved

SQL pivot query

Posted on 2014-04-02
2
263 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 49

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

10 Questions to Ask when Buying Backup Software

Choosing the right backup solution for your organization can be a daunting task. To make the selection process easier, ask solution providers these 10 key questions.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

617 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