我有两个已加入的分组SQL查询。第一个查询是提取零件号,计划员ID,数量总和和成本。第二个查询嵌入到语句中,只是部件号和事务数的计数。我正在加入关于部件号的两个查询。见下面的代码。 我已经对此进行了大量的搜索并且已经非常接近,但我仍然无法获得查询中的所有列。当我运行查询时,我得到以下内容: prtnumb_15,planid_01,数量,成本。但我错过了NoTrans(交易数量)列。我不知道我的语法是否有点偏差或者我是否遗漏了什么。任何帮助表示赞赏。
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
答案 0 :(得分:0)
正如其他人已经评论过,您在外部选择中缺少NoTrans
。你需要包含该列。所以改变你的外部选择,如
SELECT TH1.prtnum_15,
PM1.planid_01,
SUM(TH1.tnxqty_15) AS 'Qty',
PM1.cost_01 * PM1.cstcnv_01 AS 'Cost',
TH2.NoTrans
FROM transaction_history TH1
......
<rest of your query>
......