SQL Select case

后端 未结 3 1257
梦谈多话
梦谈多话 2021-01-18 16:34

I have the following sql tables

oitems table

    +---------+-----------+----------+
    | orderid | catalogid | numitems |
    +---------+-----------+------         


        
3条回答
  •  死守一世寂寞
    2021-01-18 17:39

    Here is a Linq-to-sql version of your original request (as a LINQpad query):

    Dim odateRequired = {"MasterCard", "Visa"}
    Dim odateNotRequired = {"Paypal", "Sofort"}
    Dim result = From o In Orders Join i In Oitems On o.orderid Equals i.orderid _
                 Let check = o.ocardtype IsNot Nothing _
                         AndAlso ((odateRequired.Contains(o.ocardtype) _
                                   AndAlso o.odate IsNot Nothing) _
                            OrElse odateNotRequired.Contains(o.ocardtype)) _
             Group By i.catalogid Into _
             numitem = Sum(If(check, i.numitems, 0)), _
             ignored = Sum(If(check, 0, i.numitems))
    
    result.Dump
    

    Your extra request in the comments to RichardTheKiwi's answer (it just includes Not (From b In Bookeds Where b.ignoredoid=i.orderid).Any AndAlso at the front of the check):

    Dim odateRequired = {"MasterCard", "Visa"}
    Dim odateNotRequired = {"Paypal", "Sofort"}
    Dim result = From o In Orders Join i In Oitems On o.orderid Equals i.orderid _
                 Let check = Not (From b In Bookeds Where b.ignoredoid = i.orderid).Any _
                         AndAlso o.ocardtype IsNot Nothing _
                         AndAlso ((odateRequired.Contains(o.ocardtype) _
                                   AndAlso o.odate IsNot Nothing) _
                            OrElse odateNotRequired.Contains(o.ocardtype)) _
             Group By i.catalogid Into _
             numitem = Sum(If(check, i.numitems, 0)), _
             ignored = Sum(If(check, 0, i.numitems))
    
    result.Dump
    

提交回复
热议问题