我从几张桌子中选择:
SELECT SUM(S2.F64) as sumAll
FROM someTABLE1 S1
LEFT OUTER JOIN someTABLE2 S2 ON S2.F32 = S1.F32
WHERE S2.F01 = '1006'
and S2.F68='ORDER' AND S1.F67='ITEM'
现在我想从另一个选择中得到S1.F01的值:类似这样的
SELECT S3.F01
FROM someTABLE3 S3
WHERE "something..."
所以我想要这样的事情:
SELECT SUM(S1.F64) as sumAll, S3.F01
FROM someTABLE3 S3
LEFT OUTER JOIN someTABLE1 S1 ON S1.F01 = S3.F01
LEFT OUTER JOIN someTABLE2 AS S2 ON S1.F32 = S2.F32
WHERE S1.F01 = S3_F01
and S2.F1068='ORDER' AND S1.F1067='ITEM'
但是我想获取S3中的所有项目,只需填写条件匹配的sumAll中的列。
我想要这样的东西(伪代码):
SELECT * from S3
FOR EVERY (S3.F01)
{
SELECT * FROM S1
if(S1.F1067='ITEM')
RETURN S3 AND SUM(S1.F64)
ELSE
RETURN S3
}
有没有这样做?
答案 0 :(得分:1)
您是否只是在寻找GROUP BY
?
SELECT SUM(S1.F64) as sumAll, S3.F01
FROM someTABLE3 S3
LEFT OUTER JOIN someTABLE1 S1 ON S1.F01 = S3.F01
LEFT OUTER JOIN someTABLE2 AS S2 ON S1.F32 = S2.F32
WHERE S2.F1068='ORDER' AND S1.F1067='ITEM'
GROUP BY S3.F01
编辑 - 将where条件移动到连接...
SELECT SUM(S1.F64) as sumAll, S3.F01
FROM someTABLE3 S3
LEFT OUTER JOIN someTABLE1 S1 ON S1.F01 = S3.F01 AND S1.F1067='ITEM'
LEFT OUTER JOIN someTABLE2 AS S2 ON S1.F32 = S2.F32 AND S2.F1068='ORDER'
GROUP BY S3.F01
答案 1 :(得分:0)
不完全确定我完全了解你,但这是一种方法:
SELECT S3.*, SQ.sumValue
FROM someTABLE3 S3
LEFT JOIN( SELECT F01,SUM(F64)
FROM someTable1
WHERE F1067='ITEM'
GROUP BY F01) SQ ON SQ.F01 = S3.F01
LEFT JOIN someTable1 AS S1 ON S1.F01 = S3.F01
LEFT JOIN someTABLE2 AS S2 ON S1.F32 = S2.F32 AND S2.F1068='ORDER'
您执行内部查询,在SUM(F64) GROUP BY F01
上获得table1
。这样,当您从LEFT JOIN
执行table3
时,您将获得所需的table3
和K或SUM
。
然后对于LEFT JOIN
table2
,LEFT JOIN
首先table1
,因为我不知道您是否可以将table2
与table3
联系起来{{1}} {1}}。
这有点令人困惑,但我希望你理解这种方法