Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQL pivot query

Posted on 2014-04-02
2
Medium Priority
?
266 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 2000 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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…
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.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

730 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