0

I have data in two tables that I am joining. Table A looks like this:

VISIT ID | ADM_DATE   | ...
12345678 | 1800-01-01 | ...
...

Table B looks like this:

VISIT ID | CLASFPRIO | CLASFCD | CLASFTYPE
12345678 | 01        | ###.##  | DF
12345678 | 02        | ###.##  | DF
12345678 | 03        | ###.##  | DF

What I am trying to achieve is output that looks like this:

A.VISIT ID | B.CLASFCD_1 | B.CLASFCD_2 | B.CLASFCD_2
12345678   | ###.##      | ###.##      | ###.##
...

Table A has columns in it that I set conditions on in a where clause and so does table B. Here is what I am trying so far, which works, its just a lot of code:

DECLARE @SD DATETIME;
DECLARE @ED DATETIME;
DECLARE @CP_1 INT;
DECLARE @CP_2 INT;
DECLARE @CP_3 INT;
DECLARE @SCT VARCHAR(10);

SET @SD = '2014-01-01';
SET @ED = '2014-02-01';
SET @CP_1 = 1;
SET @CP_2 = 2;
SET @CP_3 = 3;
SET @SCT = 'DF';


DECLARE @ICD9_1 TABLE (
PtNo_Num VARCHAR(20)
, ICD_1 VARCHAR(10)
)

INSERT INTO @ICD9_1
SELECT B.PtNo_Num
, B.CLASFCD_1

FROM (
SELECT DISTINCT PtNo_Num,
ClasfCd AS CLASFCD_1

FROM smsdss.BMH_PLM_PtAcct_Clasf_Dx_V

WHERE ClasfPrio = @CP_1
AND SortClasfType = @SCT
) B

--SELECT * FROM @ICD9_1

-----------------------------------------------------------------------
DECLARE @ICD9_2 TABLE (
PtNo_Num VARCHAR(20)
, ICD_2 VARCHAR(10)
)

INSERT INTO @ICD9_2
SELECT C.PtNo_Num
, C.CLASFCD_2

FROM (
SELECT DISTINCT PtNo_Num,
ClasfCd AS CLASFCD_2

FROM smsdss.BMH_PLM_PtAcct_Clasf_Dx_V

WHERE ClasfPrio = @CP_2
AND SortClasfType = @SCT
) C

--SELECT * FROM @ICD9_2

-----------------------------------------------------------------------
DECLARE @ICD9_3 TABLE (
PtNo_Num VARCHAR(20)
, ICD_3 VARCHAR(10)
)

INSERT INTO @ICD9_3
SELECT D.PtNo_Num
, D.CLASFCD_3

FROM (
SELECT DISTINCT PtNo_Num,
ClasfCd AS CLASFCD_3

FROM smsdss.BMH_PLM_PtAcct_Clasf_Dx_V

WHERE ClasfPrio = @CP_3
AND SortClasfType = @SCT
) D

--SELECT * FROM @ICD9_3

-----------------------------------------------------------------------
DECLARE @ICD9F TABLE(
PTNO_NUM VARCHAR(20)
, ICD9_1 VARCHAR(10)
, ICD9_2 VARCHAR(10)
, ICD9_3 VARCHAR(10)
)

INSERT INTO @ICD9F
SELECT
E.PTNO_NUM
, E.ICD9_1
, E.ICD9_2
, E.ICD9_3

FROM (

SELECT PV.PtNo_Num
, ICD9_1.ICD_1 AS ICD9_1
, ICD9_2.ICD_2 AS ICD9_2
, ICD9_3.ICD_3 AS ICD9_3

FROM smsdss.BMH_PLM_PtAcct_V PV
JOIN @ICD9_1 ICD9_1
ON PV.PtNo_Num = ICD9_1.PtNo_Num
JOIN @ICD9_2 ICD9_2
ON PV.PtNo_Num = ICD9_2.PtNo_Num
JOIN @ICD9_3 ICD9_3
ON PV.PtNo_Num = ICD9_3.PtNo_Num

WHERE PV.Adm_Date >= @SD
AND PV.Adm_Date < @ED
AND PV.Plm_Pt_Acct_Type = 'I'
AND PV.PtNo_Num < '20000000'
) E

SELECT * FROM @ICD9F

While this does work, it is a lot of code and it does get a little confusing, so I am changing the question to one of optimizing the query.

UPDATE: THE CLASFCD CAN BE ALPHA-NUMERIC ALSO UPDATE CODE TO WHAT I AM NOW WORKING WITH AS SUGGESTED BY @METAPHOR

Thank you,

1 Answer 1

1

Try this (paraphrased):

select 
    VisitID,
    sum(case when classfprio = '01' then classfcd else 0 end) as ClassFCD1,
    sum(case when classfprio = '02' then classfcd else 0 end) as ClassFCD2,
    sum(case when classfprio = '03' then classfcd else 0 end) as ClassFCD3
from TableA a
join TableB b on b.VisitID = a.VisitID
group by VisitID, ClassFPrio
Sign up to request clarification or add additional context in comments.

4 Comments

will give it a try in the morning, thank you, interesting way to think about it.
This will not work, I did not realize that the ClasfCD can be alpha-numeric, I will upvote anyways for the effort and fix my question.
Yeah, that's a requirement of a cross-tab, the column needs to be an aggregate.
Now I am trying to create three tables, one for each clasfcd then create a 4th, which seems to work, it's just a lot of code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.