I expect someone could help me.
I made a table with some data, no need to know what in particual. I come from this #TempTable1:
WEEK_NUMBER SUM MEAN_OF_SUM
---------------------------------------------------------------------------
1 10
2 20
3 30
4 60
5 30
6 60
7 0
My desired table #TempTable1:
WEEK_NUMBER SUM MEAN_OF_SUM
---------------------------------------------------------------------------
1 10 10 --(10/1)
2 20 15 --(30/2)
3 30 20 --(60/3)
4 60 30 --(120/4)
5 30 30 --(150/5)
6 60 35 --(210/6)
7 0 30 --(210/7)
I tried with sums and expected the group by clause would work, but they do not. Does someone has an idea on how to solve this ?
I think are looking for a cumulative average:
select t.*, avg(sum*1.0) over (order by week_number)
from t;
This doesn't match the last value. I'm guessing that is a typo.
Related
Suppose there is a table StackOverflow
Stackoverflow
Date
num_of_questions
num_of_answers
2022/09/01
10
5
2022/09/01
20
5
2022/09/02
25
10
2022/09/02
10
9
2022/09/03
1
8
2022/09/03
8
2
Is there a way to select all entries within a given date range, and sum all the values for each same day? For instance, return the sum for the range 2022/09/01 - 2022/09/02:
expectation return:
Date
num_of_questions
num_of_answers
2022/09/01
30
10
2022/09/02
35
19
So far I only figured out to write
SELECT * FROM Stackoverflow WHERE Date BETWEEN TIMESTAMP BETWEEN'2011/02/01' and '2011/02/02';
which return:
Date
num_of_questions
num_of_answers
2022/09/01
10
5
2022/09/01
20
5
2022/09/02
25
10
2022/09/02
10
9
(but I have not been able to sum this)
SELECT Date, SUM(num_of_questions) as num_of_questions , SUM(num_of_answers) as num_of_answers
FROM Stackoverflow
WHERE Date BETWEEN '2011/02/01' and '2011/02/02' GROUP BY Date;
Change your query as above then it will give the expected results.
My original table looks like:
id, date, 1, 2, 3,
1 1 10 10 10
1 2 20 20 20
1 3 30 30 30
1 4 15 15 15
By running the query:
ARRAY_AGG(STRUCT(1, 2, 3)) OVER (PARTITION BY id ORDER BY date ROWS BETWEEN 2
PRECEDING AND CURRENT ROW)
I get this output
1 2 3
1 10 10 10
2 10 10 10
20 20 20
3 10 10 10
20 20 20
30 30 30
4 20 20 20
30 30 30
15 15 15
I would like the output to be:
1 2 3
1 10 10 10
2 20 20 20
10 10 10
3 30 30 30
20 20 20
10 10 10
4 15 15 15
30 30 30
20 20 20
So bascically, the values that I get in my output are all correct, but I would like order of the output to be flipped. Anyone know how to do that with an array(struct)) type of column?
One option is to reverse the sort order of the window clause, or you could use a simple solution of calling the ARRAY_REVERSE function:
ARRAY_REVERSE(
ARRAY_AGG(STRUCT(col1, col2, col3)) OVER (
PARTITION BY id ORDER BY date
ROWS BETWEEN 2 PRECEDING AND CURRENT ROW)
)
Added below for the sake of having reverse the sort order of the window clause option here
ARRAY_AGG(STRUCT(col1, col2, col3)) OVER(
PARTITION BY id ORDER BY date DESC
ROWS BETWEEN CURRENT ROW AND 2 FOLLOWING)
Trying to work on SUM SQL code to SUM SOH for all ITEM_PARENT numbers. SOH is the result I'm looking for and ITEM_PARENT and STOCK_ON_HAND is what I have.
CASE WHEN ITEM_PARENT = ITEM_PARENT THEN SUM(STOCK_ON_HAND) ELSE 'DN' END AS SOH_SUM,
The code I have already done seems to be not working....
ITEM_PARENT STOCK_ON_HAND SOH_SUM
123649336 1 11
123649336 1 11
123649336 1 11
123649336 5 11
123649336 2 11
123649336 1 11
123649328 1 15
123649328 1 15
123649328 2 15
123649328 1 15
123649328 2 15
123649328 1 15
123649328 1 15
123649328 3 15
123649328 3 15
124566152 3 19
124566152 1 19
124566152 3 19
124566152 3 19
124566152 2 19
124566152 7 19
Anyone know what I'm missing there?
It looks as though you trying to do some sort of conditional aggregation, but your expected output does not require this. We can just do a select over the entire table and then use SUM as an analytic function to compute the SOH_SUM.
SELECT
ITEM_PARENT, STOCK_ON_HAND,
SUM(STOCK_ON_HAND) OVER (PARTITION BY ITEM_PARENT) SOH_SUM
FROM yourTable
ORDER BY ITEM_PARENT;
You can use window functions to get the desired result.
select
[ITEM_PARENT]
,[STOCK_ON_HAND]
,sum([STOCK_ON_HAND]) over(partition by [ITEM_PARENT]) as [ItemTotal]
from tablename
;
Otherwise, if you wanted to just get the total stock on hand for each item then group the data:
select
[ITEM_PARENT]
,sum([STOCK_ON_HAND]) as [TotalStockOnHand]
from tablename
group by
[ITEM_PARENT]
;
t 10 20 30 40
output 10 30 50 70
One method of doing this is a cumulative sum with a window function:
select col,
sum(col) over (order by col
rows between 1 preceding and current row
)
from t;
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;