如何使用内部联接将这两个查询作为单个查询?

时间:2013-05-06 14:08:29

标签: sql inner-join

我需要在单个查询中进行这两个查询。 Keyfield1和TPOLNO应该是连接字段。我将如何进行单一查询?

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

提前感谢任何方向!

  • 约什

1 个答案:

答案 0 :(得分:-1)

如果我理解你的问题,你需要对2个表进行简单的内连接:

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