亲爱的所有人,请帮助我,因为我是SQL Server的新手。我有一个选择查询,当前产生以下结果:
DoctorName 球队 访问日期使用以下查询: SELECT d.DoctorName,t.TeamName,ca.VisitDate FROM cActivity AS ca. INNER JOIN医生AS d ON ca.DoctorId = d.Id INNER JOIN团队AS t ON ca.TeamId = t.Id 在'1/1/2010'和'1/31/2010'之间的访问日期
我想制作以下内容:
DoctorName Team 1 2 3 4 5 6 7 ... 31访问了答案 0 :(得分:0)
使用:
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 '1/1/2010' AND '1/31/2010'
GROUP BY d.doctorname, t.teamname