sql or python solution for getting merging duplicate rows into one in an ordered table and reordering them - duplicates

Can someone help me do this in SQL in a select statement?
I have a table xyz as follow:
ColumnID
Column A
Column B
1
1
A
1
2
B
1
3
C
1
4
D
2
1
A
2
2
B
2
3
C
2
4
C
3
1
A
3
2
A
3
3
B
3
4
B
4
1
A
4
2
B
4
3
V
4
4
V
I want it to change to this:
Column A
Column B
1
A
2
B
3
C
4
D
1
A
2
B
3
C
1
A
2
B
1
A
2
B
3
V
haven't tried anything

Here is the solution for anyone who has the same question:
with abc as (select columnID, ROW_NUMBER() over (PARTITION by columnID, column_b ORDER BY columnID) as column_a, column_b
from xyz)
select row_number() over (partition by columnID order by column_a asc) as column_a, column_b
from abc where row_num = 1

Related

Django: groupby with getting first row

I have table as
Model name: Test
id col1 col2 col3 col4 .....
1 A 1
2 A 1
3 A 3
4 B 1
5 B 1
6 B 4
7 D 2
8 D 3
9 D 3
10 F 3
11 B 4
I want to get all the rows with unique combination of col1 and col2 and the row with recent id and also the total count of the unique combination of col1 and col2
count id col1 col2 col3 col4 .....
2 11 B 4
1 10 F 3
1 9 D 3
1 7 D 2
2 5 B 1
1 3 A 3
1 2 A 1
and also later i want to sort w.r.t count and id. But i want the above table also
count id col1 col2 col3 col4 .....
2 11 B 4
2 5 B 1
1 10 F 3
1 9 D 3
1 7 D 2
1 3 A 3
1 2 A 1
How can i get this
I am using mysql
SELECT count(*) as count,max(id) as id,col1,col2 FROM test group by col1,col2 order by id desc;
sort:
SELECT count(*) as count,max(id) as id,col1,col2 FROM test group by col1,col2 order by count desc,id desc;

subtract from values by groups

What is the best way to subtract the lowest value from all values by group?
Something like:
ID Name Value
1 A 10
1 B 40
1 C 100
2 A 20
2 B 80
2 C 90
3 A 4
3 B 7
3 C 8
turn to:
ID Name Value
1 A 0
1 B 30
1 C 90
2 A 0
2 B 60
2 C 70
3 A 0
3 B 3
3 C 4
Use window functions:
select id, name, value - min(value) over (partition by id) as
from t;
If you actually want to update the values, in MySQL, aggregation and join are probably the simplest solution:
update t join
(select id, min(value) as min_value
from t
group by id
) tt
on t.id = tt.id
set value = value - min_value
where min_value <> 0;

MySQL filter out subset by group by

1A
a b c
1 1 6
1 1 7
2 1 8
2 2 2
2 2 9
B
a b c
1 1 7
2 2 9
I want to filter out a subset of A
a b c
1 1 6
2 2 2
I am intend to join two tables by group by column a, b
such that to select the value in column c is less than the c value in table B, which is the desired subset.
But don't know how to implement this.
Try this:
SELECT A.* FROM A INNER JOIN B
ON A.a=B.b AND A.c<B.c;
See MySQL Join Made Easy tutorial.

Mysql selecting rows

I have a database scheme with a table like this:
id --- username --- sex
1 A 1
2 D 2
3 F 1
4 G 2
5 H 1
6 x 1
7 r 1
I want to select only 2 males and lets say 4 females, male is 1 and female is 2. How would we achieve that in one mysql query and if I have more var's to select by ?
(select * from your_table where sex = 1 limit 2)
union all
(select * from your_table where sex = 2 limit 4)

MySql subqueries and max or group by?

I have this table:
ID STUDENT CLASS QUESTION ANSWER TIME
1 1 1 1 c 12:30
2 1 1 1 d 12:36
3 1 1 2 a 12:38
4 2 1 1 b 11:24
5 2 1 1 c 11:26
6 2 1 3 d 11:35
7 2 3 3 b 11:24
I'm trying to write a query that does this:
For each STUDENT in a specific CLASS select the most recent ANSWER for each QUESTION.
So, choosing class "1" would return:
ID STUDENT CLASS QUESTION ANSWER TIME
2 1 1 1 d 12:36
3 1 1 2 a 12:38
5 2 1 1 c 11:26
6 2 1 3 d 11:35
I've tried various combinations of subqueries, joins, and grouping, but nothing is working. Any ideas?
You can use a sub-query to get most recent ANSWER per QUESTION, Then use this as a derived table and join back to the original table:
SELECT m.*
FROM mytable AS m
INNER JOIN (
SELECT STUDENT, QUESTION, MAX(`TIME`) AS mTime
FROM mytable
WHERE CLASS = 1
GROUP BY STUDENT, QUESTION
) AS d ON m.STUDENT = d.STUDENT AND m.QUESTION = d.QUESTION AND m.`TIME` = d.mTime
WHERE m.CLASS = 1
Demo here