Adding a Simple Subtraction Formula to a Case When Statement - sql

I'm writing a sql script where I'd like to add a subtraction formula to the script. My problem is that when I add this CASE STATEMENT my script will not run. I read that you need to add paranetheses around the formula, which I did, and when I do this without the CASE WHEN it will work great. Can you just not use formulas within a case statement?
In my statement below, I have a column, TotalWeightLoss, where its a cumulative total weight lost by a person. So what I am trying to do is see the monthly weight lost instead of a cumulative total.
SELECT *
,case when rownmbr=1 then TotalWeightLoss else (TotalWeightLoss - LAG(TotalWeightLoss) OVER (PARTITION BY AccountNumber ORDER BY ProcessDate, ProcessDate)) AS AmountLost
from cte;"))
Thanks!

Besides the missing END: Assuming that rownmbr is based on a ROW_NUMBER calculation this can be simplified to
TotalWeightLoss
- LAG(TotalWeightLoss,1,0) -- LAG supports a default for a missing value
OVER (PARTITION BY AccountNumber
ORDER BY ProcessDate, ProcessDate) AS AmountLost

Related

SQL calculating running total as you go down the rows but also taking other fields into account

I'm hoping you guys can help with this problem.
I have a set of data which I have displayed via excel.
I'm trying to work out the rolling new cap allowance but need to deduct from previous weeks bookings. I don't want to use a cursor so can anyone help.
I'm going to group by the product id so it will need to start afresh for every product.
In the image, Columns A to D are fixed and I am trying to calculate the data in column E ('New Cap'). The 'New Cap' is the expected results.
Column F gives a detailed formula of what im trying to do.
Not sure what I've done for the post to be marked down.
Thanks
Update:
The formula looks like this.
You want the sum of the cap through this row minus the sum of booked through the previous row. This is easy to do with window functions:
select t.*,
(sum(cap + booked) over (partition by productid order by weekbeg) - booked
) as new_cap
from t;
You can get the new running total using lag and sum over window functions - calculate the cap-booked first, then use sum over() for the running total:
select weekbeg, ProductId, Cap, Booked,
Sum(n) over(partition by productid order by weekbeg) New_Cap
from (
select *, cap - Lag(booked,1,0) over(partition by productid order by weekbeg)n
from t
)t

Spotfire Running Balance (Cumulative Sum)

I am trying to create a running balance column in Spotfire that's supposed to look like the picture attached below. In essence,I want to calculate the cumulative total of the "amount" column row by row, and that I want it to start from 0 as the date changes.
I have tried several OVER functions:
Sum([AMOUNT]) OVER AllPrevious([Date])
Sum([AMOUNT]) OVER Intersect([CURRENCY],AllPrevious([SETTLEDATE]))
Sum([AMOUNT]) OVER Intersect([Calculation Date],AllPrevious([SETTLEDATE]))
Any help is greatly appreciated.
You were very close with your first over statement. The problem is, when you use over (AllPrevious([Date])) and you don't have 1 row for each date, then you will skip rows. So, the last row of your data would only sum it over the rows where 6/1/2017 is in the Date column. Instead, we need to apply a RowID to your data set and then sum over it. This will ensure we sum over all previous rows.
Assuming your data set is in the order you want it to be in when you bring it into SpotFire, do the following:
Insert a calculated column RowID() and name it RowID
Use this calculation: Sum([amount]) over (Intersect([Date],AllPrevious([RowID])))
This will give you the running sum you are looking for.
#scsimon- I modified your custom expression slightly to include date as requested in the question.
Modified expression:
Sum([Amt]) over (intersect(Allprevious([rowID]),[Date]))
Final output table:
#LeoL - Hope this answers your question.

Select and manipulate SQL data, DISTINCT and SUM?

Im trying to make a small report for myself to see how my much time I get inputed in my system every day.
The goal is to have my SQL to sum up the name, Total time worked and Total NG product found for one specific day.
In this order:
1.) Sort out my data for a specific 'date'. I.E 2016-06-03
2.) Present a DISTINCT value for 'operators'
3.) SUM() all time registered at this 'date' and by this 'operator' under 'total_working_time_h'
4.) SUM() all no_of_defects registered at this 'date' and by this 'operator' under 'no_of_defects'
date, operator, total_working_time_h, no_of_defects
Currently I get the data I want by using the Query below. But now I need both the DISTINCT value of the operator and the SUM of the information. Can I use sub-queries for this or should it be done by a loop? Any other hints where I can learn more about how to solve this?
If i run the DISTINCT function I don't get the opportunity to sum my data the way I try.
SELECT date, operator, total_working_time_h, no_of_defects FROM {$table_work_hours} WHERE date = '2016-06-03' "
Without knowing the table structure or contents, the following query is only a good guess. The bits to notice and work with are sum() and GROUP BY. Actually syntax will vary a bit depending on what RDBMS you are using.
SELECT
date
,operator
,SUM(total_working_time_h) AS total_working_time_h
,SUM(no_of_defects) AS no_of_defects
FROM {$table_work_hours}
WHERE date = '2016-06-03'
GROUP BY
date
,operator
(Take out the WHERE clause or replace it with a range of dates to get results per operator per date.)
I'm not sure why you are trying to do DISTINCT. You want to know the data, no of hours, etc for a specific date.
do this....
Select Date, Operator, 'SumWorkHrs'=sum(total_working_time_h),
'SumDefects'=sum(no_ofDefects) from {$table_work_hours}
Where date='2016-06-03'
Try this:
SELECT SUM(total_working_time) as total_working_time,
SUM(no_of_defects) as no_of_defects ,
DISTINCT(operator) AS operator FROM {$table_work_hours} WHERE
date = '2016-06-03'

How to group by 1 element and get sum of other?

I have a table that goes like this:
Now i'm trying to get all called columns and a sum of the duration for a specific called var.
Now i'm trying this :
select called,SUM(duration) as dursum,time,CONVERT(VARCHAR(8), time, 4) AS Batch
from Calls
where caller='somevalue'
group by called,time
order by called
Now the problem is that i guess because i order by time as well i don't really get distinct values by called, i get the same number several times, and ofcourse the sum acts the same, sums only part of the rows.
I can't remove the group by time as it will cause an error for using the SUM function.
Again i want the result to have every called number once per day and the dursum to contain all the duration sum and grouped by the batch column somehow...
How can i solve this?
You are right, not grouping by time brings you the correct resultset. However, the error occuring then is easily explainable: it is caused by the selection of " time,CONVERT(VARCHAR(8), time, 4) AS Batch " in the select clause -> thus you just need some kind of aggregation for the time-field (for instance MAX(time),CONVERT(VARCHAR(8), MAX(time), 4) AS Batch.
Best regards
Well this is how i solved it in the end :
select called,SUM(duration) as dursum, dateadd(DAY,0, datediff(day,0, time))as Batch2 ,CONVERT(VARCHAR(8), time, 4) AS Batch
from Calls
where caller='0502081971'
group by called, dateadd(DAY,0, datediff(day,0, time)),CONVERT(VARCHAR(8), time, 4)
order by called

Sql Server : Column wise total SQl Query

Is it possible to get column wise total using query?
in my grid there are 20 columns. i have to display each columns total value in its footer. now im using TemplateField field and javascript function to get the total value.if it is possible to get it from sql query i can reduce the code
Try something like:
SELECT *, SUM(SalesAmount) OVER() as TotalSales
FROM YourTable
But if you only need the sum and nothing else, just do:
SELECT SUM(SalesAmount) as TotalSales
FROM YourTable
And in future, please try to give more information in your question.
Rob
To sum columns, it's best to use whatever client you're dealing with (Reporting Services, Datagrid, whatever), and just tell that to display a totals row.
If you were to do it within the same query, then you'd end up with rows that meant something different, and displaying it becomes quite awkward.
You CAN do it in the query, but you probably shouldn't.
Rob
I think you are looking for SUM function
Eg:
SELECT SUM(salary) as "Total Salary"
FROM employees
select MAX([p-1]) p1,MAX([p-2]) p2 from #temp