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;
Related
Input:
Buy-A
Date
Time
Qty
Per Share price
Total Value
15
10
10
10
100
15
14
20
10
200
Sell - B
Date
Time
Qty
Per Share price
Total Value
15
15
15
20
300
Output:
Date
Buy Time
Buy Qty
Buy Per Share price
Buy Total Value
Sell Qty
Sell Per Share Price
Sell Total Value
15
10
10
10
100
10
20
200
15
14
5
10
50
5
20
100
15
14
15
10
150
By using pyspark
I have a table that looks like this:
customer_id item price cost
1 Shoe 120 36
1 Bag 180 50
1 Shirt 30 9
2 Shoe 150 40
3 Shirt 30 9
4 Shoe 120 36
5 Shorts 65 14
I am trying to find the most expensive item each customer bought along with the cost of item and the item name.
I'm able to do the first part:
SELECT customer_id, max(price)
FROM sales
GROUP BY customer_id;
Which gives me:
customer_id price
1 180
2 150
3 30
4 120
5 65
How do I get this output to also show me the item and it's cost in the output? So output should look like this...
customer_id price item cost
1 180 Bag 50
2 150 Shoe 40
3 30 Shirt 9
4 120 Shoe 36
5 65 Shorts 14
I'm assuming its a Select statement within a Select? I would appreciate the help as I'm fairly new to SQL.
One method that usually has good performance is a correlated subquery:
select s.*
from sales s
where s.price = (select max(s2.price)
from sales s2
where s2.customer_id = s.customer_id
);
Desired Output table T with Calculated Cost column:
SvcID Code ID Date Mins Units Cost
1 3000 15 4/4/2016 60 10 70
2 3000 17 4/4/2016 45 10 0
3 3000 15 5/2/2016 30 10 70
4 3000 18 5/2/2016 60 10 0
5 3000 10 5/2/2016 30 10 0
6 4200 16 2/1/2016 60 4 60
7 4200 9 2/1/2016 30 2 30
Query for calculating and displaying:
SELECT
...
,CASE
WHEN Code=4200 THEN Units*15
WHEN Code=3000 THEN ?
END AS Cost
FROM ...
WHERE Code IN ('3000','4200')
GROUP BY ....;
Cost should be a total of 70 for all services offered on same date for Code 3000, irrespective of number of services offered. No relation between Minutes and Units for this Code for calculating Cost.
One way could be to calculate cost as 70 for any one service and make the remaining services cost 0 for same date. Can this be done in the CASE statement?
Any better way to achieve this?
You need to Investigate Window functions MSDN.
Your case would become something like this:
-- New select statament
SELECT
...
,CASE
WHEN Code=4200 THEN Units*15
WHEN Code=3000 THEN ( CASE WHEN DuplicateNum = 1 THEN 70 ELSE 0 END )?
END AS Cost
FROM(
-- Your current query (with case statement removed) and ROW_NUMBER() function added
SELECT
..., ROW_NUMBER() OVER( PARTITION BY Code, Date ORDER BY ID ) AS DuplicateNum
FROM ...
WHERE Code IN ('3000','4200')
GROUP BY ....
) AS YourCurrentQuery;
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.
I have a SSAS 2012 cube that has project name, milestone name and milestone date dimensions. The basic measure is dollars spent. I need an additional calculated measure of the cumulative dollars for a project spent through each milestone date.
Example of Data:
Project Milestone Milestone Date Spent Running Total
A 1 1/1/2015 10 10
A 2 2/23/2015 35 45
A 3 4/5/2015 4 49
B 1 6/7/2015 2 2
B 2 9/7/2015 49 51
The milestone date dimension is referenced through the Milestone dimension. Any suggestions on how to create the calculated measure would be appreciated.
Here is an example against AdvWrks:
WITH
MEMBER [Measures].[rnTotal] AS
Sum
(
(NULL : [Date].[Calendar].CurrentMember)
*
[Product].[Category].CurrentMember
,[Measures].[Internet Sales Amount]
)
SELECT
[Measures].[rnTotal] ON 0
,
[Product].[Category].[Category]
*
(
[Date].[Calendar].[Month].&[2006]&[6]
:
[Date].[Calendar].[Month].&[2007]&[6]
) ON 1
FROM [Adventure Works];