1

好的,所以我需要将最初在 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 = ) ]
4

1 回答 1

3

a 的正确语法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

在您的查询中,您缺少ENDCASE 的:

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] 
于 2013-05-21T16:58:21.243 回答