亲爱的,我目前将产生以下结果SELECT查询:
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“2012/2/1” AND“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,则“X” ELSE NULL END)AS 1多达天数? 请请