T-SQL select statement to redistribute value in row into n rows - sql

I have a table like (SQL Server 2016):
ID Month Sales
1 Jan 2019 40
2 Feb 2019 80
3 Mar 2019 400
...
would like to get sales redistributed by weeks (here we can assume each month is 4 weeks) like:
ID Month Sales
1 012019 10
1 022019 10
1 032019 10
1 042019 10
2 052019 20
2 062019 20
2 072019 20
2 082019 20
3 092019 100
3 102019 100
3 112019 100
3 122019 100
...
How can I achieve sth like that?

You could join the query with a hard-coded query that generates four rows:
SELECT id, month, sales / 4
FROM mytable
CROSS JOIN (SELECT 1 AS col
UNION ALL
SELECT 2
UNION ALL
SELECT 3
UNION ALL
SELECT 4) t

Related

Counting SUM(VALUE) from previous cell

I have the following table:
A
Sum(Tickets)
01-2022
5
02-2022
2
03-2022
8
04-2022
1
05-2022
3
06-2022
3
07-2022
4
08-2022
1
09-2022
5
10-2022
5
11-2022
3
I would like to create the following extra column 'TotalSum(Tickets)' but I am stuck....
Anyone who can help out?
A
Sum(Tickets)
TotalSum(Tickets)
01-2022
5
5
02-2022
2
7
03-2022
8
15
04-2022
1
16
05-2022
3
19
06-2022
3
22
07-2022
4
26
08-2022
1
27
09-2022
5
32
10-2022
5
37
11-2022
3
40
You may use SUM() as a window function here:
SELECT A, SumTickets, SUM(SumTickets) OVER (ORDER BY A) AS TotalSumTickets
FROM yourTable
ORDER BY A;
But this assumes that you actually have a bona-fide column SumTickets which contains the sums. Assuming you really showed us the intermediate result of some aggregation query, you should use:
SELECT A, SUM(Tickets) AS SumTickets,
SUM(SUM(Tickets)) OVER (ORDER BY A) AS TotalSumTickets
FROM yourTable
GROUP BY A
ORDER BY A;
left join the same table where date is not bigger, then sum that for every date:
select
table1.date,
sum(t.tickets)
from
table1
left join table1 t
on t.date<= table1.date
group by
table1.date;

Oracle Sql cumulative sum by month and user

I have an table like below in oracle
USER
YEAR
MONTH
POINT
1
2020
1
1
2
2020
1
1
3
2020
1
0
1
2020
2
1
2
2020
2
0
3
2020
2
1
1
2020
3
1
2
2020
3
0
3
2020
3
1
now I want to get table like below
USER
YEAR
MONTH
POINT
1
2020
1
1
1
2020
2
1
1
2020
3
2
2
2020
1
1
2
2020
2
1
2
2020
3
1
3
2020
1
0
3
2020
2
1
3
2020
3
2
I tried below but not working what I expected
SELECT A_YEAR,A_MONTH,USER, SUM(POINTT) OVER (ORDER BY A_YEAR,A_MONTH,USER) CUM_POINT
FROM (
SELECT WA.USER, WA.A_YEAR,WA.A_MONTH,1 AS POINTT FROM HRANALY.PUAN_ACTUAL PA
INNER JOIN HRANALY.WAGE_ACTUAL WA ON WA.USER= PA.USER AND WA.A_YEAR = PA.A_YEAR AND WA.A_MONTH = PA.A_MONTH
INNER JOIN HRANALY.PUAN_ACTUAL_DETAIL PAD ON PAD.REF_WAGE=PA.ID AND PAD.KALEM_KOD='GRUP_HEDEF' AND PAD.AMOUNT>0
ORDER BY a_month
)
ORDER BY A_YEAR,A_MONTH,USER;
in this query CUM_POINT goes from 1 to n not working as user year month based
How can I get second table with query.
Thanks in advance
You need analytical function as follows:
select user_id, year, month,
sum(point) over (partition by user_id order by year, month) as points
from t
In ytour query just add partition by clause as follows:
SUM(POINTT) OVER (PARTITION BY USER ORDER BY A_YEAR,A_MONTH,USER) CUM_POINT
select x.*,
sum(point) over (partition by year, month, userid) sum_point
from foo x

Decreasing of the sum by percentage (subtraction of percentage) SQL

Substraction of percentage
I have got a table where is one record. Table looks like:
cust_code, SUM
1 25
I need to calculate a subtraction like this.
cust_code, ID, SUM
1 1 25
1 2 23
1 3 21.16
1 4 19.47
1 5 17.91
1 6 16.48
1 7 15.16
1 8 13.95
. . .
. . .
. . .
1 15 7.78
where value of sum in record 2 is subtracted by 8% of record 1,
value of sum in record 3 is subtracted by 8% of record 2,
value of sum in record 4 is subtracted by 8% of record 3, etc.
Max ID will be 15.
It should be single query, I can use any additional external table (containing for example simple counter from 0 to 15).
Best regards,
Volcano
Easy to do with a recursive CTE:
WITH cte AS
(SELECT cust_code, 1 AS id, sum FROM cust
UNION ALL
SELECT cust_code, id + 1, sum * 0.92 FROM cte WHERE id < 15)
SELECT * FROM cte ORDER BY id;
cust_code id sum
---------- ---------- ----------
1 1 25.0
1 2 23.0
1 3 21.16
1 4 19.4672
1 5 17.909824
1 6 16.4770380
1 7 15.1588750
1 8 13.9461650
1 9 12.8304718
1 10 11.8040340
1 11 10.8597113
1 12 9.99093444
1 13 9.19165969
1 14 8.45632691
1 15 7.77982076

to join the same table to match records from last year

I want to create a join that joins records of last year to the same period of the current year. All the data is in the same table.
Input Table:
A | B | C | D
a 2017 1 10
a 2017 2 20
a 2017 3 5
a 2016 1 100
a 2016 2 50
a 2016 3 1
Output Table:
A | B | C | D | E
a 2017 1 10 100
a 2017 2 20 50
a 2017 3 5 1
a 2016 1 100 NULL
a 2016 2 50 NULL
a 2016 3 1 NULL
There are several ways of doing this. One is a left join:
select t.*, tprev.d as e
from t left join
t tprev
on t.a = tprev.a and t.c = tprev.c and tprev.b = t.b - 1;
Another way is to use window functions:
select t.*, lag(t.d) over (partition by t.a, t.c order by b) as e
from t;
These work on the same on your data. But they are subtly different. The first looks specifically at the data one year before. The second looks at the most recent year before where there is data.

Get 1 row from multiple columns

I have 2 tables
table#1: Order
orderid unitid active
1 aa 1
2 bb 0
3 cc 1
4 dd 1
table#2:Details
orderid month
1 6
1 7
1 12
2 1
2 6
3 1
3 2
3 3
3 4
3 6
Output desired:
orderid unitid jan feb mar apr may jun ......... dec
1 aa yes yes
3 cc yes yes yes yes
For all orders where ACTIVE is 1 and all unitids.
I tried using case statement, i get multiple rows for a single orderid, which is not how i want.
I see a lot of examples for pivot with one table, how to do this using 2 tables? I am using SQL Server 2012.
Maybe a Select within a SELECT as argument
Something like this;
Select orderid, unitid, (SELECT month
From Table2
WHERE ...)
From table1
Where ...
I am referencing with this answer this Issue:
A select query selecting a select statement