oracle 如何选择所有在一列中具有相同值,但在另一列中具有不同对应值的条目?

nhhxz33t  于 2023-08-04  发布在  Oracle
关注(0)|答案(1)|浏览(130)

我在一家制造电脑芯片的工厂工作。
我们识别由“批次ID”生产的计算机芯片批次
我们还通过相同格式的“批次ID”识别这些芯片生产中使用的几种耗材批次。
此信息存储在Oracle数据库中名为MM_LOT_INV_ST_LG的表中。该表的一部分如下所示:
| 材料ID| MATERIAL_ID |
| --| ------------ |
| 芯片批次1| Chips Batch 1 |
| 芯片批次2| Chips Batch 2 |
| SMD胶带批次1| SMD Tape Batch 1 |
| SMD胶带批次2| SMD Tape Batch 2 |
| 盖带批次1| Cover Tape Batch 1 |
(SMD胶带和盖胶带是两种不同的耗材。)
批次ID应仅使用一次。例如,上述批次ID 1000585855涉及计算机芯片批次。它不应该被重复使用-甚至不为另一个计算机芯片批次。
然而,我记得有一次,一个计算机芯片批次和一个SMD磁带批次被分配了相同的批号。

我是否可以编写SQL查询来提取不同MATERIAL_ID之间共享的批次ID?

xkrw2x1b

xkrw2x1b1#

可以使用HAVIN COUNT DISTINCT
我已经添加了一个重复的行,以显示它的作品

CREATE TABLE MM_LOT_INV_ST_LG 
    ("LOT_ID" int, "MATERIAL_ID" varchar2(18))
;

x

INSERT ALL 
    INTO MM_LOT_INV_ST_LG  ("LOT_ID", "MATERIAL_ID")
         VALUES (1000585855, 'Chips Batch 1')
    INTO MM_LOT_INV_ST_LG  ("LOT_ID", "MATERIAL_ID")
         VALUES (1000585856, 'Chips Batch 2')
    INTO MM_LOT_INV_ST_LG  ("LOT_ID", "MATERIAL_ID")
         VALUES (1000586452, 'SMD Tape Batch 1')
    INTO MM_LOT_INV_ST_LG  ("LOT_ID", "MATERIAL_ID")
         VALUES (1000586453, 'SMD Tape Batch 2')
    INTO MM_LOT_INV_ST_LG  ("LOT_ID", "MATERIAL_ID")
         VALUES (1000456987, 'Cover Tape Batch 1')
      INTO MM_LOT_INV_ST_LG  ("LOT_ID", "MATERIAL_ID")
         VALUES (1000456987, 'Cover Tape Batch 5')
SELECT * FROM dual
;
6 rows affected
SELECT "LOT_ID" FROM MM_LOT_INV_ST_LG GROUP BY "LOT_ID" HAVING  COUNT(DISTINCT "MATERIAL_ID") > 1

的数据
| LOT_ID |
| ------------ |
| 1000456987 |
fiddle
如果他们都有共同的智力,你可以简单地排除他们

CREATE TABLE MM_LOT_INV_ST_LG 
    ("LOT_ID" int, "MATERIAL_ID" varchar2(40))
;
INSERT ALL 
    INTO MM_LOT_INV_ST_LG  ("LOT_ID", "MATERIAL_ID")
         VALUES (1000585855, 'Chips Batch 1')
    INTO MM_LOT_INV_ST_LG  ("LOT_ID", "MATERIAL_ID")
         VALUES (1000585856, 'Chips Batch 2')
    INTO MM_LOT_INV_ST_LG  ("LOT_ID", "MATERIAL_ID")
         VALUES (1000586452, 'SMD Tape Batch 1')
    INTO MM_LOT_INV_ST_LG  ("LOT_ID", "MATERIAL_ID")
         VALUES (1000586453, 'SMD Tape Batch 2')
    INTO MM_LOT_INV_ST_LG  ("LOT_ID", "MATERIAL_ID")
         VALUES (1000456987, 'Cover Tape Batch 1')
      INTO MM_LOT_INV_ST_LG  ("LOT_ID", "MATERIAL_ID")
         VALUES (1000456987, 'INT Cover Tape Batch 5')
SELECT * FROM dual
;
6 rows affected
SELECT "LOT_ID"  a FROM MM_LOT_INV_ST_LG 
  WHERE SUBSTR("MATERIAL_ID",1,3) <> 'INT' 
  GROUP BY "LOT_ID"
  HAVING  COUNT(DISTINCT "MATERIAL_ID") > 1

fiddle

相关问题