Solved

SQL pivot query

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how the fundamental information of how to create a table.

752 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