我有两张桌子
表格
ID | TYPE
1 Annual
2 Sick
3 Unpaid
4 Marriage
表格LeaveData
IDLEAVEDATA | LEAVETYPE*
1 1
2 1
3 2
4 2
LEAVETYPE是外键(参见表格中的ID)
如何计算表LeaveData中ID的出现?
输出示例:
TYPE | COUNT
Annual 2
Sick 2
Unpaid 0 or null
Marriage 0 or null
答案 0 :(得分:1)
试试这个:
SELECT L.[Type] AS [Type]
,ISNULL(COUNT(*),0) AS [Count]
FROM Leave AS L
LEFT JOIN LeaveData AS LD ON LD.LeaveType = L.ID
ORDER BY 2 DESC
答案 1 :(得分:0)
试,
SELECT l.TYPE , COUNT(ld.LEAVETYPE) as COUNT
FROM Leave AS l
LEFT JOIN LeaveData AS ld ON ld.LEAVETYPE = L.ID
GROUP BY ld.LEAVETYPE