Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

SQL Pivot Table

Posted on 2014-03-03
4
Medium Priority
?
504 Views
Last Modified: 2014-03-10
I have data that has 2 columns, a status and a date.

is it possible to build the following report in SQL and if so how would I do it

                  Today         Yesterday           Week             Month
StatusA
StatusB

So I would want a total sent today from statusA and B then yesterdays everything this week and finally everything this month (last 7 and 30 days)
0
Comment
Question by:CaptainGiblets
[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
  • 3
4 Comments
 
LVL 93

Expert Comment

by:Patrick Matthews
ID: 39901701
So, count of all StatusAs for today, count of all from yesterday, etc?
0
 
LVL 93

Accepted Solution

by:
Patrick Matthews earned 2000 total points
ID: 39901752
Assuming my assumption above is correct...

DECLARE @StDate datetime = 
    (SELECT MIN(x.Dt)
    FROM
        (SELECT CONVERT(date, DATEADD(day, 1 - DATEPART(WEEKDAY, GETDATE()), GETDATE())) AS Dt
        UNION
        SELECT DATEADD(month, DATEDIFF(month, 0, GETDATE()), 0) AS Dt) x)

SELECT s.[Status],
    SUM(CASE 
        WHEN DATEDIFF(day, s.[Date], GETDATE()) = 0 THEN 1
        ELSE 0 END) AS [Today],
    SUM(CASE 
        WHEN DATEDIFF(day, s.[Date], GETDATE()) = 1 THEN 1
        ELSE 0 END) AS [Yesterday],
    SUM(CASE 
        WHEN DATEDIFF(week, s.[Date], GETDATE()) = 0 THEN 1
        ELSE 0 END) AS [ThisWeek],
    SUM(CASE 
        WHEN DATEDIFF(month, s.[Date], GETDATE()) = 0 THEN 1
        ELSE 0 END) AS [ThisMonth]
FROM [SomeTable] s
WHERE s.[Date] >= @StDate
GROUP BY s.[Status]

Open in new window


Note: edited to make it more index-friendly
0
 
LVL 6

Author Comment

by:CaptainGiblets
ID: 39903197
That works a treat, just a quick question before I close it off, if I wanted to use a different date field for 2 different statuses is this possible? From what I know I would have to create 2 different views with the different date field but just thought I would throw it out there.

For example 2 status  "Sent Out" and "Returned"  with 2 dates "date Sent Out" and "Date Returned"
0
 
LVL 93

Expert Comment

by:Patrick Matthews
ID: 39915102
Sounds to me that that approach could be extended to additional date columns...
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

609 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