t1:
SO | SOL | QTY |
125 | 1 | 10 |
126 | 1 | 10 |
127 | 1 | 75 |
t2:
SO | SOL | JOB | QTY |
125 | 1 | 12345 | 1 |
126 | 1 | 12346 | 2 |
127 | 1 | 12347 | 3 |
127 | 1 | 12348 | 4 |
t3:
t1.SO | t1.SOL | t1.QTY | sum(t2.QTY) |
125 | 1 | 10 | 1 |
126 | 1 | 10 | 2 |
127 | 1 | 75 | 7 |
要求:
由t1+t2得到t3, 条件:t1.so+t1.sol=t2.so+t2.sol (so+sol唯一)
我的思路:首先将t2根据so+sol汇总QTY。然后与t1连接。但是不知道怎么写。
SELECT t1.SO, t1.SOL, t1.QTY, t3.QTY
FROM t1, (SELECT t2.SO, t2.SOL, SUM(t2.QTY) AS QTY FROM t2 group by SO) as t3
WHERE t1.SO = t3.SO AND t1.SOL = t3.SOL
我在sqlite上面试的,因为我没有access。