This question already has answers here:
SQL - Select first 10 rows only?
(12 answers)
Closed 2 years ago.
I have this table
| sale_id | amount |
| ------- | ------ |
| 5 | 3 |
| 1 | 2 |
| 3 | 1 |
And i need select JUST the sale_id of the max amount, just the number id 5 because 3 is the max amount. Sounds simple, but im having problems with this.
Can someone help me, please?
In standard SQL, this looks like:
select sale_id
from t
order by amount desc
fetch first 1 row only;
Not all databases support the fetch clause, but all have some mechanism for returning a result set with one row, such as limit or select top.
MS SQL dialect
select top 1 sale_id
from tbl
order by amount desc
In SQL server this can be achieved by:
SELECT TOP 1 sale_id FROM t order by amount desc
Incase you have duplicates max amount and need to fetch both sale_id then you can go for windowing function.
Related
This question already has answers here:
What is the default SQL result sort order with 'select *'? [duplicate]
(2 answers)
Closed 3 months ago.
I run the simplest query:
select id from table_xx where name='aaa'
toward this table:
table_xx
-----------------------------
| id | name |
-----------------------------
| 1 | aaa |
| 2 | bbb |
| 3 | aaa |
------------------------------
Note: id is a primary key.
so if I run code like this:
result = execute_query(query)
print (result[0][0])
Will it ALWAYS return the smallest id first in dataset? which is id=1
or is there a chance that id=3 will be returned as the first row in dataset
There is no "default order". You will only get a specific sort order if you use order by.
If there is no order by, the database is free to return the rows in any order it thinks is most efficient.
P.S. original detailed answer
Adding a sort clause to your sql query would guarantee this
This question already has answers here:
Fetch the rows which have the Max value for a column for each distinct value of another column
(35 answers)
Select First Row of Every Group in sql [duplicate]
(2 answers)
Return row with the max value of one column per group [duplicate]
(3 answers)
SQL Selecting dates with maximum sale for each department [duplicate]
(3 answers)
SQL: getting the max value of one column and the corresponding other columns [duplicate]
(2 answers)
Closed 1 year ago.
I am trying to write a query in Oracle which will return id of the driver who transported the most goods in range of a single goods category.
So far my query is as follows:
SELECT
truckset.driver_id, cargo.additional_liecence_id,
SUM(order_details.cargo_amount) as cargo_sum
FROM
order_details
INNER JOIN
truckset on truckset.order_id = order_details.order_id
INNER JOIN
cargo on order_details.cargo_id=cargo.id
WHERE
cargo.additional_liecence_id IS NOT NULL
GROUP BY
truckset.driver_id, cargo.additional_liecence_id
ORDER BY
SUM(order_details.cargo_amount) DESC;
And it returns:
| DRIVER_ID | ADDITIONAL_LICENCE_ID | CARGO_SUM |
| 14 | 8 | 174 |
| 17 | 8 | 144 |
| 7 | 5 | 70 |
| 11 | 5 | 50 |
| 7 | 6 | 50 |
while I expect something like this:
| DRIVER_ID | ADDITIONAL_LICENCE_ID | CARGO_SUM |
| 14 | 8 | 174 |
| 7 | 5 | 70 |
| 7 | 6 | 50 |
I don't have your database, so the syntax may not be exact, but you want to use SQL Analytics functions. You want to do something like this, where you can tweak the syntax in a live database:
WITH T AS (
SELECT truckset.driver_id, cargo.additional_liecence_id, SUM(order_details.cargo_amount) as cargo_sum,
ROW_NUMBER() OVER (
PARTITION BY driver_id, additinoal_liecence_id
ORDER BY cargo_sum
) AS ROW_NUMBER -- ADD THIS 'COLUMN' TO YOUR SELECT CLAUSE.
FROM order_details
INNER JOIN truckset on truckset.order_id = order_details.order_id
INNER JOIN cargo on order_details.cargo_id=cargo.id
WHERE cargo.additional_liecence_id IS NOT NULL
GROUP BY truckset.driver_id, cargo.additional_liecence_id
ORDER BY SUM(order_details.cargo_amount) DESC
)
SELECT * FROM T WHERE ROW_NUMBER = 1;
I wrote a whole page on SQL Analytics functions from the Oracle documentation when I taught them in graduate school. It is here:
http://www.qa76.net/sql_analytics
It is very nicely colorized, if I do say so myself :), which makes it much more readable then the original Oracle documentation from which it comes. About two thirds of the way down the page it discusses the ROW_NUMBER() function.
There's probably an even more elegant and expressive way to do this with different SQL Analytics functions, but this should get you started.
Enjoy!
(Edit: My thanks to 'MT0' in the comments for addressing the syntax issue. Changed it to use 'WITH' and filter on that.)
This question already has answers here:
Select top 10 records for each category
(14 answers)
Closed 2 years ago.
I have 10k - 1m goods wich are discribed by fields product_id, name, category, price. Which is the fastest way to fetched 10 most expensive goods from each category? Previously I checked this answer https://stackoverflow.com/a/176985/9513268.
My table:
-------------------------------------
|product_id| name | category| price |
-------------------------------------
| 1 |Phone | Gadgets | 599.99|
------------------------------------
| 2 |Jacket| Clothes | 399.00|
-------------------------------------
| ... | ... | ... | ... |
-------------------------------------
You can use window functions, as showned in the answer that you linked.
select *
from (
select t.*, rank() over(partition by category order by price desc) rn
from mytable t
) t
where rn <= 10
order by category, rn
The key is to properly define the over() clause of the window function. You want the top 10 by category, so this column goes to the partition by; you want the top most expensive goods, so the order by criteria is on descending price.
You can run the subquery separately and stare and the rn column to better understand the logic.
This question already has an answer here:
Access query producing results like ROW_NUMBER() in T-SQL
(1 answer)
Closed 8 years ago.
I have a table with some field.
name, stud_id
ali | 100
has | 230
mah | 300
I want to get some of record and show a row field beside of record.
1 | ali | 100
2 | has | 230
3 | mah | 300
How I can do it.
Thanks.
Select (select count(*) from Table1 A where A.stud_id>=B.stud_id) as RowNo, B.*
from Table1 as B
order by A.stud_id
MS-ACCESS does not have rownum function, so this might help you.
But your ID need to be sortable and unique.
This question already has answers here:
Select first row in each GROUP BY group?
(20 answers)
Closed 8 years ago.
I have a table with fields utm and tracking_id. There may be many tracking_ids for each utm.
So I need to select distinct utm and any one tracking id for each utm (let`s say thefirst one).
For example, we got the table:
utm | tracking_id
-------------------
ddff | 1
ddff | 2
llzz | 3
ddff | 4
ohoh | 5
ohoh | 6
And as an output i want to get:
utm | tracking_id
-------------------
ddff | 1
llzz | 3
ohoh | 5
I use PostgreSQL 9.1.
Is there a way to do it with SQL?
select utm, min(tracking_id)
from t
group by utm
if you have only one column, than simple aggregate is what you want, go with Clodoaldo Neto's advice.
If you have more than one columns, you can use dictinst on syntax:
select distinct on(utm)
utm, tracking_id, column1, column2, ...
from t
order by utm, tracking_id
sql fiddle demo