1
我有兩個分組的sql查詢,我已經加入。第一個查詢是提取零件號,計劃員編號,數量總和和成本。嵌入到語句中的第二個查詢只是部件號和事務數的計數。我參加了部件號上的兩個查詢。看下面的代碼。 我已經做了大量的搜索,並已變得非常接近,但我仍然無法獲得查詢中的所有列。當我運行查詢時,我得到以下內容: prtnumb_15,planid_01,數量,成本。但我錯過了NoTrans(交易數量)列。我不知道我的語法是否稍微偏離了一點,或者我缺少某些東西。任何幫助表示讚賞。加入兩個sql分組查詢
SELECT TH1.prtnum_15, PM1.planid_01, SUM(TH1.tnxqty_15) AS 'Qty', PM1.cost_01 * PM1.cstcnv_01 AS 'Cost'
FROM transaction_history TH1
INNER JOIN part_master PM1
ON TH1.prtnum_15 = PM1.prtnum_01
INNER JOIN
(
SELECT TH2.prtnum_15, COUNT (TH2.TNXQTY_15) AS NoTrans
FROM transaction_history TH2
WHERE
NOT (TH2.prtnum_15 LIKE '0%'
OR TH2.prtnum_15 LIKE '1%'
OR TH2.prtnum_15 LIKE '8%'
OR TH2.prtnum_15 LIKE '9%'
OR TH2.prtnum_15 LIKE 'X%'
OR TH2.prtnum_15 LIKE 'Y%')
AND TH2.tnxdte_15 BETWEEN '2013-01-01 00:00:00' AND '2014-03-11 00:00:01'
AND TH2.tnxcde_15 IN ('R')
GROUP BY TH2.PRTNUM_15
) TH2 ON TH1.prtnum_15 = TH2.prtnum_15
WHERE
NOT (TH1.prtnum_15 LIKE '0%'
OR TH1.prtnum_15 LIKE '1%'
OR TH1.prtnum_15 LIKE '8%'
OR TH1.prtnum_15 LIKE '9%'
OR TH1.prtnum_15 LIKE 'X%'
OR TH1.prtnum_15 LIKE 'Y%')
AND TH1.tnxcde_15 IN ('I')
AND TH1.tnxdte_15 BETWEEN '2013-01-01 00:00:00' AND '2014-03-11 00:00:01'
GROUP BY TH1.PRTNUM_15, PM1.PLANID_01, PM1.cost_01 * PM1.cstcnv_01, TH2.NoTrans
ORDER BY TH1.prtnum_15
嗯沒有th2.NoTrans在選擇領域,有你把它包括在選擇? – cyan
看起來您只需要將'TH2.NoTrans'添加到'SELECT'語句。 –