-1

我有 2 张桌子:

AppDetailFee table has AppID (PK), TypeID, Amount
AppDetail table has AppID (PK)   

我在 AppID 上加入了两个表,我需要检查以下内容的 TypeID 和金额。有3种情况:

If TypeID = 6 and Amount = 0 then  print appid and amount zero
If typeID = 6 and amount <> 0  - bypass and do not print
When TypeID <> 6 then print zero

下面是我使用的代码,但是我得到了所有不等于零且数量为零的行的结果。

任何帮助,将不胜感激。谢谢

SELECT  ad.AppID
      , MAX(case when (adf.TypeID = 6 AND adf.Amount = 0) then 0 
                 when (adf.TypeID = 6 AND adf.Amount <> 0) then Adf.Amount
       ELSE 0 END) AS FeeAmount

FROM         
      AppDetail AS ad   

inner join  AppDetailFee adf ON
      ad.AppID = adf.AppID 

WHERE  adf.Amount =0
4

1 回答 1

3

您有一个WHERE条款正在删除不符合条件的所有内容:

WHERE  adf.Amount =0

尝试删除它并运行您的查询:

SELECT  ad.AppID
      , MAX(case when (adf.TypeID = 6 AND adf.Amount = 0) then 0 
                 when (adf.TypeID = 6 AND adf.Amount <> 0) then Adf.Amount
                 else 0 END) AS FeeAmount
FROM  AppDetail AS ad   
inner join  AppDetailFee adf ON
      ad.AppID = adf.AppID 
group by ad.AppID

您还缺少GROUP BY查询中的子句。

于 2013-01-16T21:02:26.843 回答