Not sure if this is just a bad habit or a valid way to do something, but for large complex report queries that span table bundles, I often get summary statistics by summing the statement Case.
For example:
SELECT Contact.Name,
SUM(CASE WHEN Order.Type = 'Special' THEN 1 ELSE 0 END) AS SpecialOrders,
SUM(CASE WHEN Order.Type = 'Magic' THEN 1 ELSE 0 END) AS MagicOrders,
FROM Contact
LEFT JOIN Order ON (Contact.ContactID = Order.ContactID)
How to do it in LINQ to SQL? (In vb.net, but I think some example. Net)
Dim Orders = _
From Order In DB.Orders
Select New With {.Name = Contact.Name,
.Special = If(Order.Type = "Special", 1, 0),
.Magical = If(Order.Type = "Magical ", 1, 0)}
I need to sum the values โโof .Specialand .Magical.
(In fact, the request covers several tables consisting of information on booking events, and the decision to summarize the record or does not depend on the fields in several of them)
source
share