使用案例陈述

时间:2015-07-14 12:29:32

标签: sql sql-server select case

下午好。

我是SQL Server的新手,需要建立我的选择。我正在寻找一些建议,因为我不知道该转向何处。

所以如果列存在于其他表中,我需要更新一个表。然而,另一个表可能只有999的defualt值,这意味着任何值。我会尝试编写sudo代码。

select row
from master table
where between dates
and cust is in ( custtable where cust matches or cust from custtable = 999)
and branch is in ( branchtable where branch matches or branch from branchtable = 999)
and product is in ( producttable where product matches or product from producttable = 999)

只有当3等于被发现时我才能拥有该行。

请告诉我您需要更多信息,因为它可能没有意义

提前致谢。在我身上轻松我只是在学习

更新

所以我有以下

select @SI_Id = SIC_IdSupportedInitiative, @SI_SuppId = SIC_IdSupplier,
 @sd = SIC_StartDate, @ed = SIC_EndDate, @SI_Amt = SIC_ClaimAmount
from SIC_SupportedInitiative
where SIC_IdSupportedInitiative = 1

select sc.ID, sc.[Actual Posting Date],sc.[Customer Account], 
 sc.[Accounting Branch],sc.[Product Code],sc.[SalesQ uantity],sc.SIC_TotalClaimAmount
From SalesCurrent sc
where sc.[Actual Posting Date] between @sd and @ed
and exists 
( select 1 from SIC_CustomerDetails c
    where c.SIC_IdSupportedInitiative = @SI_id
      and c.SIC_ICC_Customer = sc.[Customer Account] or c.SIC_ICC_Customer = 999 )
and exists
( select 1 from SIC_BranchDetails b
    where b.SIC_IdSupportedInitiative = @SI_id
      and b.SIC_IMO_Branch = sc.[Accounting Branch] or b.SIC_IMO_Branch = 999 )
and exists
( select 1 from SIC_ProductDetails p
    where p.SIC_IdSupportedInitiative = @SI_id
      and p.SIC_SupplierProductCode = sc.[Product Code] or 
          p.SIC_SupplierProductCode IN (SELECT sp.SIC_SupplierProductCode 
                                          FROM SIC_SupplierProducts sp 
                                         WHERE sp.SIC_IdSupplier = @SI_SuppId 
                                           AND sp.SIC_SupplierProductCode = sc.[Product Code])
)

then I was going to loop round as I need to update the SIC_TotalClaimAmount 

to be SIC_TotalClaimAMount = SIC_TotalClaimAmount + ( sc.[Sales Quantity] * @SI_Amt )

再次,如果有一种更容易听到的方式。

由于

1 个答案:

答案 0 :(得分:3)

我不知道查询的其余部分是什么样的,但您可以使用exists执行您要查找的逻辑:

from mastertable mt
where between dates and
      exists (select 1 from custtable c where c.cust = mt.cust or c.cust = 999) and
      exists (select 1 from branchtable b where b.branch = mt.branch or b.branch = 999) and
      exists (select 1 from producttable p where p.product = mt.product or p.product = 999)