SELECT Pod_Code, COUNT(*)
FROM [DBHFT 201314 Trust SLAM Model PID].dbo.D_Actual_Activity_HRG A
INNER JOIN dbo.D_HrgCodes B ON A.HRG_ID = B.Hrg_ID
INNER JOIN dbo.D_PodAll C ON A.Pod_ID = C.Pod_ID
INNER JOIN dbo.D_SpecCodes D On A.Spec_ID = D.Spec_ID
INNER JOIN dbo.D_GppCodes E ON A.Gpp_ID = E.Gpp_ID
LEFT JOIN dbo.D_CalcResults_ReportMasterIDs F ON A.ReportMasterID = F.ReportMaster_ID
LEFT JOIN dbo.D_CalcResultsMain G ON F.RM_ID = G.RM_ID
AND A.MonthNo = G.Month
AND A.Spec_ID = G.Spec_ID
AND A.Pod_ID = G.POD_ID
AND A.Hrg_ID = G.HRG_ID
WHERE G.Category_Type = 0
AND (Activity_Actual <>0 OR Price_Actual <>0)
AND CASE WHEN
(Pod_Code = 'DI' THEN A.SpellAdmissionDate = G.SpellAdmissionDate) END
GROUP BY Pod_Code
ORDER BY Pod_Code;
您好,
我已经编写了上述代码,并且我已尝试在CASE WHEN
和WHERE
区域使用我的JOIN
语句,但没有任何乐趣。我想要实现的是,当 POD代码 DI 时,还会链接仅具有相同录取日期的数据,如目前行正在重复。问题是我不能只链接这个字段,因为不是每一行都有一个入场日期,所以它会导致输出问题。到目前为止,我已经在网站上阅读了各种解决方案,并试图让它们适应这种情况,但没有快乐(无法运行或错误信息等)。任何帮助将不胜感激。
此致 SIE
答案 0 :(得分:0)
请参阅下面WHERE子句中使用CASE语句的正确语法
AND
A.SpellAdmissionDate= CASE WHENPod_Code = 'DI' THEN G.SpellAdmissionDate END
答案 1 :(得分:0)
这可以用简单的OR子句表示:
AND (A.SpellAdmissionDate = G.SpellAdmissionDate OR Pod_Code != 'DI' OR Pod_Code IS NULL)
如果Pod_Code不可为空,则会减少为:
AND (A.SpellAdmissionDate = G.SpellAdmissionDate OR Pod_Code != 'DI')