I have 4 tables and would like to return the tables in the most efficient manner where the keys map to their correct entries.
Table A columns:
[ SVC_PREFIX | SVC_CD | ... ]
Table B columns:
[ SVC_PREFIX | SVC_CD | SVC_ID |... ]
Table C columns:
[ SVC_ID | ... ]
Table D columns:
[ SVC_ID | ... ]
Table A maps to Table B by SVC_PREFIX and SVC_CD
Table B is then responsible for retrieving Table C and D via SVC_ID
Table C and D are nearly the same except Table D has a few less columns than C
For instance, C may have a isGood and isSilly but D will only have isSilly.
Is it possible to retrieve the entries such that D will be pulled with a NULL value for isSilly while C will bring the corresponding data?
Here’s an example:
Table A columns:
[ SVC_PREFIX | SVC_CD | anotherData ]
[ 111 | 123 | AAA ]
Table B columns:
[ SVC_PREFIX | SVC_CD | SVC_ID ]
[ 111 | 123 | 007]
Table C columns:
[ SVC_ID | isGood | isHappy ]
[ 007 | Y | Y ]
Table D columns:
[ SVC_ID | isGood ]
[ 007 | Y ]
Data I’d like to retrieve:
[ SVC_PREFIX | SVC_CD | anotherData | SVC_ID | isGood | isHappy ]
Actual data
[ 111 | 123 | AAA | 007 | Y | Y ]
[ 111 | 123 | AAA | 007 | Y | NULL ]
I’d like to display both C and D’s table as separate rows.
Currently, I have an implicit join, but does not achieve the result I desire…
SELECT *
FROM A,B,C,D
WHERE A.SVC_PREFIX = B.SVC_PREFIX
AND A.SVC_CD = B.SVC_CD
AND B.SVC_ID = C.SVC_ID
AND B.SVC_ID = D.SVC_ID
AND C.SVC_ID = D.SVC_ID
Thanks for the help!
You can use a UNION. I believe this would work in SQL Server.