IIF到案例翻译

时间:2013-05-21 16:55:22

标签: sql ms-access

好的,所以我需要将最初在访问数据库上使用的查询转换为将在SQL-Compact Edition数据库上运行的查询。

并且在该查询中我有一个IIF语句(在sql中不起作用)

我查找的所有内容都表明我应该用Case When替换它。

这是我的原始代码:

Select [OrderNumber], [CustomerName], [PO_TAG], [Shape], [Size], [DrawingNumber],
[OrderBuildTime], [RequiredDateTime], [LatestStatusTime], [ExpectedCompletionDateTime],
[DateImported],  Sum(IIF([Status] = 'Printed', PanelBuildTime, 0)) AS OrderBuildTimee from 
SteelOrders 
INNER JOIN FinalizedPrintedStickers 
ON 
SteelOrders.OrderNumber = LEFT(FinalizedPrintedStickers.SN,10)
GROUP BY SteelOrders.OrderNumber, [CustomerName], [PO_TAG], [Shape], [Size], [DrawingNumber], 
[OrderBuildTime], [RequiredDateTime], [LatestStatusTime], [ExpectedCompletionDateTime], 
[DateImported] 

这是IIF声明:

Sum(IIF([Status] = 'Printed', PanelBuildTime, 0)) AS OrderBuildTimee

这就是我用以下内容替换了IIF语句:

Sum(CASE Status WHEN 'Printed' THEN PanelBuildTime ELSE 0) AS OrderBuildTimee 

这就是整个代码的后缀:

Select [OrderNumber],
[CustomerName],
[PO_TAG],
[Shape],
[Size],
[DrawingNumber],
[OrderBuildTime],
[RequiredDateTime],
[LatestStatusTime],
[ExpectedCompletionDateTime],
[DateImported],
Sum(CASE Status WHEN 'Printed' THEN PanelBuildTime ELSE 0) AS OrderBuildTimee 
From SteelOrders 
INNER JOIN FinalizedPrintedStickers 
ON 
SteelOrders.OrderNumber = LEFT(FinalizedPrintedStickers.SN,10)
GROUP BY SteelOrders.OrderNumber, [CustomerName], [PO_TAG], [Shape], [Size], [DrawingNumber], 
[OrderBuildTime], [RequiredDateTime], [LatestStatusTime], [ExpectedCompletionDateTime], 
[DateImported] 

这是我的新错误消息:

Major Error 0x80040E14, Minor Error 25501
> Select [OrderNumber],
[CustomerName],
[PO_TAG],
[Shape],
[Size],
[DrawingNumber],
[OrderBuildTime],
[RequiredDateTime],
[LatestStatusTime],
[ExpectedCompletionDateTime],
[DateImported],
Sum(CASE Status WHEN 'Printed' THEN PanelBuildTime ELSE 0) AS OrderBuildTimee 
From SteelOrders 
INNER JOIN FinalizedPrintedStickers 
ON 
SteelOrders.OrderNumber = LEFT(FinalizedPrintedStickers.SN,10)
GROUP BY SteelOrders.OrderNumber, [CustomerName], [PO_TAG], [Shape], [Size], [DrawingNumber], 
[OrderBuildTime], [RequiredDateTime], [LatestStatusTime], [ExpectedCompletionDateTime], 
[DateImported]
There was an error parsing the query. [ Token line number = 12,Token line offset = 58,Token in 
error = ) ]

1 个答案:

答案 0 :(得分:3)

CASE的正确语法是:

Simple CASE expression: 
CASE input_expression 
     WHEN when_expression THEN result_expression [ ...n ] 
     [ ELSE else_result_expression ] 
END 

Searched CASE expression:
CASE
     WHEN Boolean_expression THEN result_expression [ ...n ] 
     [ ELSE else_result_expression ] 
END

在您的查询中,您错过了CASE的END

Sum(CASE Status WHEN 'Printed' THEN PanelBuildTime ELSE 0 END) AS OrderBuildTimee 

所以完整的代码是:

Select [OrderNumber],
  [CustomerName],
  [PO_TAG],
  [Shape],
  [Size],
  [DrawingNumber],
  [OrderBuildTime],
  [RequiredDateTime],
  [LatestStatusTime],
  [ExpectedCompletionDateTime],
  [DateImported],
  Sum(CASE Status WHEN 'Printed' THEN PanelBuildTime ELSE 0 END) AS OrderBuildTimee 
From SteelOrders 
INNER JOIN FinalizedPrintedStickers 
  -- left not supported by sql server ce use substring
  -- ON SteelOrders.OrderNumber = LEFT(FinalizedPrintedStickers.SN,10)
  ON SteelOrders.OrderNumber = substring(FinalizedPrintedStickers.SN, 1, 10)
GROUP BY SteelOrders.OrderNumber, [CustomerName], [PO_TAG], [Shape], [Size], [DrawingNumber], 
[OrderBuildTime], [RequiredDateTime], [LatestStatusTime], [ExpectedCompletionDateTime], 
[DateImported]