Django: groupby with getting first row - mysql

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;

Related

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

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

How to write a sql to generate same id column when any of three columns is the same?

We have a table T1 like below:
col1 col2 col3
1 1 1
1 2 1
1 2 3
4 4 4
We want to generate one more column, and then new table T2 is like below:
col1 col2 col3 id
1 1 1 1
1 2 1 1
1 2 3 1
4 4 4 2
The first three rows has at lease one same value in col1/col2/col3, so they have the same id 1. For example, row1 and row3 have same value 1 in col1, so they have the same id. The forth row don't have any same value with the first three row in col1/col2/col3, so it have a new id 2.
To be more percise, when T1 have one more column (4,2,4) like below, all rows have same id 1.
col1 col2 col3
1 1 1
1 2 1
1 2 3
4 4 4
4 2 4
My Idea:
1、We can join table T1 with itself to eliminate different rows
select * from T1 t11 join T1 t12 on t11.col1 = t12.col1 or t11.col2 = t12.col2 or t11.col3 = t12.col3.
t11.col1 t11.col2 t11.col3 t12.col1 t12.col2 t12.col3
1 1 1 1 1 1
1 1 1 1 2 1
1 1 1 1 2 3
1 2 1 1 1 1
1 2 1 1 2 1
1 2 1 1 2 3
1 2 3 1 1 1
1 2 3 1 2 1
1 2 3 1 2 3
4 4 4 4 4 4
2、Maybe we can distinct or group by the result above, but I don't kown how to do?
Can somebody help me out with this?
First of all, your table is lacking a fourth column which provides the suggested ordering of the records. For the purpose of this answer, I will assume that there exists an id column as follows:
id | col1 | col2 | col3
1 | 1 | 1 | 1
2 | 1 | 2 | 1
3 | 1 | 2 | 3
4 | 4 | 4 | 4
We can use analytic functions here:
WITH cte AS (
SELECT *, CASE WHEN col1 = col2 AND col2 = col3 AND col1 = col3
THEN 1 ELSE 0 END AS val
FROM yourTable
)
SELECT col1, col2, col3, SUM(val) OVER (ORDER BY id) AS new_col
FROM cte
ORDER BY id;
Demo

django queryset - mysql : how to get distinct item w.r.t two columns

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
id col1 col2 col3 col4 .....
11 B 4
10 F 3
9 D 3
7 D 2
5 B 1
3 A 3
2 A 1
How can i get this
i tried
Test.objects.all().order_by('-id').distinct('col1','col2')
But it says DISTINCT ON fields is not supported by this database backend
You can use annotate on the queryset to make a group by query like below:
from django.db.models import Max, Subquery
grouping_query = Test.objects.values('col1', 'col2').annotate(Max('id'))
print(grouping_query.query) # prints the sql query that gets executed
This will return the max of the id column for the col1-col2 combination.
Then use this in a subquery on Test model to get the other fields col3, col4 etc. like below:
Test.objects.filter(id__in=Subquery(grouping_query.values('id__max')))

update statement with aggregate function

Good day everyone..!:-)
I have this table tab where totalUsed is equal to the sum of all used values referenced to name
cid name used total
1 a 1 1
2 a 3 4
3 a 6 10
4 b 3 3
5 b 7 10
6 b 10 0
7 a 5 0
i have this code but it only copy totalUsed's adjacent used value
UPDATE tab
SET totalUsed=
(
SELECT SUM(used)
)
cid name used total
1 a 1 1
2 a 3 3
3 a 6 6
4 b 3 3
5 b 7 7
6 b 10 10
7 a 5 5
if used is set as 10 for cid 6, totalUsed should be 20
and for cid 7 it should be 15.
how to do it in mysql?
it should look like this.
cid name used total
1 a 1 1
2 a 3 4
3 a 6 10
4 b 3 3
5 b 7 10
6 b 10 20
7 a 5 15
thanks for help
:-)
In most dialects of SQL, you can do this using an update with a correlated subquery:
UPDATE tab
SET totalUsed = (SELECT SUM(used)
from tab tab2
where tab2.name = tab.name and
tab2.cid <= tab.cid
);
EDIT:
The above will not work in MySQL. You can do this instead:
UPDATE tab join
(select tab2.cid,
(SELECT SUM(used)
from tab tab3
where tab3.name = tab2.name and
tab3.cid <= tab2.cid
) as cum_used
from tab tab2
) tab2
on tab.cid = tab2.cid
SET tab.totalUsed = tab2.cum_used;

TABLE 1 data needs to populated to table2 by grouping

I have
TABLE 1
col1 col2 col3 col4
1 1 1 AP
1 2 1 MP
4 2 1 MP
1 3 1 AP
2 2 2 JP
2 4 2 JP
8 7 2 LP
8 7 2 LP
8 8 3 HP
2 4 3 HP
3 9 3 ZP
6 9 3 ZP
I have a requirement that i have move data from table 1 to Table 2 by satisfying requirements
TABLE2
col1 col2 col3 col4
2 3 1 AP
5 2 1 MP
4 4 2 JP
16 7 2 LP
10 8 3 HP
9 9 3 ZP
where values col3 are grouped in such a way that we should sum all the values of col1 and where as max value of col2 needs to be populated
Any answers or clues will be appriciated
INSERT INTO table2
SELECT SUM(col1),MAX(col2),col3
FROM table1
GROUP BY col3;
Does this work?
select sum(col1) col1, max(col2) col2, col3 col3
into table2
group by col3
Can you try with nosid's statement? Just make sure to add col4 in the select and group by statements. Does that work?
INSERT INTO table2
SELECT SUM(col1),MAX(col2),col3, col4
FROM table1
GROUP BY col3,col4;