亲爱的,我有一个选择查询,目前产生以下结果:
DoctorName Team 1 2 3 4 5 6 7 ... 31 拜访了医生。作为 A x x ... 2 次
博士。Sc A x ... 1 次
博士。Gh B x ... 1 次
博士。Nd C ... x 1 次
使用以下查询:
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' AND '2/29/2012' 所以结果将是:
DoctorName Team 1 2 3 4 5 6 7 ... 29 拜访了医生。作为 A x x ... 2 次
博士。Sc A x ... 1 次
博士。Gh B x ... 1 次
博士。Nd C ... x 1 次
有人可以帮助我如何获取两个日期之间的天数并帮助我修改查询,以便它可以循环 MAX(CASE WHEN ca.visitdate = 1 THEN 'x' ELSE NULL END) AS 1 和天数一样多?请请