简体   繁体   中英

How can I make these two queries a single query with an inner join?

I have these two queries that I need to make into a single query. Keyfield1 and TPOLNO should be the join fields. How would I go about making this a single query?

SELECT TPOLNO, SUM(TTSAMT) AS SUM FROM PFPOSTR410 WHERE 
((TTRNYY=2012 AND TTRNMM=3 AND TTRNDD>=27) OR (TTRNYY=2012 AND TTRNMM>3) OR 
(TTRNYY=2013 AND TTRNMM<=2) OR (TTRNYY=2013 AND TTRNMM=3 AND TTRNDD<=27)) 
GROUP BY TPOLNO HAVING SUM(TTSAMT)>=5000 ORDER BY TPOLNO ASC

SELECT KEYFIELD1, KEYFROBJ FROM CMRELATN WHERE RELROLETC=8

Thanks in advance for any direction!

  • Josh

If i understand your questions, you need to do a simple inner join of the 2 tables:

SELECT TPOLNO, SUM(TTSAMT) AS SUM, KEYFIELD1, KEYFROBJ
FROM PFPOSTR410, CMRELATN 
WHERE 
    ((TTRNYY=2012 AND TTRNMM=3 AND TTRNDD>=27) OR (TTRNYY=2012 AND TTRNMM>3) OR 
(TTRNYY=2013 AND TTRNMM<=2) OR (TTRNYY=2013 AND TTRNMM=3 AND TTRNDD<=27))
    AND KEYFIELD1=TPOLNO
    AND RELROLETC=8
GROUP BY TPOLNO, KEYFIELD1, KEYFROBJ
HAVING SUM(TTSAMT)>=5000
ORDER BY TPOLNO ASC

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM