我有一个连接两个表的查询,这些表查找当前有效的成员ID的名称。我想要做的是使用相同的查询显示数据,为一个成员在一行中产生7天和30天的价值。
这就是我的想法
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)
答案 0 :(得分:0)
那么,这样的事情呢?
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