我目前不得不使用两个不同的查询来取回我正在寻找的结果。我已经尝试将两个查询组合在一起,但最终我得到了大量不需要的额外(重复)数据。下面我列出了工作查询。
SELECT p1.note as Itemcode,
order.ID as OrderNo,
piece1.ID As Piece1,
piece2.ID As Piece2,
i1.count as Unit,
unit.count as TotalUnits,
i1.rack as RackNo,
p1.EndDate as Piece1Finish,
p2.EndDate as Piece2Finish,
unit.group as BatchNo
FROM db.dbo.unit
JOIN db.dbo.order on order.entry_ID = unit.entry_ID
JOIN db.dbo.piece piece1 on piece1.ID_piece = unit.ID_piece_1
JOIN db.dbo.piece piece2 on piece2.ID_piece = unit.ID_piece_3
JOIN db.dbo.items i1 on i1.ID_unit = unit.ID_unit
JOIN db.dbo.items i2 on i2.ID_unit = unit.ID_unit
JOIN db.dbo.items i3 on i3.ID_unit = unit.ID_unit
JOIN db.dbo.items i4 on i4.ID_unit = unit.ID_unit
JOIN db.dbo.process p1 on p1.ID_process = i1.ID_process
JOIN db.dbo.process p2 on p2.ID_process = i2.ID_process
JOIN db.dbo.process p3 on p3.ID_process = i3.ID_process
JOIN db.dbo.process p4 on p4.ID_process = i4.ID_process
WHERE p1.note like '12A%'
and p1.ID_pieceorder = '1'
and p1.ID_job = '150'
and p2.ID_pieceorder = '3'
and p2.ID_job = '150'
and i1.count = i2.count
and i1.count = i3.count
and i1.count = i4.count
and i1.rack = i2.rack
and p1.note = p2.note
and i1.status = '1'
and i2.status = '1'
and p3.ID_pieceorder = '0'
and p4.ID_pieceorder = '2'
and p3.ID_job = '153'
and p4.ID_job = '151'
and i3.status = '0'
and i4.status = '0'
and order.status <> '4'
ORDER BY OrderNo
此查询正常。我查询的第二组数据添加了以下信息
SELECT ...(same as above)
FROM ...(same as above plus the following)
JOIN db.dbo.items i5 on i5.ID_unit = unit.ID_unit
JOIN db.dbo.items i6 on i6.ID_unit = unit.ID_unit
JOIN db.dbo.process p5 on p5.ID_process = i5.ID_process
JOIN db.dbo.process p6 on p6.ID_process = i6.ID_process
WHERE p1.note like '12B%'
and ... (same as above plus the following)...
and p5.ID_pieceorder = '1'
and p5.ID_job = '152'
and p6.ID_pieceorder = '3'
and p6.ID_job = '152'
and i5.status = '1'
and i6.status = '1'
and i1.count = i5.count
and i1.count = i6.count
当我尝试进行合并查询时,i5
,i6
,p5
和p6
的表格连接会为{{p1.note like '12A%'
生成大量重复的结果1}}因为它不需要字段。有没有一种方法可以在WHERE
语句中启动连接,以便在p1.note like '12B%'
时只使用这两个表?
SELECT ....
FROM ....
WHERE (p1.note like '12A%'
or
(p1.note like '12B%'
and p5.ID_pieceorder = '1'
and p5.ID_job = '152'
and p6.ID_pieceorder = '3'
and p6.ID_job = '152'
and i5.status = '1'
and i6.status = '1'
and i1.count = i5.count
and i1.count = i6.count
(JOIN db.dbo.items i5 on i5.ID_unit = unit.ID_unit
JOIN db.dbo.items i6 on i6.ID_unit = unit.ID_unit
JOIN db.dbo.process p5 on p5.ID_process = i5.ID_process
JOIN db.dbo.process p6 on p6.ID_process = i6.ID_process)))
我知道上面的语法不起作用,但我正在寻找类似的方法。
**编辑DRapp请求的表结构 我将用ID_unit 782327
填写结果-db.dbo.items-
-ID_item- -ID_process- -count- -status- -rack- -ID_unit-
628335 782328 1 0 25 782327
628336 782330 1 1 25 782327
628337 782331 1 1 25 782327
628338 782333 1 0 25 782327
628339 782335 1 1 25 782327
628340 782336 1 1 25 782327
628341 782337 1 0 25 782327
-db.dbo.process-
-ID_process- -ID_unit- -ID_pieceorder- -ID_job- -sequence-
782328 782327 0 50 1
782329 782327 1 5305 1
782330 782327 1 150 1
782331 782327 1 152 2
782332 782327 2 5408 2
782333 782327 2 151 1
782334 782327 3 5308 3
782335 782327 3 150 1
782336 782327 3 152 2
782337 782327 0 153 4
-db.dbo.unit-
-ID_unit- -status- -ID_piece_1- -ID_piece_2--ID_piece_3--ID_product-count
782327 2 5305 5408 5308 50 1
db.dbo.items包含每个&#34;项目的唯一键&#34;进入一个部分,它包含相应的过程代码,&#34;计数&#34;领域,&#34;状态&#34;每个&#34;项目&#34;,每个&#34;项目&#34;的机架,以及每个&#34;项目&#34;的单位ID。
db.dbo.process包含每个&#34;进程&#34;,ID_unit,ID_pieceorder
,ID_job
和sequence
的唯一键。每个单元都以ID_pieceorder
0开头,正在建立产品类型,然后ID_piece
1为第一件,等等。
db.dbo.unit包含每个&#34; unit&#34;的唯一键,其所有部分的代码,单元的状态以及count
,即单位数。< / p>
db.dbo.piece is a table with the master list of codes on it.
-ID_Piece- -Desc-
5305 14black
5408 14blue
150 Cut
需要6个过程的要点如下 p1&amp; i1:确保Piece是第1部分,是过程150,并且过程150完成
p2&amp; i2:确保该片是片2,是过程150,并且该过程150完成
p3&amp; i3:确保完成过程153未完成。
p4&amp; i4:确保连接器进程152没有完成(没有连接这两个部分)。
p5&amp; i5:有些部分需要完成额外的工作,这会检查并确保在第1部分完成了152的过程。
P6&amp; i6:与p5和amp;相同i5但是对于第2部分。
答案 0 :(得分:1)
如果您不想要重复项,请在它们之间使用Union,将order by子句移动到第二个的底部。确保在两个语句中选择列的顺序相同。
select (your columns)
From (tables and joins)
where (your where)
UNION
select (your columns)
From (tables and joins)
where (your where)
order by
答案 1 :(得分:1)
根据聊天室的说明,我已经为您提出以下内容,并根据需要进行最低限度的调整。
SELECT
u.id_unit,
u.count as TotalUnits,
u.group as BatchNo,
p1.ID_PieceOrder as Piece1Order,
p1Cut.ID_Process as Piece1CutProcessID,
i1Cut.Count as Item1CutCount,
i1Cut.Status as Item1CutStatus,
p1Tamp.ID_PieceOrder as Piece1OrderTampered,
p1Tamp.ID_Process as Piece1TamperedProcessID,
i1Tamp.Count as Item1TamperedCount,
i1Tamp.Status as Item1TamperedStatus,
p2.ID_PieceOrder as Piece2Order,
p2Cut.ID_Process as Piece2CutProcessID,
i2Cut.Count as Item2CutCount,
i2Cut.Status as Item2CutStatus,
p2Tamp.ID_PieceOrder as Piece2OrderTampered,
p2Tamp.ID_Process as Piece2TamperedProcessID,
i2Tamp.Count as Item2TamperedCount,
i2Tamp.Status as Item2TamperedStatus,
pSpacer.ID_PieceOrder as SpacerPieceOrder,
iSpacer.Count as SpacerCount,
iSpacer.Status as SpacerStatus,
pSealed.ID_PieceOrder as SealedPieceOrder,
iSealed.Count as SealedCount,
iSealed.Status as SealedStatus
FROM
db.dbo.unit u
JOIN Process p1
ON u.ID_Unit = p1.ID_Unit
AND u.ID_Piece_1 = p1.ID_Job
JOIN Process p1Cut -- ALL PIECES MUST BE CUT first
ON p1.ID_Unit = p1Cut.ID_Unit
AND p1.ID_PieceOrder = p1Cut.ID_PieceOrder
AND p1Cut.ID_Job = 150
JOIN Items i1Cut
ON p1Cut.id_process = i1Cut.id_process
LEFT JOIN Process p1Tamp -- NOT ALL PIECES MUST BE TEMPERED
ON p1Cut.ID_Unit = p1Tamp.ID_Unit
AND p1Cut.ID_PieceOrder = p1Tamp.ID_PieceOrder
AND p1Tamp.ID_Job = 152
LEFT JOIN Items i1Tamp
ON p1Tamp.id_process = i1Tamp.id_process
JOIN Process p2
ON u.ID_Unit = p2.ID_Unit
AND u.ID_Piece_3 = p2.ID_Job
JOIN Process p2Cut -- ALL PIECES MUST BE CUT first
ON p2.ID_Unit = p2Cut.ID_Unit
AND p2.ID_PieceOrder = p2Cut.ID_PieceOrder
AND p2Cut.ID_Job = 150
JOIN Items i2Cut
ON p2Cut.id_process = i2Cut.id_process
LEFT JOIN Process p2Tamp -- NOT ALL PIECES MUST BE TEMPERED
ON p2Cut.ID_Unit = p2Tamp.ID_Unit
AND p2Cut.ID_PieceOrder = p2Tamp.ID_PieceOrder
AND p2Tamp.ID_Job = 152
LEFT JOIN Items i2Tamp
ON p2Tamp.id_process = i2Tamp.id_process
LEFT JOIN Process PSpacer
ON u.ID_Unit = PSpacer.ID_Unit
AND u.ID_Job = 151
LEFT JOIN Items ISpacer
ON PSpacer.ID_Process = ISpacer.ID_Process
LEFT JOIN Process PSealed
ON u.ID_Unit = PSealed.ID_Unit
AND u.ID_Job = 153
LEFT JOIN Items ISealed
ON PSealed.ID_Process = ISealed.ID_Process
WHERE
u.ID_Unit IN (782327, 782328, 782329 )
- 只是3个单元ID的样本,用于测试修订后的查询结构的概念
我从单位表开始。从那以后,我将每件作品沿着各自的路径走下去...首先得到这件作品,然后是作品,它总是需要它的CUT。从CUT到其永远存在的ITEM状态记录。从CUT开始,我正在寻找相同订单的TAMPERED状态。由于并非所有玻璃都需要被篡改,我将其作为LEFT-JOIN。然后我按进程ID左键加入到被篡改的项目记录中。
我为第二部分做同样的事。
然后,LEFT-JOIN看看是否需要间距(如果需要间隔,可以更改为JOIN)
最后,工作中的LEFT-JOIN已完成录入。同样,不知道是否所有条目都预先填写了进程的所有阶段而不是。
注意我的别名来自P1,用于第1段的过程,然后是p1Cut到i1Cut,到p1Tamp到i1Tamp。所以现在,我没有通用的p1-p6,而是拥有流程或项目的上下文。此外,我只是抓住了相应部件中的所有柱子,切割,篡改,间隔和密封的步骤,清晰的列名称。如果你不想要什么东西你可以随时删除它,但是有了这个,你应该永远不会在每个UNIT上得到任何重复。
我没有分别将连接添加到订单或部分表中。我不知道是否真的需要piece,除非你有其他列,比如P1.Name,但你的表结构转储显示了描述。
由于我没有添加任何其他限定符,因此您应该在一行中获得单个UNIT的完整状态。我会调整WHERE子句来选择一些可以测试的单位范围并确认上下文。然后,调整where子句以查找其他条件,例如项目计数=另一个或不是,或者在'%a%'上下文中查找。但是,我认为这会让你顺利。
答案 2 :(得分:0)
像这样的东西
LEFT JOIN db.dbo.process p1
on p1.ID_process = i1.ID_process
and p1.note like '12A%'
and p1.ID_pieceorder = '1'
and p1.ID_job = '150'
and p2.ID_pieceorder = '3'
and p2.ID_job = '150'
and i1.count = i2.count
and i1.count = i3.count
and i1.count = i4.count
and i1.rack = i2.rack
and p1.note = p2.note
and i1.status = '1'
and i2.status = '1'
and p3.ID_pieceorder = '0'
and p4.ID_pieceorder = '2'
and p3.ID_job = '153'
and p4.ID_job = '151'
LEFT JOIN db.dbo.items i5
on i5.ID_unit = unit.ID_unit
and p1.note like '12B%'
and i5.status = '1'