通过运行下面的整个 T-SQL 代码,我似乎在下面收到以下错误。
故障排除步骤:
- 在每个“UNION”之前运行每个代码块(每个块执行良好并返回结果)
- 检查了每个块的所有语法,没有发现下划线语法错误
- 执行包含“PartTran”的部分的最后三个代码块,并且三个块正确执行。
- 执行前两个代码块并提供错误
我对这个错误感到很困惑,因为如果我使用第一段代码并且我在选择列表中的一行并按特定列的列表分组,所有代码都会运行。据我所知,我为每个代码块选择并分组了相同的列,但是当一起运行所有块时,我收到错误。任何帮助,将不胜感激!
执行错误:
消息 205,级别 16,状态 1,第 2 行所有使用 UNION、INTERSECT 或 EXCEPT 运算符组合的查询在其目标列表中必须具有相同数量的表达式。
SELECT
OH.OrderStatus
,OH.ShipByTime
,CS.PriceTolerance
,OH.OrderNum
,OH.ExtCompany
,CS.[Name]
,OH.TotalInvoiced - OH.TotalDiscount AS 'net_sales_order'
,OH.CustNum
,OH.ShipToNum
,OH.ShipToCustNum
,SUM(CAST(OD.OrderQty AS int)) AS QtySum
,COUNT(OD.OrderLine) AS LineCnt
FROM erp.customer AS CS WITH (NOLOCK)
INNER JOIN erp.orderhed AS OH WITH (NOLOCK)
ON CS.custnum = OH.custnum
LEFT JOIN erp.OrderDtl as OD
ON OH.OrderNum = OD.OrderNum
AND OH.ExtCompany = OD.ExtCompany
WHERE (OH.VoidOrder <> '1') AND OH.OrderStatus BETWEEN 'N' AND 'Q' --order status is currently empty in E10LiveDB on RASE10PROD
GROUP BY
OH.OrderStatus
,OH.ShipByTime
,CS.PriceTolerance
,OH.OrderNum
,OH.ExtCompany
,CS.[Name]
,OH.TotalInvoiced - OH.TotalDiscount
,OH.CustNum
,OH.ShipToNum
,OH.ShipToCustNum
UNION
SELECT --PT.STATUS
PT.TranDate
,'CONSIGN' AS Expr1
,PT.PartNum
,0 AS 'ext'
,'KRAFTMAID' + ' - ' + PT.LotNum AS Expr2
,SUM(CONVERT(DECIMAL(10, 2), PT.Acttranqty * PT.MtlUnitCost)) AS Expr3
,PT.LotNum
,0 AS Expr4
,'KRAFTMAID' AS Expr5
,SUM(CAST(PT.Acttranqty AS int)) AS QtySum
,COUNT(PT.OrderLine) AS LineCnt
FROM erp.PartTran AS PT WITH (NOLOCK)
INNER JOIN erp.PartWhse AS PW WITH (NOLOCK)
ON PT.PartNum = PW.PartNum
INNER JOIN erp.PartLot AS PL WITH (NOLOCK)
ON PT.PartNum = PL.PartNum
AND PL.FirstRefDate < GETDATE()
AND PL.ExpirationDate > GETDATE()
LEFT JOIN erp.Customer AS C WITH (NOLOCK)
ON PT.CustNum = C.CustNum
AND C.CustID = '43360'
WHERE (
PT.LotNum IN (
'KM'
,'KMUT'
)
)
-- AND (X.STATUS < 'S')
-- AND XL.shipped = 0
GROUP BY --X.STATUS,
PT.TranDate
,PT.PartNum
,PT.LotNum
UNION
SELECT -- X.STATUS
PT.TranDate
,'CONSIGN' AS Expr1
,PT.PartNum
,0 AS 'ext'
,'AMERICAN WOODMARK' + ' - ' + PT.LotNum AS Expr2
,SUM(CONVERT(DECIMAL(10, 2), PT.Acttranqty * PT.MtlUnitCost)) AS Expr3
,PT.LotNum
,0 AS Expr4
,'AMERICAN WOODMARK' AS Expr5
,SUM(CAST( PT.Acttranqty AS int)) AS QtySum
,COUNT(PT.OrderLine) AS LineCnt
FROM erp.PartTran AS PT WITH (NOLOCK)
INNER JOIN erp.PartWhse AS PW WITH (NOLOCK)
ON PT.PartNum = PW.PartNum
INNER JOIN erp.PartLot AS PL WITH (NOLOCK)
ON PT.PartNum = PL.PartNum
AND PL.FirstRefDate < GETDATE()
AND PL.ExpirationDate > GETDATE()
LEFT JOIN erp.Customer AS C WITH (NOLOCK)
ON PT.CustNum = C.CustNum
AND C.CustID = '43360'
WHERE (PT.LotNum = 'AWNC')
-- AND (X.STATUS < 'S')
-- AND XL.shipped = 0
GROUP BY --X.STATUS,
PT.TranDate
,PT.PartNum
,PT.LotNum
UNION
SELECT -- X.STATUS
PT.TranDate
,'CONSIGN' AS Expr1
,PT.PartNum
,0 AS 'ext'
,'AMERICAN WOODMARK' + ' - ' + PT.LotNum AS Expr2
,SUM(CONVERT(DECIMAL(10, 2), PT.Acttranqty * PT.MtlUnitCost)) AS Expr3
,PT.LotNum
,0 AS Expr4
,'AMERICAN WOODMARK' AS Expr5
,SUM(CAST(PT.Acttranqty AS int)) AS QtySum
,COUNT(PT.OrderLine) AS LineCnt
FROM erp.PartTran AS PT WITH (NOLOCK)
INNER JOIN erp.PartWhse AS PW WITH (NOLOCK)
ON PT.PartNum = PW.PartNum
INNER JOIN erp.PartLot AS PL WITH (NOLOCK)
ON PT.PartNum = PL.PartNum
AND PL.FirstRefDate < GETDATE()
AND PL.ExpirationDate > GETDATE()
LEFT JOIN erp.Customer AS C WITH (NOLOCK)
ON PT.CustNum = C.CustNum
AND C.CustID = '11088'
WHERE (PT.LotNum = 'AWAZ')
-- AND (X.STATUS < 'S')
--AND XL.shipped = 0
GROUP BY -- X.STATUS,
PT.TranDate
,PT.PartNum
,PT.LotNum
UNION
SELECT -- X.STATUS
PT.TranDate
,'CONSIGN'
,PT.PartNum
,0 AS 'ext'
,'STAR MARK' + ' - ' + PT.LotNum
,SUM(CONVERT(DECIMAL(10, 2), PT.Acttranqty * PT.MtlUnitCost))
,PT.LotNum
,0
,'AMERICAN WOODMARK'
,SUM(CAST(PT.Acttranqty AS int)) AS QtySum
,COUNT(PT.OrderLine) AS LineCnt
FROM erp.PartTran AS PT WITH (NOLOCK)
INNER JOIN erp.PartWhse AS PW WITH (NOLOCK)
ON PT.PartNum = PW.PartNum
INNER JOIN erp.PartLot AS PL WITH (NOLOCK)
ON PT.PartNum = PL.PartNum
AND PL.FirstRefDate < GETDATE()
AND PL.ExpirationDate > GETDATE()
LEFT JOIN erp.Customer AS C WITH (NOLOCK)
ON PT.CustNum = C.CustNum
AND C.CustID IN
(
'49023'
,'11444'
)
WHERE PT.LotNum = 'SM'
-- AND X.STATUS < 'S'
-- AND XL.shipped = 0
GROUP BY --X.STATUS,
PT.TranDate
,PT.PartNum
,PT.LotNum
GO
根据在线书籍:
您的第一个 select 语句中有 12 列,而其他语句有 11 列。
OH.OrderStatus
从第一个 select 语句中注释掉(或删除) 。