I have this table and I want to add another column to calculate the average of the seconds column.
For example:
my table:
id
avg
1
2.5
2
3.2
3
4.1
4
0.8
my desired table:
id
avg
daily_avg
1
2.5
2.65
2
3.2
2.65
3
4.1
2.65
4
0.8
2.65
Is there any simple and short way to do it?
Im using postgreSQL
Thanks
demo: db<>fiddle
You can use the AVG() window function:
SELECT
id, avg,
AVG(avg) OVER () AS daily_avg
FROM mytable
Related
I'm trying to calculate in Quicksight the median over time on a quarterly bases, so given these values:
Value
Date
1
01/01/2023
2
01/02/2023
3
01/02/2023
4
01/02/2023
5
01/03/2023
6
01/04/2023
7
01/04/2023
8
01/05/2023
Ideally I would like to get these values:
Value
Date
1
01/2023
2.5
02/2023
3
03/2023
6.5
04/2023
7
05/2023
There are some functions in Quicksight (like periodToDateAvgOverTime()) that perform this aggregation for other functions. Is there a way to calculate this with a custom formula or some work around?
You could use periodToDateAvgOverTime(median(value),....), or use the mediaIf(value,condition) and use the date as condition
Currently I'm trying to take a list of values from my table and order them alphanumerically so they appear from number to letters. For example I have this data set:
3
8
0.64
0.64 + 2.8
70
90
AK
050LL (Beta)
070
PQ
W3
0.5
0.6
0.8
040
070
1.2
1.5
1.6
100
150
187
2.8
250
3.0
6.3
800
8mm
And I want it to print 0.5 first and then W3 last. I am using an Lpad to grab the data, but it displays like shown above, with no ordering. Is there a way I can sort these by numbers, then numbers+letters, finally letters in PostgreSQL? Do I need to have some special clause for the ordering to be correct?
The SQL statement:
SELECT *
FROM data_table
ORDER BY LPAD(parameter_type, 10) ASC
OFFSET 0 ROWS FETCH NEXT 1000 ROWS ONLY;
would like to find out the syntax in tableau, given column number, trying to generate rows for number in decreasing order down to 0.
Below is an example of what I'm trying to do
BEFORE
ID
NUMBER
A
4
B
5
AFTER
ID
NUMBER
A
4
A
3
A
2
A
1
B
5
B
4
B
3
B
2
B
1
If your Tableau version supports, try using ROWNUMBER with order by. Something like this:
{ ORDERBY [Your column]:ROW_NUMBER()}
I would like to create a table in Amazon Redshift SQL or in PostgreSQL that will contain consecutive integers from 0 to some large random number. This should be done without using arrays which are not supported in Redshift version of SQL.
CREATE TEMP TABLE tmp_numbers_1000 AS
SELECT ROW_NUMBER() OVER()
FROM stl_scan
LIMIT 1000;
SELECT * FROM tmp_numbers_1000 ORDER BY 1 LIMIT 10;
row_number
------------
1
2
3
4
5
6
7
8
9
10
(10 rows)
SELECT * FROM tmp_numbers_1000 ORDER BY 1 DESC LIMIT 10;
row_number
------------
1000
999
998
997
996
995
994
993
992
991
(10 rows)
I have a postgresql table that looks like this
Division Rate
a 7
b 3
c 4
a 5
b 2
a 1
I want to return a table that looks like this
Division Average
a 3.5
b 1.6
c 5
Is there any way for me to do that? I can't seem to come up for the logic for it.
select Division,avg(Rate) from your_table group by Division;