麻烦的SQL CASE查询

时间:2018-02-23 04:44:39

标签: sql switch-statement

有点卡在下面,任何人都可以确定我出错的地方吗?我认为这是一个简单的案例表达问题:

CASE when (ISNULL([6WeekSales].SalesQTY / 6 , 0)) >= (ISNULL([12WeekSales].SalesQTY / 12 , 0))
then
((dbo.ItemVendorDetails.LeadTime / 7 * CASE ISNULL([6WeekSales].SalesQTY / 6 , 0) - (ISNULL(ItemsStock.StockOnHand , 0) - ISNULL(SO.OpenQTY , 0) + ISNULL(PO.[Open PO's'] , 0)) - (dbo.ItemVendorDetails.LeadTime / 7 * CASE ISNULL([6WeekSales].SalesQTY / 6 , 0)) 
 ELSE ((dbo.ItemVendorDetails.LeadTime / 7 * CASE ISNULL([12WeekSales].SalesQTY / 12 , 0) - (ISNULL(ItemsStock.StockOnHand , 0) - ISNULL(SO.OpenQTY , 0) + ISNULL(PO.[Open PO's'] , 0)) - (dbo.ItemVendorDetails.LeadTime / 7 * CASE ISNULL([12WeekSales].SalesQTY / 12 , 0)) END

1 个答案:

答案 0 :(得分:0)

你的问题不清楚,但似乎你正在努力解决括号的格式问题。

试试这个: -

select 
CASE when   (ISNULL([6WeekSales].SalesQTY / 6 , 0)) >= (ISNULL([12WeekSales].SalesQTY / 12 , 0)) 
    then (dbo.ItemVendorDetails.LeadTime / 7 * CASE ISNULL([6WeekSales].SalesQTY / 6 , 0) 
    - (ISNULL(ItemsStock.StockOnHand , 0) 
    - ISNULL(SO.OpenQTY , 0) 
    + ISNULL(PO.[Open PO's'] , 0)) 
    - (dbo.ItemVendorDetails.LeadTime / 7 * CASE ISNULL([6WeekSales].SalesQTY / 6 , 0))))
    ELSE 
    ((dbo.ItemVendorDetails.LeadTime / 7 * CASE ISNULL([12WeekSales].SalesQTY / 12 , 0) 
    - (ISNULL(ItemsStock.StockOnHand , 0) 
    - ISNULL(SO.OpenQTY , 0) 
    + ISNULL(PO.[Open PO's'] , 0)) 
    - (dbo.ItemVendorDetails.LeadTime / 7 * CASE ISNULL([12WeekSales].SalesQTY / 12 , 0)))) 
    END