Question

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;

Hi,

I've wrote the above code and I've tried using my CASE WHEN statement in both the WHERE and the JOIN area with no joy. What I'm trying to achieve is that when the POD Code is DI, then also link the data that ONLY has the same admission date, as at present the rows are duplicating. The problem is I can't just link on this field, as not every row has an admission date and so it causes problems with the output. I've read various solutions so far on the site and tried to adapt them to this context, but with no joy (fails to run or error messages etc). Any help would be greatly appreciated.

Regards, Sie

Was it helpful?

Solution

This can be expressed with a simple OR clause:

AND (A.SpellAdmissionDate = G.SpellAdmissionDate OR Pod_Code != 'DI' OR Pod_Code IS NULL)

In case Pod_Code is not nullable then this reduces to:

AND (A.SpellAdmissionDate = G.SpellAdmissionDate OR Pod_Code != 'DI')

OTHER TIPS

See correct syntax for using CASE statement in WHERE clause below

AND 
  A.SpellAdmissionDate= CASE WHENPod_Code = 'DI' THEN  G.SpellAdmissionDate END
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top