我想问一下是否有办法修改我的SQL Select语句以防止冗余行,如下所示。
SELECT tblIssueCommsItem.ItemID, cardNo, tblIssueCommsEqp.currentQty FROM tblIssueCommsItem
INNER JOIN tblCommsItem ON tblIssueCommsItem.ItemID = tblCommsItem.ItemID
INNER JOIN tblIssueCommsEqp ON tblIssueCommsItem.EqpID = tblIssueCommsEqp.EqpID
INNER JOIN tblSetInfo ON tblSetInfo.EqpID = tblIssueCommsEqp.EqpID
WHERE SetID = 11 AND (cardNo = 'TK 14' OR cardNo = '') AND cardNo != 'Unassigned'
AND tblIssueCommsItem.VoucherNo = 10006102 AND tblIssueCommsEqp.currentQty > 0
ORDER BY cardNo, serial
我得到了什么:
ItemID: 1386, cardNO: *blank*, currentQty: 10
ItemID: 1386, cardNO: *blank*, currentQty: 1
ItemID: 1387, cardNO: *blank*, currentQty: 10
ItemID: 1387, cardNO: *blank*, currentQty: 1
ItemID: 1101, cardNO: TK 14, currentQty: 10
ItemID: 1101, cardNO: TK 14, currentQty: 1
ItemID: 2575, cardNO: TK 14, currentQty: 10
ItemID: 2575, cardNO: TK 14, currentQty: 1
SetInfo
包含属于集合的不同EqpIDs
。在这种情况下,集合为SetID
11.我想要实现的目标如下:
ItemID: 1386, cardNO: *blank*, currentQty: 10
ItemID: 1387, cardNO: *blank*, currentQty: 10
ItemID: 1101, cardNO: TK 14, currentQty: 10
ItemID: 2575, cardNO: TK 14, currentQty: 10
我意识到冗余行只发生在我在cardNO = 'TK 14'
集合中有多个项目时。如您所见,冗余行是currentQty = 1
的行。我的数据库中没有这样的记录。我认为它只显示为1,因为它是int
字段。 currentQty
不同,这些不是真正重复的行,因此我无法使用DISTINCT
来摆脱它。
我对JOIN
的概念并不好,但我尝试了各种JOIN
,它会产生完全相同的结果。
任何帮助表示赞赏!
更新:解决了问题。感谢那些澄清不提供默认整数的SQL行为的人。 tblIssueCommsEqp.*
,帮助我识别罪魁祸首,并意识到我需要tblIssueCommsEqp.VoucherNo = 10000602 and tblIssueCommsItem.VoucherNo = 1000602
来获得我想要的东西。
由于我的概念仍然很弱,我仍然不确定为什么如果我在一组中只有1项“TK 14”,那么一切都按照正常情况运作。
对于有助于修改我的陈述的答案,这些可能也有用,但它们有点作弊,我只会将它作为最后的手段。
答案 0 :(得分:0)
假设SQL Server,例如:
group by tblIssueCommsItem.ItemID, cardNo, tblIssueCommsEqp.currentQty
having max(tblIssueCommsEqp.currentQty)
不确定是否适用于订单,但希望能让您朝着正确的方向前进:)
答案 1 :(得分:0)
我想你想要:
SELECT
tblIssueCommsItem.ItemID,
cardNo,
MAX(tblIssueCommsEqp.currentQty)
FROM
tblIssueCommsItem
INNER JOIN tblCommsItem
ON tblIssueCommsItem.ItemID = tblCommsItem.ItemID
INNER JOIN tblIssueCommsEqp
ON tblIssueCommsItem.EqpID = tblIssueCommsEqp.EqpID
INNER JOIN tblSetInfo
ON tblSetInfo.EqpID = tblIssueCommsEqp.EqpID
WHERE
SetID = 11
AND (
cardNo = 'TK 14'
OR cardNo = ''
)
AND cardNo != 'Unassigned'
AND tblIssueCommsItem.VoucherNo = 100006102
AND tblIssueCommsEqp.currentQty > 0
GROUP BY
tblIssueCommsItem.ItemID,
cardNo
ORDER BY
cardNo
答案 2 :(得分:0)
试试这个:
SELECT tblIssueCommsItem.ItemID, cardNo, q.currentQty
FROM tblIssueCommsItem
INNER JOIN tblCommsItem ON tblIssueCommsItem.ItemID = tblCommsItem.ItemID
INNER JOIN
(
select EqpID, MAX(currentQty) as currentQty
from tblIssueCommsEqp
group by EqpID
having max(currentQty) > 0
) q ON tblIssueCommsItem.EqpID = q.EqpID
INNER JOIN tblSetInfo ON tblSetInfo.EqpID = tblIssueCommsEqp.EqpID
WHERE SetID = 11 AND (cardNo = 'TK 14' OR cardNo = '')
AND tblIssueCommsItem.VoucherNo = 100006102
-- The below line is redundant, though it may still be useful in matching an index
-- AND cardNo != 'Unassigned'
ORDER BY cardNo, serial
答案 3 :(得分:0)
这可能会有所帮助。我认为包含 max function 会有所帮助,因为你包含了 max 内置函数,所以删除 tblIssueCommsEqp.currentQty> where子句中的0条件。
SELECT tblIssueCommsItem.ItemID, cardNo, Max(tblIssueCommsEqp.currentQty)
FROM tblIssueCommsItem
INNER JOIN tblCommsItem ON tblIssueCommsItem.ItemID = tblCommsItem.ItemID
INNER JOIN tblIssueCommsEqp ON tblIssueCommsItem.EqpID = tblIssueCommsEqp.EqpID
INNER JOIN tblSetInfo ON tblSetInfo.EqpID = tblIssueCommsEqp.EqpID
WHERE SetID = 11
AND (cardNo = 'TK 14' OR cardNo = '')
AND cardNo != 'Unassigned'
AND tblIssueCommsItem.VoucherNo = 100006102
ORDER BY cardNo, serial;