MS Access WHY 到 SQL CASE 语句

MS Access SUMIIF to SQL CASE statement

我正在尝试将 MS Access 查询转换为 SQL,我正在努力寻找执行此操作的正确方法。

The MS Access line is: Records Loaded: Sum(IIf([cmp_CampaignTable].>[ContactID]>0,1,0))

我目前的 SQL 代码是:

SELECT        Project.ProjectName, 
                cmp_UKWarranty.ProjectID, 
                SUM(CONVERT(numeric,(CASE WHEN (cmp_UKWarranty.ContactID) >0,1,0) AS RecordsLoaded
FROM            cmp_UKWarranty INNER JOIN
                         Project ON cmp_UKWarranty.ProjectID = Project.ProjectID INNER JOIN
                         Dial ON cmp_UKWarranty.DialID = Dial.DialID CROSS JOIN
                         CRC 

我没有其他人要问了,有人能给我指明正确的方向吗?

亚当

你快到了。您需要 thenelse。此外,不需要转换为 "numeric":

SELECT p.ProjectName, w.ProjectID,
       SUM(CASE WHEN w.ContactID > 0 THEN 1 ELSE 0) AS RecordsLoaded
FROM cmp_UKWarranty w INNER JOIN
     Project p
     ON w.ProjectID = p.ProjectID INNER JOIN
     Dial d
     ON w.DialID = Dial.d CROSS JOIN
     CRC

你的语法错误情况:

SELECT Project.ProjectName,
       cmp_UKWarranty.ProjectID,
       Sum(CASE
             WHEN ( cmp_UKWarranty.ContactID > 0 ) THEN 1
             ELSE 0
           END)
FROM   cmp_UKWarranty
       INNER JOIN Project
               ON cmp_UKWarranty.ProjectID = Project.ProjectID
       INNER JOIN Dial
               ON cmp_UKWarranty.DialID = Dial.DialID
       CROSS JOIN CRC