Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Please Help Me Combine These Queries

Status
Not open for further replies.

lrdave36

Technical User
Joined
Jan 6, 2010
Messages
77
Location
US
Hi guys,

I still can't seem to grasp the concept of combining queries together. UNION doesn't work in this case. I have these queries:


query 1

Code:
SELECT   AGTY_SYS_CD, count(*)  AS "TOTAL RETIREES" 
FROM     DSNP.PR01_T_RECIP_SYS 
WHERE    RECIP_RETIR_DT <= '2010-02-01' 
AND      BENEF_STAT_CD IN ('AC', 'DP') 
AND      RECIP_TYPE_CD = '10'
AND      BENEF_SEQ_NBR = 1 
GROUP BY AGTY_SYS_CD 
order by 2 desc


query 2


Code:
SELECT   AGTY_SYS_CD, count(*)  AS "NEW RETIREES" 
FROM     DSNP.PR01_T_RECIP_SYS 
WHERE    RECIP_RETIR_DT BETWEEN '2010-02-01' and '2010-02-28'
AND      BENEF_STAT_CD IN ('AC', 'DP') 
AND      RECIP_TYPE_CD = '10'
AND      BENEF_SEQ_NBR = 1 
GROUP BY AGTY_SYS_CD 
ORDER BY 2 DESC


The only difference between the two is the dates. I want my query to have one column for Total and one for New Retirees.


Can someone show me how to combine these using derived tables? I gave it a shot , but I don't know how to reference Alias named fields in the main query. Thanks!

 
Try
Code:
SELECT   AGTY_SYS_CD, 
sum(case when  RECIP_RETIR_DT BETWEEN '2010-02-01' and '2010-02-28' then 1 end) AS "NEW RETIREES",
sum(case when RECIP_RETIR_DT <= '2010-02-01' then 1 end) AS "TOTAL RETIREES"

 FROM     DSNP.PR01_T_RECIP_SYS 
WHERE   BENEF_STAT_CD IN ('AC', 'DP') AND      RECIP_TYPE_CD = '10'AND      BENEF_SEQ_NBR = 1 GROUP BY AGTY_SYS_CD ORDER BY 2 DESC

PluralSight Learning Library
 
Try using CASE. This is untested...

Code:
SELECT   AGTY_SYS_CD, 
  CASE WHEN RECIP_RETIR_DT <= '2010-02-01'
       THEN count(*)  AS "TOTAL RETIREES"
       ELSE count(*) AS "NEW RETIREEES"
FROM     DSNP.PR01_T_RECIP_SYS
WHERE    BENEF_STAT_CD IN ('AC', 'DP')
AND      RECIP_TYPE_CD = '10'
AND      BENEF_SEQ_NBR = 1
GROUP BY AGTY_SYS_CD
order by 2 desc

-SQLBill

The following is part of my signature block and is only intended to be informational.
Posting advice: FAQ481-4875
 
Dang - Markros was faster and more accurate than me. Oh well...


-SQLBill

The following is part of my signature block and is only intended to be informational.
Posting advice: FAQ481-4875
 
Thanks Markros. Yours works great. Can this be done with derived tables though? I'm trying to practice more with that technique. I came up with this, but it errors out on me:

Code:
SELECT   AGTY_SYS_CD,
         A.TOTAL RETIREES,
         B.NEW RETIREES
          
FROM (SELECT AGTY_SYS_CD, count(*) as "TOTAL RETIREES"


FROM     DSNP.PR01_T_RECIP_SYS 
WHERE    RECIP_RETIR_DT <= '2010-02-01' 
AND      BENEF_STAT_CD IN ('AC', 'DP') 
AND      RECIP_TYPE_CD = '10'
AND      BENEF_SEQ_NBR = 1 
GROUP BY AGTY_SYS_CD ) as A,

(

SELECT   AGTY_SYS_CD, count(*)  AS "NEW RETIREES" 
FROM     DSNP.PR01_T_RECIP_SYS 
WHERE    RECIP_RETIR_DT BETWEEN '2010-02-01' and '2010-02-28'
AND      BENEF_STAT_CD IN ('AC', 'DP') 
AND      RECIP_TYPE_CD = '10'
AND      BENEF_SEQ_NBR = 1 
GROUP BY AGTY_SYS_CD ) as B

FROM DSNP.PR01_T_RECIP_SYS
WHERE A.RECIP_SSN_NBR = B.RECIP_SSN_NBR

ORDER BY 2 DESC


 
Code:
SELECT   A.AGTY_SYS_CD,
         A.TOTAL RETIREES,
         B.NEW RETIREES
          
FROM (SELECT AGTY_SYS_CD, 
count(*) as "TOTAL RETIREES"


FROM     DSNP.PR01_T_RECIP_SYS
WHERE    RECIP_RETIR_DT <= '2010-02-01'
AND      BENEF_STAT_CD IN ('AC', 'DP')
AND      RECIP_TYPE_CD = '10'
AND      BENEF_SEQ_NBR = 1
GROUP BY AGTY_SYS_CD ) as A

INNER JOIN 
(

SELECT   AGTY_SYS_CD, count(*)  AS "NEW RETIREES"
FROM     DSNP.PR01_T_RECIP_SYS
WHERE    RECIP_RETIR_DT BETWEEN '2010-02-01' and '2010-02-28'
AND      BENEF_STAT_CD IN ('AC', 'DP')
AND      RECIP_TYPE_CD = '10'
AND      BENEF_SEQ_NBR = 1
GROUP BY AGTY_SYS_CD ) as B


ON A.AGTY_SYS_CD = B.AGTY_SYS_CD

ORDER BY 2 DESC

PluralSight Learning Library
 
Thanks again, Markros! So with derived tables, I can only join on a field that is being referenced in the inner selects? Is this why you joined on agty_sys_cd instead of recip_ssn_nbr?

I did have to go with a LEFT OUTER JOIN as the B table wouldn't show all the agencies if the count was 0.
 
Yes, you can only use fields from inner selects - you can not use fields from the tables that form that select - they don't exist anymore as far as query is concerned.

The original code we used is more efficient here, than derived tables approach - I just showed you the principle.

PluralSight Learning Library
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top