Solved

# SQL Pivot Table

Posted on 2014-03-03
499 Views
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
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
• 3

LVL 92

Expert Comment

ID: 39901701
So, count of all StatusAs for today, count of all from yesterday, etc?
0

LVL 92

Accepted Solution

Patrick Matthews earned 500 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]
``````

Note: edited to make it more index-friendly
0

LVL 6

Author Comment

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 92

Expert Comment

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

## Featured Post

Question has a verified solution.

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

### Suggested Solutions

shrink table after huge delete 2 33
How to keep a record with the highest value 3 56
If in a where clause in t-sql 7 49
Database Owner 3 41
Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Starting up a Project
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.
###### Suggested Courses
Course of the Month8 days, 13 hours left to enroll