SQL 数据仅按日历日进行每周排序。例如,2013 年 2 月 1 日是星期五,需要提取 2 月 1 日和 2 月 2 日的数据。现在它会提取 1 月 27 日整周的数据-2013年至2013年2月2日。我的结果计入了周末日期的账单总额。 EX( 1-5-13, 1-12-13) 对于一月底和二月初,我需要结果看起来像 1-27-13 到 1-31-13 =“总计”,并以一周结束日期为31号。 2 月 2-1-13 至 2-2-13 =“总计”
我已经能够在几天内做到这一点,但这不是“会计”想要的。
提前致谢。 这是我的代码:
SELECT
DATEADD(week, DATEdiff(WK, 0, bd.[Service Date 1]) ,5) AS month,
bd.Chart,
bd.[Transaction Code],
SUM(bd.Units) AS [Total Billed]
FROM dbo.[Billing Detail] AS bd
INNER JOIN dbo.Patient AS p ON bd.Chart = p.[Chart Number]
WHERE
(bd.[Transaction Code] = 'H2016')
AND (bd.[Service Date 1] >= '01/01/2013')
AND (bd.[Service Date 1] < '12/31/2013')
GROUP BY
DATEADD(week, DATEdiff(WK, 0, bd.[Service Date 1]) ,5),
bd.Chart,
bd.[Transaction Code]
ORDER BY bd.Chart
最佳答案
只需向您的选择列表(并按列表分组)添加两个字段即可:
...
MONTH([Service Date 1]),
DATEPART(week,[Service Date 1])
...
这样就可以了
关于按日历日期排序的 SQL 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14843104/