简体   繁体   English

SQL查询将根据另一列中的值省略具有相同ID的记录

[英]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个类似的查询,这些查询将返回包含多个项目的采购订单,并且:

  1. Contain NO Lot Controlled Items 包含NO Lot Controlled Items
  2. Contain ALL Lot Controlled Items 包含所有批次控制的项目
  3. Contain a MIX of Lot Controlled and Non-Lot Controlled Items 包含批次控制和非批次控制项目的混合

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中,以便它可以加入:

NO LOT CONTROLLED: 没有很多控制:

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`

LOT CONTROLLED: 很多控制:

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`

ALL LOT CONTROLLED: 所有受控制的:

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 . 所以,只需使用lotcontrolledmin()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.

相关问题 SQL查询以基于同一字段和条件中的2个不同值选择记录 - SQL Query to select records based on 2 different values in the same field and a condition SQL查询以传输THE SAME表的另一列上的所有记录 - Sql Query to transfer all records on another column on THE SAME table 在同一列上通过另一列SQL查询中的值进行AVG - AVG on same column by values in another column single sql query SQL查询根据ID汇总记录 - SQL query to aggregate records based on ID SQL:获取具有相同ID的所有列记录 - SQL: get all column records with same id SQL | 如何根据记录ID和另一列获取所有记录ID? (可能是简单的问题:)) - SQL | How to get all records IDs based on record id and another column ? (probably simple issue :) ) SQL查询列出所有记录,其中一列的所有值不在另一列中 - SQL Query for listing all records where all values for one column are not in another column 获取一个查询以列出在同一ID的特定列的开始和结束值之间和之间的记录 - Get a query to list the records that are on and in between the start and the end values of a particular column for the same Id 如何编写一个查询,在 sql 中连接同一个表并将一个表的列值与另一个表中的相同列进行比较 - How to write a query that joins same table in sql and compares the column values of one table with same column in another table SQL查询根据另一列中的唯一元素在列中查找公共ID - SQL Query Finding a common id in a column based on unique elements in another column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM