I have records in a table like that
ID Name priority
1 MyString 12
2 Search 20
3 MyString 50
4 MyString 10
5 Search 7
I want to get distinct rows with highest priority value. For example, the above example should give the following result
ID Name priority
2 Search 20
3 MyString 50
I was going through the docs and found out that distinct on columns cannot be found out on mysql. So I tried to perform a group-by and sort (descending on priority column).
I tried this
model_name.objects.all().values('name','priority','id').annotate(Count('search_name')).order_by('-priority')
But I am not getting the desired result. Is it possible to do this in a single orm query.
I am using django 1.6 and mysql as my database.
Have you tried adding the .distinct() method to your query? Django ORM has the distinct() method. I hope this helps you:
https://docs.djangoproject.com/en/dev/ref/models/querysets/#distinct
Related
Environment: MySQL 5.6
SqlTable name = CategoryTable
Sql Columns
CATEGORY_ID (INT)
CATEGORY_NAME (VARCHAR)
LEVEL (INT)
MOTHER_CATEGORY (INT)
I've tried with
SELECT
CATEGORY_ID, CATEGORY_NAME , LEVEL , MOTHER_CATEGORY
FROM
CategoryTable
But I don't know how to use the ORDER BY in order to get that result.
So the first line here are the columns, and from the second lines, there start the table content:
CATEGORY_ID CATEGORY_NAME LEVEL MOTHER_CATEGORY
1 MainCategory 0 0
2 -SubCategory1 1 1
3 --SubCategory2 2 2
4 ---SubCategory3 3 3
5 2Nd_Main_Category 0 0
6 -SubCategory1 1 5
7 --SubCategory2 2 6
8 ---SubCategory3 3 7
is there a way to achieve something like this with a mysql query?
You aren't very clear in what you are trying to achieve. I'll take a guess that you want to order using a multi-level parent child structure. there are some very complicated ways of handling such a feat within mysql 5.6, a DB that's not really ideal for such a structure, but I have come up with something simple myself that I use in my own apps. you create a special ordering field that creates a path of zero filled ids for each record.
ordering_path_field
/
/0000000001/
/
/0000000001/0000000002
/0000000003
/0000000003/0000000005
/0000000003/0000000005/0000000006
etc
so each record contains a path of each parent up to the root, using zero filled ids. then you can just sort by this field to get them in proper order. the drawbacks being that you'll have to set a max number of levels allowed, so that the ordering fields doesn't overflow, and also, moving a record to a new parent if ever needed would be a big pain.
I have a column of data, e.g. as follows:
select league_id from leagues
This gives me a single column (league_id) and 100+ rows for that column.
I want to convert it into a single cell (1 row, 1 column) with the following structure:
[1001, 1002, 42022, 203412, 24252, etc..]
Essentially converting the rows into one big array.
There must be a way of doing it but can't see how.
I'm using MariaDB 10.2.
You can use the GROUP_CONCAT() function for that.
Usage is straightforward:
id
val
1
1001
2
1002
3
42022
4
203412
5
24252
SELECT group_concat(val)
FROM tab
gives you
group_concat(val)
1001,1002,42022,203412,24252
See db<>fiddle.
(Note: Before MariaDB 10.3.3 you cannot use the LIMIT clause with GROUP_CONCAT, in case you should need that).
Transport table
id name
1 T1
2 T2
Pallets table
id name
1 P1
2 P2
Transport Pallet Capacity table
id transport_id pallet_id capacity
1 1 1 10
2 1 2 null
3 2 1 20
4 2 2 24
How to generate table like this:
id transport_id pallet_id_1_capacity pallet_id_2_capacity
1 1 10 null
2 2 20 24
Problem: pallets and transports can be added, so, neither quantity is known in advance.
For example, manager adds another pallet type and 'pallet_id_3_capacity' column should be generated (and can show null if no capacity data is yet available).
Another manager can fill 'transport pallet capacity' table later when notified.
Is there a way to build sql in mysql that will care about the above: specifically - dynamic number of pallets?
The SQL select-list must be fixed at the time you write the query. You can't make SQL that auto-expands its columns based on the data it finds.
But your request is common, it's called a pivot-table or a crosstab table.
The only solution is to do this in multiple steps:
Query to discover the distinct pallet ids.
Use application code to build a dynamic SQL query with as many columns as distinct pallet id values found in the first query.
Run the resulting dynamic SQL query.
This is true for all SQL databases, not just MySQL.
See MySQL pivot row into dynamic number of columns for a highly-voted solution for producing a pivot-table query in MySQL.
I am not voting your question as a duplicate of that question, because your query also involves transport_id, which will make the query solution a bit different. But reading about other pivot-table solutions should get you started.
I have a query:
select lr2.event_id as ce
from data_requests as lr2
group by lr2.event_id,
that returns 88 rows. Then I tried the following:
select count(lr2.event_id) as cc, lr2.event_id as ce
from data_requests as lr2
group by lr2.event_id
but it only returned 25 rows, so I am really puzzled, where did other 63 rows go.
I tried it in sqlfiddle, it seems to work correctly, but on my server it just doesn't, so it must be a setting or something... Feels like the server calculates the count after it select a subset of all group results. weird.
if you want to count the number of rows for each lr2.event_idyou must use count(*) , not count(lr2.event_id) . Remember, you are counting rows.
Function of GROUP BY
The SQL GROUP BY clause is used in collaboration with the SELECT statement to arrange identical/similar/equal data into groups.
Demonstration
If I have table like below, then 1st query will give same output as table definition:
ce
--
1
2
3
4
5
And 2nd query will give output as,
cc |ce
--- ---
1 1
1 2
1 3
1 4
1 5
Since, all are distinct in Table, I got 5 rows! but If some ce values are repetitive as,
ce
--
1
2
1
2
2
then, 2nd query will give output as:
cc |ce
--- ---
2 1
3 2
And here If I get shocked where did other 3 rows go? Then I need to study!
Of course, it's a spoon feeding! OP needs to study about GROUP BY in SQL.
My bad, it seems to be a phpmyadmin problem, I run the query in the phpmyadmin, and it auto added a limit in the end of every query
how to run query select "sub" grouped by "cat" to return something like this:
SQL query:
select sub
from post
where cat = 1
group by id
to return something like:
3,4,9,14,33,22
table "post"
id cat sub
1 1 3,4,9,14
2 2 1,2
3 2 4,5
4 1 33,22
5 3 1,4
thanks,
It is a very bad idea to store lists of things in character strings. For one thing, your ids are integers, but the strings are characters. More importantly, SQL has a great data structure for storing lists -- it is called a table. You should be using a junction table.
But, sometimes you are stuck with the data you have. In that case, you can use group_concat():
select group_concat(sub)
from post
where cat = 1;