希望在同一结果中显示 7 天数据和 30 天数据的日期范围

发布于 2024-12-09 09:54:30 字数 259 浏览 0 评论 0原文

我有一个连接两个表的查询,用于查找当前有效的成员 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) 

I have a query 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) 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

救赎№ 2024-12-16 09:54:30

那么,像这样的事情吗?

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文