I'm in new this field. How do I manage the dynamic calculation with formulas and what steps to achieve the below output?.
I have tables.
Table 1 - Info_question table is for details about questions name and id.
q_id
questions_name
1
A
2
B
3
C
4
D
Table 2 - data_question is for data values.
id
q_id
period
data_value
1
1
2022
1000
2
1
2021
2000
3
2
2022
3000
4
3
2022
4000
5
4
2022
5000
I need to calculate A+B+C and that output will insert into data_question table and new question will create in the info_question table.(formulas will change for new question like A/B*100 or A+C and question id will be new generate)
For (A+B+C) Output should be shows like below tables.
data_question table
id
q_id
period
data_value
1
1
2022
1000
2
1
2021
2000
3
2
2022
3000
4
3
2022
4000
5
4
2022
5000
6
5
2022
8000
7
5
2021
2000
and info_question
q_id
questions_name
1
A
2
B
3
C
4
D
5
E
Related
I am tying to retrieve some data and it is as below:
id
year
value
1
2015
200
1
2016
3000
1
2018
500
2
2010
455
2
2015
678
2
2020
100
as you can see some years are missing - I would like to add the rows with missing years, null for column value and I want to do it per specific ids - any ideas?
You can combine GENERATE_SERIES() with a left join do expand the missing years. For example:
select x.id, x.y, t.value
from (select id, generate_series(min(year), max(year)) as y from t group by id) x
left join t on t.id = x.id and t.year = x.y
Result:
id y value
--- ----- -----
1 2015 200
1 2016 3000
1 2017 null
1 2018 500
2 2010 455
2 2011 null
2 2012 null
2 2013 null
2 2014 null
2 2015 678
2 2016 null
2 2017 null
2 2018 null
2 2019 null
2 2020 100
i have data with fields as shown below
id
grade
grade_id
year
Diff
101
5
7
2022
9
105
k
2
2021
2
106
4
6
2020
5
110
pk
1
2022
1
i want to insert records for same id until we reaches grade = pk , Like shown below for every record in the table .
id
grade
grade_id
year
Diff
101
5
7
2022
9
101
4
6
2021
8
101
3
5
2020
7
101
2
4
2019
6
101
1
3
2018
5
101
k
2
2017
4
101
pk
1
2016
3
need help in sql code
create table amish.cte_test
(id int,
grade int,
year int,
diff int)
insert into amish.cte_test
values (101,5,2022,9)
with recursive temp1( id, grade, year, diff) as
(select id, grade , year , diff from amish.cte_test
union all
select id, grade-1, year-1,diff-1 from temp1
where grade-1 > -2)
select * from temp1
I have table A with product_id,cost,year,quarter,... etc columns.
I have another table B with product_id,base_cost,current_year,p_year,p_quarter,p_order columns.
I want to write an update query to update A from B. My conditions are -
WHERE A.product_id=B.product_id
and A.year=B.current_year
and A.year=B.p_year and A.quarter>B.p_quarter
and A.cost=0;
But the problem is, with these conditions if i have more than one rows in B then i only want to update from the row of B which has the minimum of all quarters.
Example 1-
If A has one row as:
product_id cost year quarter
102 0 2019 1
102 0 2019 2
102 0 2019 3
102 0 2019 4
And B has two rows corresponding to the where clause:
product_id cost current_year p_year quarter
102 3.5 2019 2019 3
102 1.8 2019 2019 1
102 0.5 2019 2019 2
Then updated A should be:
product_id cost year quarter
102 0 2019 1
102 1.8 2019 2
102 1.8 2019 3
102 1.8 2019 4
This is a greatest-n-per-group problem which you need to apply to the table b in a sub-select:
UPDATE A
SET cost = B.base_cost
FROM (
select distinct on (product_id, current_year) product_id, current_year, cost
from b
order by product_id, current_year, quarter
) b
WHERE A.product_id = B.product_id
AND A.year = B.current_year
AND A.cost = 0
I've a table data as below, now I need to fetch the record with in same code, where (Value2-Value1)*2 of one row >= (Value2-Value1) of consequtive date row. (all dates are uniform with in all codes)
---------------------------------------
code Date Value1 Value2
---------------------------------------
1 1-1-2018 13 14
1 2-1-2018 14 16
1 4-1-2018 15 18
2 1-1-2019 1 3
2 2-1-2018 2 3
2 4-1-2018 3 7
ex: output needs to be
1 1-1-2018 13 14
as I am begginer to SQL coding, tried my best, but cannot get through with compare only on consequtive dates.
Use a self join.
You can specify all the conditions you've listed in the ON clause:
SELECT T0.code, T0.Date, T0.Value1, T0.Value2
FROM Table As T0
JOIN Table As T1
ON T0.code = T1.code
AND T0.Date = DateAdd(Day, 1, T1.Date)
AND (T0.Value2 - T0.Value1) * 2 >= T1.Value2 - T1.Value1
id o_num d_num
69af4bf986c4df522afb54da6512bdc5 5 5
69af6111de53b550b0d13f86398b59e5 19 19
69b264c4b93a1984450689b16807b293 10 10
69b26c0fb38ff1cd2d4b01696aa14883 20 20
69b5c46bdc8a8f49f913d9d2325f0a76 15 15
69b71276a69dece5630ed3405ceca411 1 6
69b790c7937602e8fd52bc4d28194625 5 17
69b7bfde4effdaf31d362165a23a8dd0 4 13
69b93626a799636aef2ab3567cf3a110 14 14
I have a table like this, there are total 20 o_num in the table, and i want to select all the row that o_num is 1 then group by the d_num to count the id number, and them change the o_num to 2, until o_num to 20. and the result is in one table.
here is my code for 1 time:
SELECT COUNT(id), o_num, d_num
FROM table1
WHERE o_num = 1
GROUP BY o_num, d_num
how can i change the code to get my table
I want get the reselt like this,a table with 3 columns
sum o_num d_num
9 1 1
8 1 2
4 1 3
……
5 1 20
4 2 1
6 2 2
8 2 3
……
3 2 20
5 3 1
……
……
2 20 20