日期:2014-05-18  浏览次数:20452 次

两查询结果相加后没有数据了。
SQL code
SELECT TOP 1 SUM(QTY) FROM MRT WHERE JOBNO=RESVT.JOBNO AND MRT.CODE=RESVT.CODE AND P_CODE=RESVT.P_CODE

SQL code
SELECT TOP 1 SUM(KT.QTY)
         FROM KT  INNER JOIN KTUDF  ON(KT.ITEMNO=KTUDF.ITEMNO AND KT.KINNO=KTUDF.KINNO)
         WHERE  KT.KTYPE='02' AND KTUDF.GDH=(CASE WHEN MASTERJOB = '' THEN JOBNO ELSE MASTERJOB END) AND KT.CODE=RESVT.CODE 



SQL code


select sum(n.col1+m.col1) col
from
(
SELECT TOP 1 SUM(QTY)col1 FROM MRT WHERE JOBNO=RESVT.JOBNO AND MRT.CODE=RESVT.CODE AND P_CODE=RESVT.P_CODE
)m,
( 
SELECT TOP 1 SUM(KT.QTY)col1
         FROM KT  INNER JOIN KTUDF  ON(KT.ITEMNO=KTUDF.ITEMNO AND KT.KINNO=KTUDF.KINNO)
         WHERE  KT.KTYPE='02' AND KTUDF.GDH=(CASE WHEN MASTERJOB = '' THEN JOBNO ELSE MASTERJOB END) AND KT.CODE=RESVT.CODE
)n



------解决方案--------------------
SQL code

select sum(n.col1+m.col1) col
from
(
SELECT TOP 1 SUM(QTY)col1 FROM MRT WHERE JOBNO=RESVT.JOBNO AND MRT.CODE=RESVT.CODE AND P_CODE=RESVT.P_CODE
)m,
( 
SELECT TOP 1 SUM(KT.QTY)col1
         FROM KT  INNER JOIN KTUDF  ON(KT.ITEMNO=KTUDF.ITEMNO AND KT.KINNO=KTUDF.KINNO)
         WHERE  KT.KTYPE='02' AND KTUDF.GDH=(CASE WHEN MASTERJOB = '' THEN JOBNO ELSE MASTERJOB END) AND KT.CODE=RESVT.CODE
)n

where m.col=n.col

--你的关联条件

------解决方案--------------------
SQL code
select sum(col1+col2) col
from
(
col1=(SELECT TOP 1 SUM(QTY)col1 FROM MRT WHERE JOBNO=RESVT.JOBNO AND 
MRT.CODE=RESVT.CODE AND P_CODE=RESVT.P_CODE
), 
col2=(SELECT TOP 1 SUM(KT.QTY)col1
         FROM KT  INNER JOIN KTUDF  
         ON(KT.ITEMNO=KTUDF.ITEMNO AND KT.KINNO=KTUDF.KINNO)
         WHERE  KT.KTYPE='02' AND 
         KTUDF.GDH=(CASE WHEN MASTERJOB = '' THEN JOBNO ELSE MASTERJOB END) 
         AND KT.CODE=RESVT.CODE
))