我有一个非常复杂的查询,我需要扩展,但不知道如何去做。
目前,我使用以下查询输出一些值。
SELECT CONVERT(CHAR(8), pay.PaidUntil, 10) as LastDay,
COUNT(*) As PaymentsDue,
SUM(sales.Amount) As TotalDue
FROM UserPaidUntil pay INNER JOIN Sales sales
ON pay.Sales_ID = sales.Sales_ID
WHERE pay.PaidUntil > getDate()
AND pay.PaidUntil < DateAdd(day, 10, getDate())
AND pay.Billing_ID = 2
GROUP BY CONVERT(CHAR(8), pay.PaidUntil, 10)
ORDER BY CONVERT(CHAR(8), pay.PaidUntil, 10)
目前,此脚本获取2个日期之间的值,相隔10天,然后按日期分组。
现在我需要做的是更改sql,以便每天实际上是从第A天上午10:01到第B天上午10:00
UserPaidUntil.PaidUntil是具有日期
的字段我不知道从哪里开始所以我想我会问是否有人可以帮助我。
干杯
答案 0 :(得分:1)
我没有这个声明的测试人员..它将是这样的:
SELECT CONVERT(CHAR(8), DateAdd(hour, 10, pay.PaidUntil), 10) as LastDay,
COUNT(*) As PaymentsDue,
SUM(sales.Amount) As TotalDue
FROM UserPaidUntil pay INNER JOIN Sales sales
ON pay.Sales_ID = sales.Sales_ID
WHERE DateAdd(hour, 10, pay.PaidUntil) > getDate()
AND DateAdd(hour, 10, pay.PaidUntil) < DateAdd(day, 10, getDate())
AND pay.Billing_ID = 2
GROUP BY CONVERT(CHAR(8), DateAdd(hour, 10, pay.PaidUntil), 10)
ORDER BY CONVERT(CHAR(8), DateAdd(hour, 10, pay.PaidUntil), 10)