CASE
WHEN
(
[Date].[Hierarchy].CurrentMember
,[Measures].[Billing Reporting CFS - Total Amount EUR]
)
+ 0
= 0
THEN
CASE
WHEN
(
ParallelPeriod
(
[Date].[Hierarchy].[Month]
,1
,[Date].[Hierarchy].CurrentMember
)
,[Measures].[Billing Reporting CFS - Total Amount EUR]
)
> 0.01
THEN 1
ELSE 0
END
ELSE 0
END
The code is as follows, when in that month the total sales - actual sold then is 0 if total sales from last month differs from amount this month then is 1. However I want the 1 to change in the actual amount that is changed how would I go about this?
Thanks in advance.
EDIT
if value is above 0.01 then I want to see the actual value how do I do that?
CASE
WHEN
(
[Date].[Hierarchy].CurrentMember
,[Measures].[Billing Reporting CFS - Total Amount EUR]
)
+ 0
= 0
THEN
CASE
WHEN
(
ParallelPeriod
(
[Date].[Hierarchy].[Month]
,1
,[Date].[Hierarchy].CurrentMember
)
,[Measures].[Billing Reporting CFS - Total Amount EUR]
)
> 0.01
THEN ParallelPeriod([Date].[Hierarchy].[Month],1,[Date].Hierarchy].CurrentMember),[Measures].[Billing Reporting CFS - Total Amount EUR]
ELSE 0
END
ELSE 0
END
Is the answer
Related
I'm writing a query to calculate how much more the average customer has transacted and spent since signing up with my company. However, it returns the error message "Overflow occurred during numeric data type conversion".
The problem is definitely this bit, as when I comment it out the query runs successfully:
SUM(SALES_GROWTH_PERCENT)
Google tells me it's likely that I'm trying to fit a number with too many decimal places into a field that's too short for it. However, I've tried changing how many decimal places the case statements are using with no luck.
Edit: for context, here are the details of the fields being used, from syscat.columns:
This is the query in question. Can anyone see where I'm going wrong please? Any advice would be appreciated!
SELECT SUM(TRANS_GROWTH_PERCENT)/COUNT(CUSTOMER_ID) AS AVG_TRANS_GROWTH_PERCENT,
SUM(SALES_GROWTH_PERCENT)/COUNT(CUSTOMER_ID) AS AVG_SALES_GROWTH_PERCENT
FROM
(
SELECT CUSTOMER_ID,
CASE WHEN POST_SIGNUP_TRANS <=0
THEN 0
WHEN POST_SIGNUP_TRANS >0
THEN CAST((CAST(POST_SIGNUP_TRANS AS DECIMAL (10,5)) - CAST(PRE_SIGNUP_TRANS AS DECIMAL (10,5))) / CAST(PRE_SIGNUP_TRANS AS DECIMAL (10,5)) * 100 AS DECIMAL (10,1))
ELSE NULL
END AS TRANS_GROWTH_PERCENT,
CASE WHEN POST_SIGNUP_SALES <=0
THEN 0
WHEN POST_SIGNUP_SALES >0
THEN CAST((CAST(POST_SIGNUP_SALES AS DECIMAL (10,5)) - CAST(PRE_SIGNUP_SALES AS DECIMAL (10,5))) / CAST(PRE_SIGNUP_SALES AS DECIMAL (10,5)) * 100 AS DECIMAL (10,1))
ELSE NULL
END AS SALES_GROWTH_PERCENT
FROM
(
SELECT CUSTOMER_ID,
SUM(PRE_SIGNUP_TRANS) AS PRE_SIGNUP_TRANS, SUM(PRE_SIGNUP_SALES) AS PRE_SIGNUP_SALES,
SUM(POST_SIGNUP_TRANS) AS POST_SIGNUP_TRANS, SUM(POST_SIGNUP_SALES) AS POST_SIGNUP_SALES
FROM
(
SELECT CUSTOMER_ID,
CASE WHEN TRANSACTION_DATE >= ANALYSIS_START_DATE THEN 1 ELSE 0 END AS ANALYSIS_FLAG,
CASE WHEN TRANSACTION_DATE < SIGNUP_DATE THEN 1 ELSE 0 END AS PRE_SIGNUP_TRANS,
CASE WHEN TRANSACTION_DATE < SIGNUP_DATE THEN SALES ELSE 0 END AS PRE_SIGNUP_SALES,
CASE WHEN TRANSACTION_DATE >= SIGNUP_DATE THEN 1 ELSE 0 END AS POST_SIGNUP_TRANS,
CASE WHEN TRANSACTION_DATE >= SIGNUP_DATE THEN SALES ELSE 0 END AS POST_SIGNUP_SALES
FROM TRANSACTIONS_TABLE
)
WHERE ANALYSIS_FLAG = 1
GROUP BY CUSTOMER_ID
)
WHERE PRE_SIGNUP_TRANS >0
AND PRE_SIGNUP_SALES >0
)
;
I have the following query which returns a total dollar amount.
select sum(cast(dollars as dec)) from financials
This includes positive and negative values.
I would like 2 separate things:
How can I just query the positive dollar amounts? ie. I have 3 records, 10 , -5 , 10. result would be 20.
I want an absolute value as a sum. ie. I have 3 records, 10, -5, 10. the result would be 25.
thanks.
FOR 1) Use conditional SUM()
SELECT SUM( CASE WHEN dollars > 0 then dollars ELSE 0 END) as positive_sum,
SUM( CASE WHEN dollars < 0 then dollars ELSE 0 END) as negative_sum
FROM financials
FOR 2) use ABS()
SELECT SUM( ABS( dollars ) )
FROM financials
Please try below queries. Thanks.
1) select sum(cast(dollars as dec))
from financials
where dollars > 0;
2) select sum(cast(abs(dollars) as dec))
from financials;
You have two queries.solutions are as follows
1.
select sum(dollars) from financials
2.
select sum((case when dollars>0 then dollars end))+sum((case when dollars<0 then -1*dollars end)) from financials
I have two columns in the same table, that needs to be summed and get the difference and then calculate the percentage?
SELECT sum([BillCount]) - sum([IssueCount]) as [Difference]
FROM Invoice
where [Year] = '2015'
In the above query I got the result for the difference as 244234. Now I need to calculate the percentage by dividing the difference with the actual Billcount 260918.
sample Data:
SELECT sum(BillCount) as bills, sum(IssueCount) as issues
FROM Invoice
where [Year] = '2015'
Result:
Bills: 260918
Issues: 16684
Difference: 244234
Divide Difference By actual Bills: 244234/260918 = 0.93
Percentage: 0.93*100 = 93.60
I have used the changed divisor from zero to one. But I am getting the percentage result in many rows, like for each individual billcount. Is there any way that I could get the exact percentage in one row like we calculate in calculator?
Thanks.
Like this?
SELECT sum([BillCount]) - sum([IssueCount]) as [Difference],
(sum(IssueCount) - sum(BillCount)) / nullif(sum(BillCount), 0) as ptcdiff
FROM Invoice
where [Year] = '2015'
You'll note I changed the order of subtraction. This makes it so that its a negative change rather than a positive change. If you need an absolute. change, you can just throw abs() around the result. You can also bake this into a function if it makes sense to
declare
#pctOld float = 260918,
#pctNew float = 16684
select PctDiff = case when #PctNew is null or #PctOld is null then null
when #PctNew = null or #PctOld = null then null
when #PctNew = 0 and #PctOld = 0 then 0
when #PctNew = 0 and #PctOld > 0 then -100.00
when #PctNew >= 0 and #PctOld < 0 then null
when #PctNew < 0 and #PctOld > 0 then null
when #PctOld = 0 then null
else ((#PctNew - #PctOld) / abs(#PctOld)) * 100.0
end
Subquery like this
Select Difference/nullif(sum(BIllCount), 1) From (SELECT sum([BillCount]) - sum([IssueCount]) as [Difference],
FROM Invoice
where [Year] = '2015') Per
Divide your current expression with SUM([BillCount]) aggregated value
Something like this
select (sum([BillCount]) - sum([IssueCount])) / NULLIF(sum([BillCount]),0) * 100
FROM Invoice
where [Year] = '2015'
I use the following
select TotalCredits - TotalDebits as Difference
from
(
select
(select sum(TOTALAMOUNT) from journal where memberid=48 and CREDIT =1) as TotalCredits,
(select SUM(totalamount) from Journal where MEMBERID=48 and DEBIT =1) As TotalDebits
) temp
this returns one field with my difference, the problem i am occuring is that if the table has no credit, but has debits, the temp table contains a NULL value in the TotalCredits Field which prohibts math being done. (Vica Versa on has Credits but no Debits) I have tried coalese but cant seem how to make it work.
rationally i need to check if:
sum(TOTALAMOUNT) from journal where memberid=48 and CREDIT =1 as TotalCredits is
null then totalcredits = 0 and visa versa
sql server 2008
select ISNULL(TotalCredits,0) - ISNULL(TotalDebits,0) as Difference
from
(
select
(select sum(TOTALAMOUNT) from journal where memberid=48 and CREDIT =1) as TotalCredits,
(select SUM(totalamount) from Journal where MEMBERID=48 and DEBIT =1) As TotalDebits
) temp
Change your query to conditional aggregation and it fixes the problem:
select sum(case when credit = 1 then TotalAmount else -TotalAmount end) as Difference
from Journal
where memberid = 48 and (credit = 1 or debit = 1);
EDIT:
If you have the case where credit and debit could both be 1, then use:
select (sum(case when credit = 1 then TotalAmount else 0 end) -
sum(case when debit = 1 then TotalAmount else 0 end)
) as Difference
from Journal
where memberid = 48 and (credit = 1 or debit = 1);
Hello may be this can also can give the expected result
select COALESCE(TotalCredits,0) - COALESCE(TotalDebits,0) as Difference
from
(
select
(select sum(TOTALAMOUNT) from journal where memberid=48 and CREDIT =1) as TotalCredits,
(select SUM(totalamount) from Journal where MEMBERID=48 and DEBIT =1) As TotalDebits
) temp
Create a sub query using isnull to replace the null with 0 and then make the sum querys and then the total substraction
Ouch. That query makes my head hurt. Discriminant functions are your friend and case lets you create them easily in SQL. Just state the problem simply.
select total_credits = sum( case j.credit when 1 then 1 else 0 end ) ,
total_debits = sum( case j.debit when 1 then 1 else 0 end ) ,
total_delta = sum( case j.credit when 1 then 1 else 0 end )
- sum( case j.debit when 1 then 1 else 0 end )
from journal j
where j.memberid = 48
I'm using SQL Server 2008. I've got sales for customers and I want to compare it to what the customer bought the previous year, also calculate the growth. It all works fine but as soon as the customer has a negative for one of the months it brings back the wrong data for Target & Growth.
Customer SalesLastYearMonth SalesThisYearMonth Target Growth
------------------------------------------------------------------------------
abcd -1 15 ???? ???
Code:
SELECT
Customer,
CASE
WHEN SalesThisYearMonth IS NULL THEN (SalesLastYearMonth * -1)
WHEN SalesLastYearMonth IS NULL THEN SalesThisYearMonth
ELSE SalesThisYearMonth - SalesLastYearMonth END as Target,
CASE
WHEN SalesThisYearMonth IS NULL THEN -1
WHEN SalesLastYearMonth IS NULL THEN 1
WHEN SalesThisYearMonth = 0 then -1
WHEN SalesLastYearMonth = 0 then 1
ELSE ( SalesThisYearMonth - SalesLastYearMonth) / SalesLastYearMonth END AS Growth
Assuming you want TARGET=16, GROWTH=16, then this should do it:
SELECT
Customer,
CASE
WHEN SalesThisYearMonth IS NULL THEN (SalesLastYearMonth * -1)
WHEN SalesLastYearMonth IS NULL THEN SalesThisYearMonth
ELSE SalesThisYearMonth - SalesLastYearMonth END as Target,
CASE
WHEN SalesThisYearMonth IS NULL THEN -1
WHEN SalesLastYearMonth IS NULL THEN 1
WHEN SalesThisYearMonth = 0 then -1
WHEN SalesLastYearMonth = 0 then 1
ELSE ( SalesThisYearMonth - SalesLastYearMonth) / ABS(SalesLastYearMonth) END AS Growth
However, I'm really not sure about GROWTH there - not sure why you're dividing by SalesLastYearMonth. In the case above this works, but if SalesLastYearMonth is -4, is dividing by 4 what you want?
You are dividing by a negative number:
SalesLastYearMonth = -1
You need to either check for it or return the absolute value.
Just a little suggestion. try using the ISNULL() function. if the value you select is NULL then you can give a default return value. As far as i know its much faster then CASE WHEN.