Using case statement on sum aggregate - sql

I'm trying to run a case on an aggregate sum but can't seem to get this working...essentially I want to return 1 if the sum of the column is > 0...
SELECT Shop.Brands, Shop.Brand, Shop.T3, Shop.ShopName, Shop.Period
CASE WHEN sum(PLData.Actuals) > 0 THEN 1 ELSE 0 END as Actuals,
CASE when sum(PLData.Budgets) > 0 THEN 1 ELSE 0 END as Budget,
CASE when sum(pldata.ForecastLedger) > 0 THEN 1 ELSE 0 END as Forecast
FROM SunFinance.dbo.Shop Shop LEFT OUTER JOIN SunFinance.dbo.PLData PLData ON Shop.T3 = PLData.Shop
WHERE Shop.BusinessType In ('CORPORATE','RETAIL','WHOLESALE')
AND PLData.Account Like '488%')
GROUP by shop.brand, shop.brands, shop.t3, shop.shopname, Shop.Period
Where am I going wrong?

If your DBMS doesn't let you use aggregates in case expressions, you can try to do the aggregation first in an inline view, then do your CASE expressions.
WITH RESULTS AS (
SELECT Shop.Brands
, Shop.Brand
, Shop.T3
, Shop.ShopName
, Shop.Period
, sum(PLData.Actuals) as Actuals
, sum(PLData.Budgets) as Budget,
, sum(pldata.ForecastLedger) as Forecast
FROM SunFinance.dbo.Shop Shop
LEFT OUTER JOIN SunFinance.dbo.PLData PLData
ON Shop.T3 = PLData.Shop
WHERE Shop.BusinessType In ('CORPORATE','RETAIL','WHOLESALE')
AND PLData.Account Like '488%'
GROUP by shop.brand, shop.brands, shop.t3, shop.shopname, Shop.Period
)
SELECT brands, brand, t3, shopname, period,
case when actuals > 0 then 1 else 0 end as actuals,
case when budget > 0 then 1 else 0 end as budget,
case when forecast > 0 then 1 else 0 end as forecast
FROM results

Related

sum a value that appears on multiple rows ones per id in microsoft sql

I have a table that looks like below. In my query I'm sum:ing the order item value for each order item type, but I'm not sure how to summarize the providerfee per order id when the providerfee appears on multiple rows. Sum(providerfee) would give me 60 instead of 30.
Raw table (sample):
order id
order item type
order item value
providerfee
1
Product
300
30
1
Shipping
40
30
1
Invoicefee
30
30
Aggregated table if I would query above example table with the query I'm using now:
noOfOrders
productAmount
ShippingAmount
InvoiceAmount
providerfee
1
300
40
30
60
What value I wish to get in the providerfee column in my aggregated table instead:
noOfOrders
productAmount
ShippingAmount
InvoiceAmount
providerfee
1
300
40
30
30
Does anyone now best practice of fetching the right sum per order id from the providerfee-column? I think it won't make any sense to publish the actual query I'm writing but basically what I'm doing to fetch the amounts for order item types is:
with amounts as (
select
case when orderItemType= 'Product' then orderItemValue else 0 end as productAmount
,case when orderItemType = 'Shipping' then orderItemvalue else 0 end as shippingAmount
case when orderItemType = 'Fee' then orderItemvalue else 0 end as InvoicefeeAmount
,providerfee
from exampleTable
)
select
sum(amounts.productAmount) as productAmount
,sum(amounts.shippingAmount) as shippingAmount
,sum(amounts.invoicefeeAmount) as invoicefeeAmount
,sum(amounts.providerfee) as providerfeeAmount <---- This one gives me too high values since I'm summing every rows and providerfee appears on every row
from amounts
Here's a picture of how my actual table look like. Providerfee is supposed to be 638 just like invoiceAmount
One thing I also tried was a logic with row number function. But this gave me a result where three providerfees where missing. Instead of 638 I got 551. Can't see why since I counted in the raw table and got it to 638 myself. Not sure if I'm missing something logical with row number function that can have an impact on the SUM. Should I try another function to be able to pull out only one providerfee-row per order id?
with amounts as (
select
row_number() over (partition by orderId order by orderItemTimestamp DESC) as rn
,case when orderItemType= 'Product' then orderItemValue else 0 end as productAmount
,case when orderItemType = 'Shipping' then orderItemvalue else 0 end as shippingAmount
,case when orderItemType = 'Fee' then orderItemvalue else 0 end as InvoicefeeAmount
,providerfee
from exampleTable
)
select
sum(amounts.productAmount) as productAmount
,sum(amounts.shippingAmount) as shippingAmount
,sum(amounts.invoicefeeAmount) as invoicefeeAmount
,sum(amounts.providerfee) as providerfeeAmount <---- This one gives me too high values since I'm summing every rows and providerfee appears on every row
,sum(case when (amounts.providerfee is not null and amounts.rn = 1) then amounts.providerfee else 0 end) as providerfee2
from amounts
Grand total can be done using conditional aggregation. (Note that I here expect every Orderto have exactly one Invoicefee.)
select COUNT(distinct OrderId) as noOfOrders,
SUM(case when orderItemType = 'Product' then orderItemValue else 0 end) as productAmount,
SUM(case when orderItemType = 'Shipping' then orderItemvalue else 0 end) as shippingAmount,
SUM(case when orderItemType = 'Fee' then orderItemvalue else 0 end) as InvoicefeeAmount,
SUM(case when orderItemType = 'Fee' then providerfee else 0 end) as providerfee
from amounts
You can also add a GROUP BY, to get aggregation per order:
select OrderId,
SUM(case when orderItemType = 'Product' then orderItemValue else 0 end) as productAmount,
SUM(case when orderItemType = 'Shipping' then orderItemvalue else 0 end) as shippingAmount,
SUM(case when orderItemType = 'Fee' then orderItemvalue else 0 end) as InvoicefeeAmount,
SUM(case when orderItemType = 'Fee' then providerfee else 0 end) as providerfee
from amounts
group by OrderId

Using a case column within another case in select clause

I have a select clause with a case statement and I need to create another case statement comparing the column created by the previous case statement. Something like this:
select client
,discount
,(case when sales_avg>10000 then 30
when sales_avg>5000 then 20
else 0 end) discount_rule
,(case when discount < discount_rule then 1 else 0 end) status
from sales;
I get a message that discount_rule is unknown. How can I accomplish that?
You can use a Common Table Expression (CTE) and reference a CTE within a CTE as:
with CTE_discount_rule as
(
select client,
discount,
(case when sales_avg>10000 then 30
when sales_avg>5000 then 20
else 0 end) as discount_rule
from sales
),
CTE_Final_Status as
(
select client,
discount,
discount_rule,
(case when discount < discount_rule then 1 else 0 end) as status
from CTE_discount_rule
)
select * from CTE_Final_Status;
The simplest way is to use a subquery that returns the column discount_rule:
select t.client, t.discount, t.discount_rule,
case
when discount < discount_rule then 1
else 0
end status
from (
select client, discount,
case
when sales_avg > 10000 then 30
when sales_avg > 5000 then 20
else 0
end discount_rule
from sales
) t

How Do I Use Case When with Group By statement

I have made a query but it don't give me what i want :
SELECT Designation_projet, COUNT(*) AS [Nb Demande], CASE WHEN Validation = 0 THEN COUNT(*) END AS Validée, CASE WHEN Validation = 1 THEN COUNT(*)
END AS NonValidée, CASE WHEN Commandé = 0 THEN COUNT(*) END AS NonCommandé,
CASE WHEN Commandé <> 0 THEN COUNT(*) END AS Commandé, SUM(TotalHT) AS TotalHT
FROM V_DemandeAchat
GROUP BY Designation_projet,Commandé,Validation
Please help me to achieve my gool.
Thanks in advance
You can do this using condition aggregation. That means that the CASE expression is the argument to SUM() -- it goes "inside" not "outside":
SELECT Designation_projet, COUNT(*) AS [Nb Demande],
SUM(CASE WHEN Validation = 0 THEN 1 ELSE 0 END) as Validée,
SUM(CASE WHEN Validation = 1 THEN 1 ELSE 0 END) as NonValidée,
SUM(CASE WHEN Commandé = 0 THEN 1 ELSE 0 END) AS NonCommandé,
SUM(CASE WHEN Commandé <> 0 THEN 1 ELSE 0 END) END AS Commandé,
SUM(TotalHT) AS TotalHT
FROM V_DemandeAchat
GROUP BY Designation_projet;
If you want one row per Designation_projet, then that should be the only key in the GROUP BY.
Assuming Validation takes on only two values, you can simplify the first two aggregation expressions:
SELECT Designation_projet, COUNT(*) AS [Nb Demande],
SUM(1 - Validation) as Validée,
SUM(Validation = 1) as NonValidée,
SUM(CASE WHEN Commandé = 0 THEN 1 ELSE 0 END) AS NonCommandé,
SUM(CASE WHEN Commandé <> 0 THEN 1 ELSE 0 END) END AS Commandé,
SUM(TotalHT) AS TotalHT
FROM V_DemandeAchat
GROUP BY Designation_projet;
You may be able to simplify the Commandé expressions as well.
Try this
SELECT Designation_projet, COUNT(*) AS [Nb Demande],sum(CASE WHEN Validation = 1 THEN 1 else 0 END AS Validée,sum( CASE WHEN Validation = 1 THEN 1 else 0
END AS NonValidée, sum(CASE WHEN NonCommandé <> 1 THEN 1 else 0 END AS NonCommandé,
sum(CASE WHEN Commandé <> 1 THEN 1 else 0 END AS Commandé, SUM(TotalHT) AS TotalHT
FROM V_DemandeAchat
GROUP BY Designation_projet

SQL percentage with rows same table with different where condition

I want to do a query like:
select
count(asterisk) where acción='a'/count(asterisk) where acción='b' * 100
from
same_table
grouped by day
but I don't want use subquery, is it possible with joins?
I`m not sure the syntax is correct, but you can use something like this:
SELECT day,
SUM(CASE WHEN "acción" = 'a' THEN 1 ELSE 0 END) AS SUM_A,
SUM(CASE WHEN "acción" = 'b' THEN 1 ELSE 0 END) AS SUM_B,
SUM(CASE WHEN "acción" = 'a' THEN 1 ELSE 0 END) AS SUM_A / SUM(CASE WHEN "acción" = 'b' THEN 1 ELSE 0 END) * 100 AS result
FROM your_table
GROUP BY day
The concept is to actually sum the the values that you need, instead of count.

SUM field not summing

Similar to my last question. Heres my code:
SELECT TR.ITEMNO,
SUM ( TR.TRANS_QUAN * CASE TR.TRANS_TYPE WHEN 'PO' THEN 1 ELSE 0 END ) AS SUM_IN,
SUM ( TR.TRANS_QUAN * CASE TR.TRANS_TYPE WHEN 'MH' THEN 1 ELSE 0 END ) AS SUM_OUT
FROM IQMS.TRANSLOG TR
WHERE TR.ITEMNO = '200672'
GROUP BY TR.ITEMNO
HAVING SUM ( TR.TRANS_QUAN * CASE TR.TRANS_TYPE WHEN 'PO' THEN 1 ELSE -1 END ) < 0
ORDER BY TR.ITEMNO
The code is running fine but it is not summing anything for SUM_OUT. I know for a fact however that there are numbers there to be summed. Is there something obvious that I am missing? Thanks in advance!! :)
Try...
SUM (TR.TRANS_QUAN * CASE TR.TRANS_TYPE WHEN 'PO' THEN 1 ELSE 0 END )
OVER(PARTITION BY TR.ITEMNO) AS SUM_IN,
SUM ( TR.TRANS_QUAN * CASE TR.TRANS_TYPE WHEN 'MH' THEN 1 ELSE 0 END )
OVER(PARTITION BY TR.ITEMNO) AS SUM_OUT,