RANGE GROUP BY 日期中的 COUNT 条记录

发布于 2024-11-03 20:54:20 字数 527 浏览 0 评论 0原文

我有一个销售表,显示每次销售的日期和时间。

例如:

saleid |发售日期 |销售时间
1 | 20110327 | 101
2 | 20110327 | 102
3 | 20110328 | 201

(因此销售 2 发生在 20110327 的 102)

我需要构建一个 SQL 语句:
按日期对销售额进行分组(每行是不同的日期),然后
统计每个时间范围内的销售额。 (每个时间范围都是一个单独的列)

该表应如下所示:

saledate | 101-159 | 101-159 200-259 |
20110327 | 2 | 0 |
20110328 | 0 | 1 |

它需要是单个语句,并且销售日期和销售时间需要保持数字格式。
(我正在从包含数百万行的数据库表中提取)

我正在使用 MS Access

非常感谢任何建议。

太感谢了!

I have a sales table that shows the date and time of each sale.

For example:

saleid | saledate | saletime
1 | 20110327 | 101
2 | 20110327 | 102
3 | 20110328 | 201

(So sale 2 occurred on 20110327 at 102)

I need to construct a single SQL statement that:
Groups the sales by date (each row is a different date) and then
counts the sales for each time range. (With each time range being a separate column)

The table should look something like this:

saledate | 101-159 | 200-259 |
20110327 | 2 | 0 |
20110328 | 0 | 1 |

It needs to be a single statement and saledate and saletime need to remain in numeric format.
(I am pulling from a database table with several million rows)

I am using MS Access

Any advice is greatly appreciated.

Thank you so much!

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

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

发布评论

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

评论(2

冷月断魂刀 2024-11-10 20:54:20
SELECT saledate,
SUM(IIF(saletime >= 101 and saletime <= 159), 1, 0) as [101To159)
SUM(IIF(saletime >= 200 and saletime <= 259), 1, 0) as [200To259)
FROM myTable
GROUP BY saledate

注意:我还没有运行这个查询。然而,事情也可能是这样的。

SELECT saledate,
SUM(IIF(saletime >= 101 and saletime <= 159), 1, 0) as [101To159)
SUM(IIF(saletime >= 200 and saletime <= 259), 1, 0) as [200To259)
FROM myTable
GROUP BY saledate

Note: I haven't run this query. However, this is how it could be.

动听の歌 2024-11-10 20:54:20
SELECT saledate,
SUM(case when saletime between 101 and 159 then 1 else 0 end ) as R101_159,
SUM(case when saletime between 200 and 259 then 1 else 0 end ) as R200_59
FROM myTable
GROUP BY saledate
SELECT saledate,
SUM(case when saletime between 101 and 159 then 1 else 0 end ) as R101_159,
SUM(case when saletime between 200 and 259 then 1 else 0 end ) as R200_59
FROM myTable
GROUP BY saledate
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文