我在SQL中有两个表。
表A
WO_Number Item_Number QtyReq
J000001 T12345ZA 5
J000002 T54321ZA 4
J000003 T98765ZA 2
表B
WO_Number WC_Code Qty
J000001 a 5
J000001 b 4
J000001 c 6
J000002 a 3
J000002 b 1
J000002 c 4
J000003 a 7
J000003 b 6
J000003 c 9
我正在创建一个包含这两个表的视图,我需要实现的是从表B中选择数量,其中TableB.WC_Code等于'a'。
我在WO_Number上加入了表A和表B.
我看到过类似的问题,但不确定要求是否完全一致,我不完全理解语法。
结果视图应如下所示:
WO_Number Item_Number QtyReq Qtya
J000001 T12345ZA 5 5
J000002 T54321ZA 4 3
J000003 T98765ZA 2 7
上表简化了。以下是实际查询:
SELECT RIGHT(dbo.tJCHeader.JCno, LEN(dbo.tJCHeader.JCno) - 1) AS 'WO NUMBER', dbo.tJCHeader.DATEfinish AS 'Date of Closure', dbo.tSalesOrderLink.SOno AS 'Sales Order',
dbo.tJCHeader.FGCode AS 'Item Number', dbo.tBOMh.UDtxt1 AS 'Film Grade', dbo.tJCHeader.QTYstd AS 'Qty To produce', dbo.tJCHeader.QTYact AS 'Good Qty Done', '' AS 'Error Qty Done',
(SELECT RIGHT(RTRIM(dbo.tJCHeader.BOMCode), 6) AS Expr1) AS Machine, dbo.tBOMh.UDval1 AS 'Roll Width', dbo.tBOMh.UDval2 AS 'Roll Length', '' AS 'sq metres', '' AS 'Core Cut time',
'' AS 'Core Cut quantity', '' AS 'Core Nothch time', '' AS 'Core Notch quantity', '0.317' AS 'Th Setup time', '' AS 'Th Slit time', '' AS 'Th Packing time', '' AS 'Rolls Scrapped quantity',
'' AS 'Rolls Scrapped sqm', '' AS 'Overruns quantity', '' AS 'Overuns sqm', dbo.tSalesOrderLink.CustomerDesc AS 'Client', '' AS 'Operator', dbo.tJCLines.Qty1, dbo.tJCLines.WCCode
FROM dbo.tBOMh INNER JOIN
dbo.tJCHeader ON dbo.tBOMh.BomCode = dbo.tJCHeader.BOMCode INNER JOIN
dbo.tJCLines ON dbo.tJCHeader.Indx = dbo.tJCLines.Indx LEFT OUTER JOIN
dbo.tSalesOrderLink ON dbo.tJCHeader.JCno = dbo.tSalesOrderLink.JCno
我需要添加到视图中的字段是dbo.tJCLines.Qty1,但仅限于dbo.tJCLines.WCCode ='SLIT_DEC120'或'SLIT_DEC056'。
答案 0 :(得分:0)
问题并不是很清楚,但你可能正在寻找这个问题:
SELECT *
FROM TableA AS a
INNER JOIN TableB AS b ON a.WO_Number=b.WO_Number
WHERE b.WC_Code='a'
根据你的评论,我认为你需要这个:
SELECT *
FROM TableA AS a
INNER JOIN TableB AS b ON a.WO_Number=b.WO_Number
AND b.WC_Code='a'
答案 1 :(得分:0)
您可以像这样添加逻辑到连接:
SELECT * FROM TableA AS A
INNER JOIN TableB AS B ON A.WO_Number = B.WO_Number AND B.WC_Code = 'a'
答案 2 :(得分:0)
请尝试按照SQL Select语句
select
TableA.WO_Number,
TableA.Item_Number,
TableA.QtyReq,
TableB.Qty
from TableA
left join TableB
on TableA.WO_Number = TableB.WO_Number
and TableB.WC_Code = 'a'
答案 3 :(得分:0)
因为您只需要来自TableB的数量
select Qty
from TableB
inner join TableA
where TableA.WO_Number = TableB.WO_Number AND TableB.WC_Code = 'a'
答案 4 :(得分:0)
希望这对你有用
SELECT
Qty
FROM
TableB
INNER JOIN TableA ON TableB.WO_Code = TableA.WO_Code
WHERE
TableB.WC_Code = 'a'