我有这个问题:
SELECT c.Show_Code, req.Statut_ID, COUNT(req.Statut_ID) 'Count'
FROM [Case] c
JOIN Request req ON req.Case_Number = c.Number
GROUP BY c.Show_Code, req.Statut_ID
结果是:
Show_Code Statut_ID Count
564900 2 1
568127 2 1
我有这个法定表(Ref_RequestStatut)
ID Name
1 Test
2 Test2
我怎样才能得到这个结果:
Show_Code Statut_ID Count
564900 1 0
564900 2 1
568127 1 0
568127 2 1
我想要所有的法规,即使那些没有价值的法律?
感谢
答案 0 :(得分:0)
如果您使用的是SQL Server 2005或更高版本:
WITH counted AS (
SELECT c.Show_Code, req.Statut_ID, COUNT(req.Statut_ID) 'Count'
FROM [Case] c
JOIN Request req ON req.Case_Number = c.Number
GROUP BY c.Show_Code, req.Statut_ID
),
showcodes AS (
SELECT DISTINCT Show_Code
FROM counted
)
SELECT
s.Show_Code,
r.ID AS Statut_ID,
Count = ISNULL(c.Count, 0)
FROM showcodes s
CROSS JOIN Ref_RequestStatut r
LEFT JOIN counted c ON s.Show_Code = c.Show_Code AND r.ID = c.Statut_ID
ORDER BY
s.Show_Code,
r.ID