我有一个非常复杂的SQL服务器数据库,我试图在这里简化。我有5个表,我想从中提取信息。我想要Table_A的所有记录。其他表可能有也可能没有记录。当他们这样做时,我希望将这些信息添加到我的决赛桌。我无法获得查询以返回正确的值。我尝试了不同的连接等,但保持相同的值。
表-A :
LotID MaterialNumber RejectReason
1234 987 16
2345 876
3456 987
4567 876
5678 987
6789 987 15
7890 987
8901 987
表-B :
LotID Color
1234 0.25
5678 0.28
6789 0.27
8901 0.3
Table_C :
LotID Gloss
1234 1.25
2345 1.28
6789 1.27
8901 1.30
Table_D :
LotID Thickness
2345 2.25
5678 2.28
6789 2.27
7890 2.3
Table_E :
NR Description
12 Tear
13 Run
14 Drip
15 Wet
16 Dry
Table_F :
Material_Number Part_Descrip
987 Front
876 Rear
查询
Select
Table_A.LotID, Table_A.MaterialNumber,
Table_B.Color,
Table_C.Gloss,
Table_D.Thickness
Table_E.Description,
Table_F.Part_Descrip,
FROM
Table_A A
LEFT OUTER JOIN
Table_B B ON A.LotID = B.LotID
LEFT OUTER JOIN
Table_C C ON A.LotID = C.LotID
LEFT OUTER JOIN
Table_D D ON A.LotID = D.LotID
LEFT OUTER JOIN
Table_E E ON A.RejectReason = E.NR
LEFT OUTER JOIN
Table_F F ON A.MaterialNumber = F.Material_Number
ORDER BY
A.LotID
返回:
LotID Material Number Color Gloss Thickness Reject Part Description
6789 987 0.27 1.28 2.27 Wet Front
希望它返回:
LotID Material Number Color Gloss Thickness Reject Part Description
1234 987 0.25 1.25 Null Dry Front
2345 876 Null 1.28 2.25 Null Rear
3456 987 Null Null Null Null Front
4567 876 Null Null Null Null Rear
5678 987 0.28 1.28 2.28 Null Front
6789 987 0.27 1.28 2.27 Wet Front
7890 987 Null Null 2.3 Null Front
8901 987 0.3 1.3 Null Null Front
原始查询
SELECT
Z_LINE5_QU.CollectTime,
Z_LINE5_QU.LotID,
Z_LINE5_QU.MOULD_ID,
Z_LINE5_QU.CAV,
Z_LINE5_QU.MOULD_CAV,
Z_LINE5_QU.OK,
Z_LINE5_QU.NOK,
Z_LINE5_QU.OKAFTERREWORK,
Z_LINE5_QU.NOKAFTERREWORK,
Z_LINE5_QU.SENDTOREWORK,
Z_LINE5_QU.SPOTCH_OK,
Z_LINE5_QU.SPOTCH_NOK,
Z_LINE5_QU.MOULD_CY,
Z_LINE5_QU.LINE,
Z_LINE5_QU.LINE_ID,
Z_LINE5_QU.REASON_NR1,
Z_CODE_QU_REASON.DESCR,
Z_CODE_PART.PART_DESCR,
Z_LINE5_SPOTCH_GLOSS.GLOSS_MP1,
Z_LINE5_SPOTCH_GLOSS.GLOSS_MP2,
Z_LINE5_SPOTCH_THICK.THICK_Z1,
Z_LINE5_SPOTCH_THICK.THICK_Z2,
Z_LINE5_SPOTCH_THICK.THICK_Z3,
Z_LINE5_SPOTCH_THICK.THICK_Z4,
Z_LINE5_SPOTCH_THICK.THICK_Z5,
Z_LINE5_SPOTCH_THICK.THICK_Z6,
Z_LINE5_SPOTCH_THICK.THICK_Z7,
Z_LINE5_SPOTCH_THICK.THICK_Z8,
Z_LINE5_SPOTCH_THICK.THICK_Z9
FROM RTUSPRAY.DBO.Z_LINE5_QU Z_LINE5_QU
LEFT OUTER JOIN RTUSPRAY.DBO.Z_LINE5_SPOTCH_COLOR
ON Z_LINE5_QU.LOTID = Z_LINE5_SPOTCH_COLOR.LOTID
LEFT OUTER JOIN RTUSPRAY.DBO.Z_LINE5_SPOTCH_THICK
ON Z_LINE5_QU.LOTID=Z_LINE5_SPOTCH_THICK.LOTID
LEFT OUTER JOIN RTUSPRAY.DBO.Z_LINE5_SPOTCH_GLOSS
ON Z_LINE5_QU.LOTID=Z_LINE5_SPOTCH_GLOSS.LOTID
LEFT OUTER JOIN RTUSPRAY.DBO.Z_CODE_QU_REASON
ON Z_LINE5_QU.REASON_NR1=Z_CODE_QU_REASON.NR
LEFT OUTER JOIN RTUSPRAY.DBO.Z_CODE_PART
ON Z_LINE5_QU.CODE_SAP = Z_CODE_PART.CODE_SAP_PRESER
ORDER BY Z_LINE5_QU.LotID
答案 0 :(得分:0)
我刚刚在我的数据库中重新创建它并且它工作正常。唯一的区别是我使用了选择列表中的表别名而不是原始问题中显示的实际表名:
Select
A.LotID,
A.MaterialNumber,
B.Color,
C.Gloss,
D.Thickness,
E.Description,
F.Part_Descrip
FROM
dbo.Table_A as A
LEFT OUTER JOIN
Table_B as B ON A.LotID = B.LotID
LEFT OUTER JOIN
Table_C as C ON A.LotID = C.LotID
LEFT OUTER JOIN
Table_D as D ON A.LotID = D.LotID
LEFT OUTER JOIN
Table_E as E ON A.RejectReason = E.NR
LEFT OUTER JOIN
Table_F as F ON A.MaterialNumber = F.Material_Number
ORDER BY
A.LotID
以下是我的结果:
LotID MaterialNumber Color Gloss Thickness Description Part_Descrip
1234 987 0.000 1.25 NULL Dry Front
2345 876 NULL 1.28 2.25 NULL Rear
3456 987 NULL NULL NULL NULL Front
4567 876 NULL NULL NULL NULL Rear
5678 987 0.280 NULL 2.28 NULL Front
6789 987 0.270 1.27 2.27 Wet Front
7890 987 NULL NULL 2.3 NULL Front
8901 987 0.300 1.3 NULL NULL Front
答案 1 :(得分:-2)
好像你已经通过LotID加入了每个表。看起来他们共享的唯一LotID是6789,结果反映了这一点。我认为你想要做的是,因为它们都共享相同的唯一标识符,实际上只创建一个从A到D的表,每个表有两列以上。假设表E列NR对应于表A的第三列,那么您就想要一个连接。唯一的其他连接应该是表A,表F列MaterialNumber
。这样你只需要两个连接。否则我相信你实际上需要一个组合(记住排列吗?)你在问题中的五个连接。