原始访问查询
IIf([Package Type]="Bar",10,IIf([upper limit unit]="g",([UL3]*[weight factor]),[piece wt (g)]*[Upper Limit Factor])) AS [Upper Limit (g)]
SQL转换
SELECT CASE WHEN [Package Type] = 'Bar' Then 10
CASE WHEN [Upper Limit Unit] = 'g' Then ([UL3)] * [Weight Factor]
ELSE [Peice Wt(g)] * [Upper Limit Factor] END AS [Upper Limit (g)]
我在关键字ELSE"附近遇到#34;语法错误,查询的各个部分工作得非常好。
如何在case
语句中引用多个case
语句?请帮助
SELECT
CASE WHEN [Package Type] = 'Bar' THEN 10
CASE WHEN [Upper Limit Unit] = 'g'
THEN CASE WHEN [Package Type] != NULL
THEN ([mav (g)] - [scale deviation factor] - [tare variation factor (g)]) / NULLIF ([weight factor], 0)
WHEN (mav.[MAV (g)] - pl.[Scale Deviation Factor] - nwmd.[Tare Variation Factor (g)])
/ NULLIF (nwmd.[Weight Factor], 0)
- CAST((mav.[MAV (g)] - pl.[Scale Deviation Factor] - nwmd.[Tare Variation Factor (g)])
/ NULLIF (nwmd.[Weight Factor], 0) AS Int)
= 0
THEN CAST((mav.[MAV (g)] - pl.[Scale Deviation Factor] - nwmd.[Tare Variation Factor (g)])
/ NULLIF (nwmd.[Weight Factor], 0) AS Int)
WHEN (mav.[MAV (g)] - pl.[Scale Deviation Factor] - nwmd.[Tare Variation Factor (g)])
/ NULLIF (nwmd.[Weight Factor], 0)
- CAST((mav.[MAV (g)] - pl.[Scale Deviation Factor] - nwmd.[Tare Variation Factor (g)])
/ NULLIF (nwmd.[Weight Factor], 0) AS Int) > 0
AND
(mav.[MAV (g)] - pl.[Scale Deviation Factor] - nwmd.[Tare Variation Factor (g)])
/ NULLIF (nwmd.[Weight Factor], 0)
- CAST((mav.[MAV (g)] - pl.[Scale Deviation Factor] - nwmd.[Tare Variation Factor (g)])
/ NULLIF (nwmd.[Weight Factor], 0) AS Int) < 0.51
THEN CAST((mav.[MAV (g)] - pl.[Scale Deviation Factor] - nwmd.[Tare Variation Factor (g)])
/ NULLIF (nwmd.[Weight Factor], 0) AS Int)
+ [Rounding Factor1] ELSE CAST((mav.[MAV (g)] - pl.[Scale Deviation Factor] - nwmd.[Tare Variation Factor (g)])
/ NULLIF (nwmd.[Weight Factor], 0) AS Int) + [Rounding Factor2] * NULLIF ( [Weight Factor],0)
ELSE CASE WHEN [Upper Limit Unit] != 'g'
THEN ([Label Wt (g)] / [Pcs Per Unit]) * [Upper Limit Factor] END AS [Upper Limit (g)]
FROM
dbo.[Net Weight Master Data] nwmd
LEFT OUTER JOIN
dbo.[Production Lines] pl ON nwmd.[Production Line] = pl.[Production Line]
INNER JOIN
dbo.[Maximum Allowable Variations] mav ON nwmd.[Label Wt (g)] = mav.[Labeled Quantity (g)]
LEFT OUTER JOIN
dbo.ItemDescription I ON nwmd.[Unit UPC Base Item] = I.[Unit UPC Base Item]
ORDER BY
nwmd.[Unit UPC Base Item],
nwmd.[Production Line]
答案 0 :(得分:1)
您有4
CASE
且只有END
,因此无法正常工作。
这是一个很大的混乱,但理想情况下你可以拆分通用标准来清理它,使用多个步骤比一个鸣喇叭嵌套CASE
查询更容易阅读。
至少你需要更多END
,缩进将有助于隔离错误:
SELECT
CASE WHEN [Package Type] = 'Bar'
THEN 10
ELSE CASE WHEN [Upper Limit Unit] = 'g'
THEN CASE WHEN [Package Type] IS NOT NULL
THEN ([mav (g)] - [scale deviation factor] - [tare variation factor (g)]) / NULLIF ([weight factor], 0)
WHEN (MAV.[MAV (g)] - PL.[Scale Deviation Factor] - NWMD.[Tare Variation Factor (g)])
/ NULLIF (NWMD.[Weight Factor], 0) - CAST((MAV.[MAV (g)] - PL.[Scale Deviation Factor]
- NWMD.[Tare Variation Factor (g)]) / NULLIF (NWMD.[Weight Factor], 0) AS Int) = 0
THEN CAST((MAV.[MAV (g)] - PL.[Scale Deviation Factor] - NWMD.[Tare Variation Factor (g)])
/ NULLIF (NWMD.[Weight Factor], 0) AS Int)
WHEN (MAV.[MAV (g)] - PL.[Scale Deviation Factor] - NWMD.[Tare Variation Factor (g)])
/ NULLIF (NWMD.[Weight Factor], 0) - CAST((MAV.[MAV (g)] - PL.[Scale Deviation Factor]
- NWMD.[Tare Variation Factor (g)]) / NULLIF (NWMD.[Weight Factor], 0) AS Int) > 0 AND
(MAV.[MAV (g)] - PL.[Scale Deviation Factor] - NWMD.[Tare Variation Factor (g)])
/ NULLIF (NWMD.[Weight Factor], 0) - CAST((MAV.[MAV (g)] - PL.[Scale Deviation Factor]
- NWMD.[Tare Variation Factor (g)]) / NULLIF (NWMD.[Weight Factor], 0) AS Int)
< 0.51
THEN CAST((MAV.[MAV (g)] - PL.[Scale Deviation Factor] - NWMD.[Tare Variation Factor (g)])
/ NULLIF (NWMD.[Weight Factor], 0) AS Int) + [Rounding Factor1]
ELSE CAST((MAV.[MAV (g)]
- PL.[Scale Deviation Factor] - NWMD.[Tare Variation Factor (g)])
/ NULLIF (NWMD.[Weight Factor], 0) AS Int) + [Rounding Factor2] * NULLIF ( [Weight Factor],0)
ELSE CASE WHEN [Upper Limit Unit] != 'g' --Double ELSE!
THEN ([Label Wt (g)] / [Pcs Per Unit]) * [Upper Limit Factor]
END
END
END
END AS [Upper Limit (g)]
FROM dbo.[Net Weight Master Data] NWMD LEFT OUTER JOIN
dbo.[Production Lines] PL ON NWMD.[Production Line] = PL.[Production Line] INNER JOIN
dbo.[Maximum Allowable Variations] MAV ON NWMD.[Label Wt (g)] = MAV.[Labeled Quantity (g)] LEFT OUTER JOIN
dbo.ItemDescription ID ON NWMD.[Unit UPC Base Item] = ID.[Unit UPC Base Item]
ORDER BY NWMD.[Unit UPC Base Item], NWMD.[Production Line]
更新:同样在一个ELSE
中发现2 CASE
,上面提到--Double ELSE!
,每ELSE
只能有一个CASE
。正如Aaron所指出的,你在那里也有一个!= NULL
,你需要IS NOT NULL
。另外添加了表别名以进一步清理。
答案 1 :(得分:0)
虽然我预先格式化了您的原始文件以查看case / when / then构造的对齐失败,但我已经更进一步通过创建新的简化列来对数据元素进行预先查询来简化可读性。 mav - scale - tare“和NULLIF(权重因子),再次作为单个”DivideTest“通过”mav - scale - tare“/ NULLIF(权重因子,0)来自原始数据。
这允许外部查询使用这些简化的列名作为案例构造的基础以提高可读性。
我仍然不知道你想要为各自的休息时间做些什么,但希望这个版本能让你更清楚自己要做什么。而且我知道我没有正确的ELSE / END组件,因为目前还不清楚你想要得到什么。最后,我希望您知道除以零是一个无效值,因此当您测试NULLIF([权重因子],0)可能会导致您的案例构造中出现问题。
SELECT
CASE WHEN PQ.pkgType = 'Bar'
THEN 10
ELSE CASE WHEN [Upper Limit Unit] = 'g'
THEN CASE WHEN PQ.pkgType != NULL
THEN PQ.DivideTest
WHEN PQ.DivideTest - CAST( PQ.DivideTest AS Int) = 0
THEN CAST( PQ.DivideTest AS Int) = 0
WHEN PQ.DivideTest - CAST( PQ.DivideTest AS Int) > 0
AND
PQ.DivideTest - CAST( PQ.DivideTest AS Int) < 0.51
THEN CAST( PQ.DivideTest AS Int) + PQ.roundFactor1
ELSE CAST( PQ.DivideTest AS Int) + PQ.roundFactor2 * PQ.WeightFactor
END
ELSE CASE WHEN [Upper Limit Unit] != 'g'
THEN PQ.notGWeight END AS [Upper Limit (g)]
END
from
( SELECT
[Package Type] pkgType,
[Upper Limit Unit] upperLimitUnit,
NULLIF (nwmd.[weight factor], 0) as weightFactor,
(mav.[mav (g)] - pl.[scale deviation factor] - nwmd.[tare variation factor (g)]) / NULLIF (nwmd.[weight factor], 0) as DivideTest,
[Rounding Factor1] roundFactor1,
[Rounding Factor2] roundFactor2,
([Label Wt (g)] / [Pcs Per Unit]) * [Upper Limit Factor] notGWeight
FROM
dbo.[Net Weight Master Data] nwmd
LEFT OUTER JOIN dbo.[Production Lines] pl
ON nwmd.[Production Line] = pl.[Production Line]
INNER JOIN dbo.[Maximum Allowable Variations] mav
ON nwmd.[Label Wt (g)] = mav.[Labeled Quantity (g)]
LEFT OUTER JOIN dbo.ItemDescription I
ON nwmd.[Unit UPC Base Item] = I.[Unit UPC Base Item]
ORDER BY
nwmd.[Unit UPC Base Item],
nwmd.[Production Line] ) PQ
答案 2 :(得分:0)
在使用了所有您感兴趣的帮助后,我仍然面临语法问题,我通过放置&#34; End&#34;在相应的位置并删除额外的&#34; Else&#34;在查询中,最终的查询是:
SELECT [wmd].[Unit UPC Base Item],
CASE WHEN [Package Type] = 'Bar' Then 10
WHEN [Upper Limit Unit] != 'g' THEN ([Label Wt (g)] / [Pcs Per Unit])* ([Upper Limit Factor])
WHEN [Upper Limit Unit] = 'g' THEN
CASE WHEN [Package Type] IS NOT NULL THEN ([mav (g)] - [scale deviation factor] - [tare variation factor (g)]) / NULLIF ([weight factor], 0)
WHEN (mav.[MAV (g)] - prod.[Scale Deviation Factor] - wmd.[Tare Variation Factor (g)])
/ NULLIF (wmd.[Weight Factor], 0)
- CAST((mav.[MAV (g)] - prod.[Scale Deviation Factor] - [wmd].[Tare Variation Factor (g)])
/ NULLIF (wmd.[Weight Factor], 0) AS Int)
= 0 THEN CAST((mav.[MAV (g)] - prod.[Scale Deviation Factor] - [wmd].[Tare Variation Factor (g)])
/ NULLIF (wmd.[Weight Factor], 0) AS Int)
WHEN (mav.[MAV (g)] - prod.[Scale Deviation Factor] - [wmd].[Tare Variation Factor (g)])
/ NULLIF (wmd.[Weight Factor], 0)
- CAST((mav.[MAV (g)] - prod.[Scale Deviation Factor] - [wmd].[Tare Variation Factor (g)])
/ NULLIF (wmd.[Weight Factor], 0) AS Int) > 0 AND
(mav.[MAV (g)] - prod.[Scale Deviation Factor] - [wmd].[Tare Variation Factor (g)])
/ NULLIF (wmd.[Weight Factor], 0)
- CAST((mav.[MAV (g)] - prod.[Scale Deviation Factor] - [wmd].[Tare Variation Factor (g)])
/ NULLIF (wmd.[Weight Factor], 0) AS Int)
< 0.51 THEN CAST((mav.[MAV (g)] - prod.[Scale Deviation Factor] - [wmd].[Tare Variation Factor (g)])
/ NULLIF (wmd.[Weight Factor], 0) AS Int)
+ [Rounding Factor1]
ELSE CAST((mav.[MAV (g)] - prod.[Scale Deviation Factor] - [wmd].[Tare Variation Factor (g)])
/ NULLIF (wmd.[Weight Factor], 0) AS Int) + [Rounding Factor2]
END *([Weight Factor])
END AS [UpperLimit(g)]
FROM dbo.[Net Weight Master Data] AS wmd LEFT OUTER JOIN
dbo.[Production Lines] AS prod ON wmd.[Production Line] = prod.[Production Line] INNER JOIN
dbo.[Maximum Allowable Variations] AS mav ON wmd.[Label Wt (g)] = mav.[Labeled Quantity (g)] LEFT OUTER JOIN
dbo.ItemDescription ON wmd.[Unit UPC Base Item] = dbo.ItemDescription.[Unit UPC Base Item]
ORDER BY wmd.[Unit UPC Base Item], wmd.[Production Line]