Would like to show date range for 7 days of data and 30 days of data in the same result
I have a que开发者_开发技巧ry that joins two tables that looks up the name for a member id that currently works. What I am trying to do is display data using the same query to produce 7 days and 30 days worth in one line for one member.
Here's what I'm thinking
Member Name | Metric 1 (7 days) | Metric 2 (7 days) | Metric 3 (7 Days)| Metric 1 (30 days) | Metric 2 (30 days) | Metric 3 (30 days)
So, something like this?
SELECT MemberName,
MIN(CASE WHEN SomeDate BETWEEN DATEADD(DAY,-7,GETDATE()) AND GETDATE() THEN [Metric 1] END) [Metric 1 (7 days)],
MIN(CASE WHEN SomeDate BETWEEN DATEADD(DAY,-7,GETDATE()) AND GETDATE() THEN [Metric 2] END) [Metric 2 (7 days)],
MIN(CASE WHEN SomeDate BETWEEN DATEADD(DAY,-7,GETDATE()) AND GETDATE() THEN [Metric 3] END) [Metric 3 (7 days)],
MIN([Metric 1]) [Metric 1 (30 days)],
MIN([Metric 2]) [Metric 2 (30 days)],
MIN([Metric 3]) [Metric 3 (30 days)]
FROM YourTable
WHERE SomeDate BETWEEN DATEADD(DAY,-30,GETDATE()) AND GETDATE()
GROUP BY MemberName
精彩评论