sql - query for all values in table with limit - sql

I have an SQL query which I run in Amazon Athena:
select
A,
B,
C,
D,
from
T
where
A = '1000'
order by
B desc
limit 1
where I order by B and take the first row only for the value 1000 for A. However I want to run this query for all values of A in T i.e for each A in T get the first row only and append to the results.
How do I do this?
Example of table data:
A B C D
1000 '12/01/2021' 1 7
1000 '10/01/2020' 2 8
1333 '06/01/1920' 3 9
1333 '07/01/1920' 4 10
1999 '09/03/1960' 5 11
1999 '09/03/1950' 6 12
and the result I want to get is:
1000 '12/01/2021' 1 7
1333 '07/01/1920' 4 10
1999 '09/03/1960' 5 11

You can try to use ROW_NUMBER window function to make it.
SELECT A,
B,
C,
D
FROM (
SELECT *,ROW_NUMBER() OVER(PARTITION BY A ORDER BY B DESC) rn
FROM T
) t1
WHERE rn = 1

Related

PostgreSQL Absolute Ranking

The users table is like this:
Id
Name
Room
Point
1
A
1
10
2
B
1
20
3
C
2
30
4
D
2
40
I want to get ranking with some conditions.
The query is SELECT *, RANK() OVER (ORDER BY users.point ASC) rnk FROM users WHERE users.room = 2
Then the ranking column(rnk) is not a absolute ranking.
The query result is
Id
Name
Room
Point
rnk
3
C
2
30
1
4
D
2
40
2
But I want absolute ranking, and the expected result is
Id
Name
Room
Point
rnk
3
C
2
30
3
4
D
2
40
4
Rank first, filter later. For example:
select *
from (
select *, rank() over(order by point) as rnk
from users
) x
where room = 2

Running total of a calculated item in sqlite

I need the running sum of the following in a query in sqlite.
Sum(of a few items)-Sum(of a few items) As rn
E.g.:
``accno qtydel qtyrec rn rnt 001 5 1 4 4 001 2 0 6 10 001 5 3 8 18 004 6 3 9 9 004 1 2 8 17
If using Sqlite 3.25 or newer, window functions make this trivial:
sqlite> WITH mytable(id, n) AS (VALUES (1,8), (2,2), (3,3))
...> SELECT n, sum(n) OVER (ORDER BY id) AS rnt FROM mytable ORDER BY id;
n rnt
---------- ----------
8 8
2 10
3 13
Straight from the SQLite tutorial on window frames ...
Assuming the two columns are labeled a and b in table t:
SELECT
a,
b,
SUM(a) OVER (
ORDER BY rowid
) RunningTotalA,
SUM(b) OVER (
ORDER BY rowid
) RunningTotalB
FROM t;
Output, with headers:
a|b|RunningTotalA|RunningTotalB
8|8|8|8
2|10|10|18
3|13|13|31

SQL query to find the entries corresponding to the maximum count of each type

I have a table X in Postgres with the following entries
A B C
2 3 1
3 3 1
0 4 1
1 4 1
2 4 1
3 4 1
0 5 1
1 5 1
2 5 1
3 5 1
0 2 2
1 2 3
I would like to find out the entries having maximum of Column C for every kind of A and B i.e (group by B) with the most efficient query possible and return corresponding A and B.
Expected Output:
A B C
1 2 3
2 3 1
0 4 1
0 5 1
Please help me with this problem . Thank you
demo: db<>fiddle
Using DISTINCT ON:
SELECT DISTINCT ON (B)
A, B, C
FROM
my_table
ORDER BY B, C DESC, A
DISTINCT ON gives you exactly the first row for an ordered group. In this case B is grouped.
After ordering B (which is necessary): We first order the maximum C (with DESC) to the top of each group. Then (if there are tied MAX(C) values) we order the A to get the minimum A to the top.
Seems like it is a greatest n per group problem:
WITH cte AS (
SELECT *, RANK() OVER (PARTITION BY B ORDER BY C DESC, A ASC) AS rnk
FROM t
)
SELECT *
FROM cte
WHERE rnk = 1
You're not clear which A needs to be considered, the above returns the row with smallest A.
itseems to me you need max()
select A,B, max(c) from table_name
group by A,B
this will work:
select * from (SELECT t.*,
rank() OVER (PARTITION BY A,B order by C) rank
FROM tablename t)
where rank=1 ;

How to update a column with incrementally sequenced values that change depending on other column value

I am trying to update a column in a table so that the Index column (which is currently arbitrary numbers) is renumbered sequentially starting at 1000 with increments of 10, and this sequence restarts every time the Group changes.
I have tried ROWNUMBER() with PARTITION and trying to define a SEQUENCE, but I can't seem to get the result I'm looking for.
Table 1
ID Group Index
1 A 1
2 A 2
3 B 3
4 B 4
5 B 5
6 C 6
7 D 7
What I want:
Table 1
ID Group Index
1 A 1000
2 A 1010
3 B 1000
4 B 1010
5 B 1020
6 C 1000
7 D 1000
You can use row_number() with some arithmetic:
select t.*,
990 + 10 * row_number() over (partition by group order by id) as index
from t;
Note that group and index are SQL reserved words, so they are really bad column names.

how to Get only the rows which's D column hold nearest lowest number to the C column?

------------------------------------------
ID Name C D
------------------------------------------
1 AK-47 10 5
2 RPG 10 20
3 Mp5 20 15
4 Sniper 20 18
5 Tank 90 80
6 Space12 90 20
7 Rifle 90 110
8 Knife 90 85
Consider 1,2 ; 3,4 ; 5,6,7,8 are as separate groups
So i need to get the row group wise that which's D column holds the nearest lower number to the C column
So the Expected Result is :
------------------------------------------
ID Name C D
------------------------------------------
1 AK-47 10 5
4 Sniper 20 18
8 Knife 90 85
How can I achieve this ?
select t1.*
from your_table t1
join
(
select c, min(abs(c-d)) as near
from your_table
group by c
) t2 on t1.c = t2.c and abs(t1.c-t1.d) = t2.near
Here is the syntax for another way of doing this. This uses a cte and will only hit the base table once.
with MySortedData as
(
select ID, Name, C, D, ROW_NUMBER() over(PARTITION BY C order by ABS(C - D)) as RowNum
from Something
)
select *
from MySortedData
where RowNum = 1