sql中按时间间隔分组
我需要同时显示 06:00 - 18:00 和 18:00 - 06:00 之间的数据,
这是示例查询 IM 使用。我无法检索第二组数据。怎么办?
喜欢 1、 2、 3 时间范围为 06:00 - 18:00
和 4, 5、 6 营业时间为 18:00 - 06:00
SELECT COUNT(cp.comm_pend_id) AS comm_pend_id, cp.UserID, um.Username, CONVERT(varchar, cp.submitted_date, 101) AS Date, SUM(cp.Earning)
AS Earning, SUM(cp.total_commission) AS total_commission
FROM dbo.comm_pending AS cp INNER JOIN
dbo.user_master AS um ON cp.UserID = um.UserID
GROUP BY cp.UserID, CONVERT(varchar, cp.submitted_date, 101), um.Username, cp.PaidStatus, CONVERT(varchar(10), cp.submitted_date, 108)
HAVING (cp.PaidStatus = 'unpaid') AND (CONVERT(varchar(10), cp.submitted_date, 108) BETWEEN '06:00:00' AND '18:00:00')
ORDER BY cp.UserID
I need to show data between time 06:00 - 18:00 and 18:00 - 06:00 at same time
this is sample Query I M Using. I can not retrieve second group data. How to do?
like
1,
2,
3
comes in 06:00 - 18:00 range
and 4,
5,
6
comes in 18:00 - 06:00 range
SELECT COUNT(cp.comm_pend_id) AS comm_pend_id, cp.UserID, um.Username, CONVERT(varchar, cp.submitted_date, 101) AS Date, SUM(cp.Earning)
AS Earning, SUM(cp.total_commission) AS total_commission
FROM dbo.comm_pending AS cp INNER JOIN
dbo.user_master AS um ON cp.UserID = um.UserID
GROUP BY cp.UserID, CONVERT(varchar, cp.submitted_date, 101), um.Username, cp.PaidStatus, CONVERT(varchar(10), cp.submitted_date, 108)
HAVING (cp.PaidStatus = 'unpaid') AND (CONVERT(varchar(10), cp.submitted_date, 108) BETWEEN '06:00:00' AND '18:00:00')
ORDER BY cp.UserID
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您无法检索第二组,因为 18:00 大于 06:00,因此它们在同一天之间没有任何内容。对于第二组,您应该像这样过滤:
You cannot retrieve the second group because 18:00 is greater than 06:00, so there's nothing between them in the same day. For the second group you should filter like this:
CASE 语句可能会有所帮助。例如:
A CASE statement might be able to help. For example: