查找两个日期之间的天数以制作动态列
亲爱的大家,我有一个选择查询,当前产生以下结果:
DoctorName Team 1 2 3 4 5 6 7 ... 31 Visiteddr. As A x x ... 2 times
dr. Sc A x ... 1 times
dr. Gh B x ... 1 times
dr. Nd C ... x 1 times
使用以下查询:
DECLARE @startDate = '1/1/2010', @enddate = '1/31/2010'
SELECT d.doctorname,
t.teamname,
MAX(CASE WHEN ca.visitdate = 1 THEN 'x' ELSE NULL END) AS 1,
MAX(CASE WHEN ca.visitdate = 2 THEN 'x' ELSE NULL END) AS 2,
MAX(CASE WHEN ca.visitdate = 3 THEN 'x' ELSE NULL END) AS 3,
...
MAX(CASE WHEN ca.visitdate = 31 THEN 'x' ELSE NULL END) AS 31,
COUNT(*) AS visited
FROM CACTIVITY ca
JOIN DOCTOR d ON d.id = ca.doctorid
JOIN TEAM t ON t.id = ca.teamid
WHERE ca.visitdate BETWEEN @startdate AND @enddate
GROUP BY d.doctorname, t.teamname
问题是我想让日期列是动态的,例如如果 ca.visitdate BETWEEN '2/1/2012'和“2012 年 2 月 29 日” 所以结果将是:
DoctorName Team 1 2 3 4 5 6 7 ... 29 Visiteddr. As A x x ... 2 times
dr. Sc A x ... 1 times
dr. Gh B x ... 1 times
dr. Nd C ... x 1 times
有人可以帮助我如何获取两个日期之间的天数并帮助我修改查询,以便它可以循环 MAX(CASE WHEN ca.visitdate = 1 THEN 'x' ELSE NULL END) AS 1 与天数一样多吗?拜托拜托
Dear all, I have a select query that currently produces the following results:
DoctorName Team 1 2 3 4 5 6 7 ... 31 Visited
dr. As A x x ... 2 times
dr. Sc A x ... 1 times
dr. Gh B x ... 1 times
dr. Nd C ... x 1 times
Using the following query:
DECLARE @startDate = '1/1/2010', @enddate = '1/31/2010'
SELECT d.doctorname,
t.teamname,
MAX(CASE WHEN ca.visitdate = 1 THEN 'x' ELSE NULL END) AS 1,
MAX(CASE WHEN ca.visitdate = 2 THEN 'x' ELSE NULL END) AS 2,
MAX(CASE WHEN ca.visitdate = 3 THEN 'x' ELSE NULL END) AS 3,
...
MAX(CASE WHEN ca.visitdate = 31 THEN 'x' ELSE NULL END) AS 31,
COUNT(*) AS visited
FROM CACTIVITY ca
JOIN DOCTOR d ON d.id = ca.doctorid
JOIN TEAM t ON t.id = ca.teamid
WHERE ca.visitdate BETWEEN @startdate AND @enddate
GROUP BY d.doctorname, t.teamname
the problem is I want to make the column of date are dynamic for example if ca.visitdate BETWEEN '2/1/2012' AND '2/29/2012'
so the result will be :
DoctorName Team 1 2 3 4 5 6 7 ... 29 Visited
dr. As A x x ... 2 times
dr. Sc A x ... 1 times
dr. Gh B x ... 1 times
dr. Nd C ... x 1 times
Can somebody help me how to get numbers of days between two date and help me revised the query so it can looping MAX(CASE WHEN ca.visitdate = 1 THEN 'x' ELSE NULL END) AS 1 as many as numbers of days? Please please
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
SQL 中的基本规则是任何给定的构造查询将始终返回相同的列 - 包括有多少列、它们的名称和类型。
因此,如果您仍想沿着这条路线走下去,那么您将考虑采用动态 SQL 方法来动态构建查询。否则,可能值得考虑一下是否可以在更高级别抑制空列(这是否适用于某种形式的报表处理器 - 例如 SQL 报表服务或水晶报表?)
编辑 1
您可能想要向查询添加其他列,例如:
CASE WHEN DATEPART(month,@StartDate) = DATEPART(month,DATEADD(day,29,@StartDate)) THEN 1 ELSE 0 END as ShowColumn29
(对于其他数字也类似) 。然后如何在报告服务中使用它,我@ma有点模糊,但我认为您可以在报告上的某个位置添加一个隐藏的文本框,该文本框绑定到 ShowColumn29 值,然后将报告的“29”列的可见性设置为该文本框的值。
抱歉 - 我不太擅长报告服务,但希望您可以尝试这种概念并使其发挥作用?
The basic rule in SQL is that any given constructed query will always return the same columns - in terms of how many there are, their names, and their types.
So you'd be looking at a dynamic SQL approach to construct the query on the fly, if you still want to go down that route. Otherwise, it might be worth looking at whether you can suppress empty columns at a higher level (is this going to some form of report processor - such as SQL reporting services or crystal reports?)
edit 1
You might want to add additional columns to your query such as:
CASE WHEN DATEPART(month,@StartDate) = DATEPART(month,DATEADD(day,29,@StartDate)) THEN 1 ELSE 0 END as ShowColumn29
(And similarly for the other numbers). How you then use that in Reporting services, I@m a bit vague, but I think you can add a hidden textbox somewhere on your report that binds to the ShowColumn29 value, and then set the visibility of the "29" column of the report to the value of this textbox.
Sorry - I'm not that good with reporting services, but hopefully you can play around with this sort of concept and make it work?