SQL - Calculate YTD number - sql

Here is the table:
Name
Month
Num
Metric
1/1/22
10
Metric
1/2/22
10
Metric
1/3/22
10
Metric
1/4/22
10
Metric
1/5/22
10
Metric
1/6/22
10
Metric
1/7/22
10
Metric
1/8/22
10
Metric
1/9/22
10
Metric
1/10/22
10
Metric
1/11/22
10
Metric
1/12/22
10
Metric
YTD
How to calculate the YTD number which sums the numbers in the Num column based on the month of the year? For example, it should return 40 right now. In May, it should return 50.
Thanks!

Related

Calculate average and running total in SQL

I have this following table #temp1 with the following column names
Month
RecordTotalByMonth
Type
Product
1
10
New
Wellness
2
20
New
Wellness
3
30
New
Wellness
4
30
New
Wellness
1
15
Average Claim Size
Wellness
2
15
Average Claim Size
Wellness
3
30
Average Claim Size
Wellness
4
10
Average Claim Size
Wellness
1
10
New
Accident
2
20
New
Accident
3
30
New
Accident
4
30
New
Accident
1
15
Average Claim Size
Accident
2
15
Average Claim Size
Accident
3
30
Average Claim Size
Accident
4
10
Average Claim Size
Accident
Now I would like to calculate AVG and running total in the new column "Total or Average".
Calculate running total by months where Product = 'Wellness' or Product = 'Accident' and Type = 'New'.
But calculate Average where Product = 'Wellness' or Product = 'Accident' and Type = 'Average claim size'.
End result should look like this
Month
Record Total By Month
Type
Product
Total or Average
1
10
New
Wellness
10
2
20
New
Wellness
30
3
30
New
Wellness
60
4
30
New
Wellness
90
1
15
Average Claim Size
Wellness
20
2
15
Average Claim Size
Wellness
20
3
30
Average Claim Size
Wellness
20
4
20
Average Claim Size
Wellness
20
1
10
New
Accident
10
2
20
New
Accident
30
3
30
New
Accident
60
4
30
New
Accident
90
1
10
Average Claim Size
Accident
15
2
10
Average Claim Size
Accident
15
3
30
Average Claim Size
Accident
15
4
10
Average Claim Size
Accident
15
My attempt
select Monthly
, RecordTotalByMonth
, Product
, Type
, sum(RecordTotalByMonth) over (partition by Product) as [Total or Average]
INTO New_table
from #temp1
where Type = 'New'
Insert into New_table
SELECT Monthly
, RecordTotalByMonth
, Product
, Type
, avg(RecordTotalByMonth) over (partition by Type) as [Total or Average]
from #temp1
where Type = 'Average Claim Size'
Can you try this:
SELECT month,
recordtotalbymonth,
type,
product,
CASE
WHEN ( ( product = 'Wellness'
OR product = 'Accident' )
AND type = 'New' ) THEN Sum(recordtotalbymonth)
OVER (
partition BY product, type
ORDER BY month)
WHEN ( ( product = 'Wellness'
OR product = 'Accident' )
AND type = 'Average claim size' ) THEN Avg(recordtotalbymonth)
OVER (
partition BY product, type
ORDER BY month)
END AS "Total or Average"
FROM t1;

How to calculate Average based on a criteria on SSAS?

I've a Measure like below,
Year ProductCategory CompanyId TotalCustCnt SalesAmt Rank
2012 Prd1 1 20 100,000 1
2012 Prd2 1 10 75,000 2
2013 Prd1 2 18 80,000 2
2013 Prd2 2 15 50,000 1
Now I want to calculate three averages out of this data.
Average = SalesAmt / TotalCustCnt
Company average (Average for each company)
Leader average (Average for the leader for the Product category,year i.e company with Rank 1)
Industry average (Average for the whole industry for the product category,year)
The first one is straight forward, I've added a calculated field in the CUBE for adding an expression (SalesAmt / TotalCustCnt)
But how do I calculate the other two averages?
I've checked the AVG function and also tried SUM([Measures].[SalesAmt].ALLMEMBER) / SUM([Measures].[TotalCustCnt].ALLMEMBER) but no success.

sql running total math current quarter

Im trying to figure out the total for the quarter when the only data shown is a running total for the year:
Id Amount Periods Year Type Date
-------------------------------------------------------------
1 65 2 2014 G 4-1-12
2 75 3 2014 G 7-1-12
3 25 1 2014 G 1-1-12
4 60 1 2014 H 1-1-12
5 75 1 2014 Y 1-1-12
6 120 3 2014 I 7-1-12
7 30 1 2014 I 1-1-12
8 90 2 2014 I 4-1-12
In the data shown above. The items in type G and I are running totals for the period (in qtrs). If my query returns period 3, is there a sql way to get the data for the qtr? The math would involve retrieving the data for the 3rd period - 2nd period.
Right now my sql is something like:
SELECT * FROM data WHERE Date='4-1-12';
In this query, it will return row #1, which is a total for 2 periods. I would like it to return just the total for the 2nd period. Im looking to make this happen with SQLite.
Any help would be appreciated.
Thank alot
You want to subtract the running total of the previous quarter:
SELECT Id,
Year,
Type,
Date,
Amount - IFNULL((SELECT Amount
FROM data AS previousQuarter
WHERE previousQuarter.Year = data.year
AND previousQuarter.Type = data.Type
AND previousQuarter.Periods = data.Periods - 1
), 0) AS Amount
FROM data
The IFNULL is needed to handle a quarter that has no previous quarter.

Oracle SQL Paired Data

I need to find the difference in averages between patient weights at different visits (time points), but I'm struggling with finding the "paired" averages:
I have 1 table (PHYS) containing patient weights at different visits:
PATIENT VISIT WEIGHT
1 Baseline 200
1 1 Month 190
1 2 Month 170
2 Baseline 300
2 1 Month 290
2 2 Month 280
3 Baseline 250
3 1 Month 230
My problem is that I only want to find the difference for paired data. For example, when calculating the amount of weight loss between the 2 month and Baseline visits, I would want to find the difference between the (average 2 Month weight) and the (average Baseline weight FOR ONLY THOSE PATIENTS WITH A 2 MONTH WEIGHT). In this example, the result should be AVG(170,280) - AVG(200,300) = -25 (since only patient 1 and 2 have 2 Month weights).
Here is what I have, but it calculates the difference based on all weights:
SELECT VISIT
AVG(WEIGHT)
-
(SELECT
AVG(WEIGHT)
FROM PHYS
WHERE VISIT = 'BASELINE')
FROM PHYS
GROUP BY VISIT
My desired output would be (I know I need to add an ORDER BY):
VISIT CHANGE FROM BASELINE
Baseline 0
1 Month -13.3
2 Month -25
Thank you and sorry for such a newb question.
You can do this with a join to the same table but only for the 'Baseline'. Then, the aggregation only aggregates the values that match, so you should get different baseline averages for the three groups (because the populations are different):
select p.visit, avg(p.weight) as avg_weight, avg(pbl.weight) as avg_blweight,
(avg(p.weight) - avg(pbl.weight)) as change
from phys p join
phys pbl
on p.patient = pbl.patient and
pbl.visit = 'Baseline'
group by p.visit;

how to calculate % in PPS 2010

i have these columns in the table and made this table as the FACT table and also using time intelligence filter in the PPS2010..
i have measures , sum (materials), sum (sales) and sum (material_%)
in the PPS dashboard design i have included this cube and all the measures.. and using an analytic chart..
i have developed separate graphs for each columns (material, sales, material_%)..
for the sales and materials there is no problem , when i use the time filter
in the material_% graph i used the time filter current quarter in months (showing three months ) shows the correct value..
when i use the current quarter filter (sum of all the 3 months)
its showing 146% (83 +33 +30) --> for actual values
and 150 % ( 50+50+50) --> for target values
actually it showed show me 46% for actual and 50% for target ,
it should be sum of material in all the 3 months / sum of sales in all the 3 months but its just calculating sum of material_% column of all the 3 months
time filter : year :: Halfyear ::quarter:: Month::Day
DataBase Table:
Month Year Material sales Material_% [ material / sales]
Jan_Act 2011 500 600 83
Jan_target 2011 400 800 50
Feb_Act 2011 300 900 33
Feb_target 2011 300 600 50
Mar_Act 2011 300 900 30
Mar_target 2011 300 600 50
......
Jan_Act 2012 0 0 0
Jan_target 2012 600 1000 60
.............
Dec_Act 2012 0 0 0
Dec_target 2012 600 800 75
MDX Query:
SELECT
HIERARCHIZE( { [Time_dim].[Year - Half Year - Quarter - Month - Date].DEFAULTMEMBER } )ON COLUMNS,
HIERARCHIZE( { [Ven Bi Actfctmaster].[Act Fct].&[ACTUAL], [Ven Bi Actfctmaster].[Act Fct].&[TARGET] } )ON ROWS
FROM [Vin Finance]
WHERE ( [Measures].[Materials - Ven Bifullrptmaster] )
Please help me to sort out this issue.
i solved this issue by changing the measure of '%' columns from sum to averageofchild in the property tab..