[英]SQL Query that will omit records with the same ID based on values in another column
* UPDATE* *更新*
Upon further review, the table I am using also has a linenumber
column. 经过进一步审查,我使用的表格也有一个linenumber
栏。 See updated example data below. 请参阅下面的更新示例数据 I feel like this could be extremely helpful in solving this....just not sure how. 我觉得这对解决这个问题非常有帮助....只是不确定如何。 Sum up the line number by PO, and if it equals 1 it is a single line, if it is greater than 1 it is a multi line....does that do anything for us? 通过PO总结行号,如果它等于1,则它是单行,如果它大于1,则它是多行....这对我们做了什么?
New here and to SQL so please forgive my ignorance. 在这里和SQL新,所以请原谅我的无知。 Hopefully this is an easy answer. 希望这是一个简单的答案。
Looking to build 3 similar queries that will return purchase orders that contain more than 1 item and: 希望构建3个类似的查询,这些查询将返回包含多个项目的采购订单,并且:
Data looks like this... 数据看起来像这样......
PONUMBER ITEMNUMBER LOTCONTROLLED LINENUMBER
PO1.18 OSC1024 0 1
PO1.18 OSC1025 0 2
PO1.18 OSC1026 0 3
PO1.2 OSC1199 0 1
PO1.2 OSC1200 1 2
PO1.21 OSC1201 1 1
PO1.21 OSC1202 1 2
PO1.22 OSC1203 1 1
PO1.23 OSC1204 1 1
PO1.23 OSC1205 0 2
PO1.24 OSC1206 1 1
PO1.24 OSC1207 1 2
PO1.24 OSC1300 0 3
Query for NO Lot Controlled items works great... 查询NO Lot Controlled项目效果很好......
SELECT
`POD`.`PONUMBER`,
`POD`.`ITEMNUMBER`,
`POD`.`LOTCONTROLLED`
FROM
table1 AS `POD`
INNER JOIN
(
SELECT `PONUMBER`, COUNT(`PONUMBER`)
FROM table1
WHERE `LOTCONTROLLED` = 0
GROUP BY `PONUMBER`
HAVING (COUNT(`PONUMBER`) > 1)
) as `POD1`
ON `POD`.`PONUMBER` = `POD1`.`PONUMBER`
I thought it would be as simple as changing the WHERE LOTCONTROLLED
to be = 1, to get Purchase Orders with ALL Lot Controlled items, but that returns some Purchase Orders that have mixed lines as well. 我认为这将简单到将WHERE LOTCONTROLLED
更改为= 1,以获得包含所有批次控制项目的采购订单,但这会返回一些具有混合线的采购订单。
How can I eliminate a purchase order from inclusion if any one of the lines are not lot controlled? 如果任何一条生产线未经批次控制,我如何从包含中删除采购订单?
I like using NOT EXISTS
here: 我喜欢在这里使用NOT EXISTS
:
SELECT POD.*
FROM table1 POD
JOIN (SELECT PONUMBER
FROM table1 POD
WHERE NOT EXISTS (SELECT *
FROM table1 POD1
WHERE POD.PONUMBER = POD1.PONUMBER
AND POD1.LOTCONTROLLED = 1)
GROUP BY PONUMBER
HAVING COUNT(*) > 1
) POD1 ON POD.PONUMBER = POD1.PONUMBER
This will omit the PONUMBER
from results if any record from that PONUMBER
has LOTCONTROLLED
= 1 or 0, depending on what you put in the exists subquery. 如果来自该PONUMBER
任何记录的LOTCONTROLLED
= 1或0,这将从结果中省略PONUMBER
,具体取决于您在exists子查询中放置的内容。
To get only records that have a mix, you can use COUNT().. HAVING
: 要仅获取具有混合的记录,可以使用COUNT().. HAVING
:
SELECT PONUMBER,
ITEMNUMBER,
LOTCONTROLLED
FROM table1 POD
JOIN (SELECT PONUMBER
FROM table1
GROUP BY PONUMBER
HAVING COUNT(DISTINCT LOTCONTROLLED) = 2
) POD1 ON POD.PONUMBER = POD1.PONUMBER
Looks like you also need to join the queries by Lot Controlled too, so I added it to the Group By and Inner Select so it could be joined: 看起来你也需要通过Lot Controlled加入查询,所以我将它添加到Group By和Inner Select中,以便它可以加入:
SELECT
`POD`.`PONUMBER`,
`POD`.`ITEMNUMBER`,
`POD`.`LOTCONTROLLED`
FROM
table1 AS `POD`
INNER JOIN
(
SELECT `PONUMBER`, 'LOTCONTROLLED', COUNT(`PONUMBER`)
FROM table1
WHERE `LOTCONTROLLED` = 0
GROUP BY `PONUMBER`, 'LOTCONTROLLED'
HAVING (COUNT(`PONUMBER`) > 1)
) as `POD1`
ON `POD`.`PONUMBER` = `POD1`.`PONUMBER` AND `POD`.`LOTCONTROLLED` = `POD1`.`LOTCONTROLLED`
SELECT
`POD`.`PONUMBER`,
`POD`.`ITEMNUMBER`,
`POD`.`LOTCONTROLLED`
FROM
table1 AS `POD`
INNER JOIN
(
SELECT `PONUMBER`, 'LOTCONTROLLED', COUNT(`PONUMBER`)
FROM table1
WHERE `LOTCONTROLLED` = 1
GROUP BY `PONUMBER`, 'LOTCONTROLLED'
HAVING (COUNT(`PONUMBER`) > 1)
) as `POD1`
ON `POD`.`PONUMBER` = `POD1`.`PONUMBER` AND `POD`.`LOTCONTROLLED` = `POD1`.`LOTCONTROLLED`
SELECT
`POD`.`PONUMBER`,
`POD`.`ITEMNUMBER`,
`POD`.`LOTCONTROLLED`
FROM
table1 AS `POD`
INNER JOIN
(
SELECT `PONUMBER`, 'LOTCONTROLLED', COUNT(`PONUMBER`)
FROM table1
WHERE `LOTCONTROLLED` IN (0,1)
GROUP BY `PONUMBER`, 'LOTCONTROLLED'
HAVING (COUNT(`PONUMBER`) > 1)
) as `POD1`
ON `POD`.`PONUMBER` = `POD1`.`PONUMBER` AND `POD`.`LOTCONTROLLED` = `POD1`.`LOTCONTROLLED`
Window functions are the simplest method, but you probably don't have those. 窗口函数是最简单的方法,但您可能没有。 So, just use the min()
and max()
of lotcontrolled
. 所以,只需使用lotcontrolled
的min()
和max()
。 The basic query is: 基本查询是:
select pod.*
from table1 pod join
(select ponumber, min(lotcontrolled) as min_lc, max(lotcontrolled) as max_lc
from table1 pod
group by ponumber
having count(*) > 1
) p
using (ponumber)
Then your three conditions are: 那你的三个条件是:
max_lc = 0 -- no lot controlled
min_lc = 1 -- all lot controlled
min_lc <> max_lc -- mixed
Some people might prefer the more verbose versions: 有些人可能更喜欢更冗长的版本:
min_lc = max_lc and max_lc = 0 -- no lot controlled
min_lc = max_lc and max_lc = 1 -- all lot controlled
min_lc <> max_lc -- mixed
try something like this: 尝试这样的事情:
--No items in the group contain LotControlled
SELECT *
FROM your_table
WHERE ponumber IN (SELECT ponumber
FROM your_table
GROUP BY ponumber
HAVING Sum(CONVERT(INT, lotcontrolled)) = 0)
--All Items Contain
SELECT *
FROM your_table
WHERE ponumber IN (SELECT ponumber
FROM your_table
GROUP BY ponumber
HAVING Sum(CONVERT(INT, lotcontrolled)) = Count(*))
--mixed
SELECT *
FROM your_table
WHERE ponumber IN (SELECT ponumber
FROM your_table
GROUP BY ponumber
HAVING Sum(CONVERT(INT, lotcontrolled)) != Count(*)
AND Sum(CONVERT(INT, lotcontrolled)) > 0)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.