Questions tagged [greatest-n-per-group]
Query the row with the greatest/least value per group.
3,946
questions
1
vote
2
answers
39
views
Get the top value row of a table without id
So i have this table:
req_num
number
status
order
1254
5
7
1
1254
4
7
2
1254
6
7
3
1246
7
8
1
1246
5
8
2
1246
3
8
3
1253
1
9
1
1253
4
7
2
1253
7
4
3
1321
7
4
1
1321
8
4
2
1321
7
4
3
1321
...
-2
votes
0
answers
19
views
SELECT FIRST 3 DISTINCT ORACLE 11g PLSQL [duplicate]
I have a table with 3 columns: id (PK), name, date
I only have 3 different names but the table is +1k records. So I need to get those 3 names ordered by date desc. But when I use the DISTINCT clause ...
-1
votes
3
answers
74
views
Show only the max id record outa of a GROUPED BY list
(Simplified example). I have this table:
USER ID | code | sales
1 x 100
1 y 200
1 z 150
2 x 300
2 z ...
-1
votes
1
answer
79
views
LEFT JOIN to return only first row
I am making a join with two tables, tab_usuarios (users) and tab_enderecos (address).
tab_usuarios structure:
id_usuario
nome
usuario
1
Administrador
admin
2
Novo Usuário
teste
3
Joao Silva
jao
...
-1
votes
3
answers
70
views
Get only max values from table for each ID record [duplicate]
I am having table like this, where id is repeating several times.
I need to get only max L1 value for each ID, so correct result from the query would be:
L1
ID
VALUE_VARCHAR
1
224754148
xDSL 1 - ...
0
votes
0
answers
23
views
MySQL get top 3 rows in for each group of results [duplicate]
This is a similar question to this question, but differs slightly.
In the simplest way possible, I have a table that comprises of the following data:
| id | post_id | time_stamp |
|----|-----...
0
votes
2
answers
42
views
Select the latest records (i.e. ORDER by) when IDs IN multiple values
For example, a devices table like this
id
user_id
last_used_at
1
111
2000-01-01 00:00:00
2
111
2003-01-01 00:00:00
3
222
2000-01-01 00:00:00
4
222
2003-01-01 00:00:00
select the last used device ...
0
votes
0
answers
57
views
How to select only values for MAX(DATE) [duplicate]
I have done similar SQL queries in my opinion already before but this is somehow impossible although sounds easy... I hope you can help me in this, I have checked multiple answers for relating ...
-2
votes
0
answers
26
views
Get user email corresponding to latest log entry for every distinct entry [duplicate]
There are 3 tables, namely:
Employees:
ID
Name
Gender
1
abc
male
2
def
female
3
ghi
male
4
mno
male
5
rms
female
employee_log: it's a table which will have the log entry for entries/...
0
votes
3
answers
84
views
Problem with SQL query,highest amount of something
Hi i have this problem where i need to do this:
-For each trainer who has given only Yoga lessons, show the name, the surname, and the city of the gym where he has given the highest number of lessons.
...
0
votes
1
answer
38
views
Fetching Top 25 Rows per Column 1's records [duplicate]
My query is as follows:
SELECT
ACCOUNT_NAME, BRAND,
SUM(SALES_YTD) AS YTD,
SUM(SALES_YTD_YA) AS YTD_YA
FROM
ACCOUNTS
WHERE
CHAIN_NAME = 'LIQUOR_DEPOT'
GROUP BY
...
-1
votes
1
answer
38
views
how to select from case name in postgresql
I use postgres v14,
I create a postgres view it contain 5 time columns.
Every user id have a date in side one column
it mean every userid have more than 4 rows
i want to get one row have greatest row
...
0
votes
1
answer
52
views
Single record of greatest n per group from a table with three identifiers
I have a table in which each record is a single purchase made by a single client.
Purchases can be made in different categories of product and different geographical areas (each of these is a single ...
0
votes
2
answers
46
views
How can I determine the average of last 3 values for each user?
Hello the question I'm working on is:
Determine for each customer the average of the last three order values.
I've tried this
SELECT AVG(value) AS the_average
FROM (SELECT value FROM sales ORDER BY ...
0
votes
0
answers
12
views
MySQL aggregate function for the last value [duplicate]
Lets suppose I have this table Purchases:
+----+------+-------+---------------------+
| ID | User | Price | Date |
+----+------+-------+---------------------+
| 1 | Bob | 35.0 | 2022-...
0
votes
1
answer
88
views
Selecting rows with max value, in correlation to two other columns
I have a table with three columns: region, country, count.
I want to reduce my table to rows of region, country and count - where count is maximal among the region.
For example, if I have the ...
0
votes
1
answer
20
views
unable to fetch wanted querie in postgres
Suppose I've next data
id cid vid firstname lastname dtype dname createdat
1 32 34 ramesh kjk t check 2021-11-02 10:00:51.66395
1 32 34 ramesh kjk t check 2021-11-01 10:...
0
votes
1
answer
65
views
Get only latest records of each ID in PostgreSQL
I have two tables
accounts table
account_id location_id
1 11
1 12
2 21
2 22
Events_table
location_id events_id event_date
...
0
votes
3
answers
121
views
How do I return only the last score a student has received by date?
I have been trying to figure out how to gather the Last Score a student has received on a yearly test, but the only promising way I've been able to come up with is using the MAX function. Where it ...
0
votes
2
answers
53
views
Merging SQL tables keeping the latest rows
I have an existing SQL table a bit like this
Question
Answer
AnsWhen
AnsWho
Q1
Red
2022-03-01
Rod
Q1
Green
2022-03-20
Freddie
Q2
Blue
2022-04-05
Jane
Q2
Yellow
2022-04-01
Rod
Q3
Green
2022-04-05
...
0
votes
2
answers
78
views
How to evaluate rows and get the max value based on multiple columns
I have a SQL question where I want to evaluate and get the latest work order for a location based on multiple criteria
The table looks something like this
location work order create dt. ...
0
votes
0
answers
12
views
SELECT THE NEWEST RECORD IN GROUP MYSQL [duplicate]
I can't figure out how I would select data for the record with the newest social.id aka sid while grouping by t1.id. I have tried making a select in select, ordering, grouping by t1.id having social....
0
votes
1
answer
56
views
SQL Top N per group each year without using row_number / rank or variables [duplicate]
I have a limitation of not being able to use window functions or variables due to tool constraints. (Sorry couldn't elaborate much) but is there any scalable way to get Top N per group for each year? ...
-2
votes
2
answers
115
views
Select Only the Most Recent Row [duplicate]
I'm using a query to pull the valuation cost of projects. It works well, except sometimes it returns multiple rows for a valuation.
How can I modify my query to get only the most recent record to ...
2
votes
5
answers
104
views
How can I retrieve distinct data by using group by
I have following table and its sample data as follows,
+------------------+-------+--------+---------------------+-------+
| PhoneCode | SeqID | Active | Token | CUSID |
+---------...
0
votes
1
answer
59
views
DISTINCT nor GROUP BY doesn't work to return unique values
SELECT DISTINCT order_reference as barcode,
mth.location as location,
mth.last_update_ts as updated
FROM sales_order_header soh
INNER JOIN manufacturing_tracking_history mth
ON soh.id = ...
0
votes
0
answers
11
views
SQL: for each unique value in column a, get the latest value of column b, sorted by column c [duplicate]
Say I have a table with 3 columns: "Make", "Model", "SaleDate". I want to know, for each make, the most recent model that was sold. In other words "For each unique ...
1
vote
2
answers
83
views
ORACLE SQL: select MAX value based on 2 different variables (group by function) [duplicate]
I think my problem is quite simple but I don´t really get it :)
I'm using SQL Developer as IDE and have a large table which looks like this:
ID
technology
speed
1
3G
20
1
2G
10
1
4G
40
1
5G
100
...
1
vote
1
answer
78
views
Only return rows with local max [duplicate]
The code below gives me something similar to the table below. What I am looking to do is only return the PROVID that has the max count per PATID.
SELECT PAT_ID AS PATID
, VISIT_PROV_ID AS PROVID
...
-2
votes
1
answer
55
views
Good way to 'join' and 'max' in mysql [duplicate]
i'm a web developper and i'm a bit lost in one of my queries which seems to be simple...
here is the point, i have a "step" table, where i can see what document a guy can upload in my site.
...
0
votes
2
answers
126
views
most efficient way to select duplicate rows with max timestamp
Suppose I have a table called t, which is like
id content time
1 'a' 100
1 'a' 101
1 'b' 102
2 'c' 200
2 'c' 201
id are duplicate, and for the same id, ...
0
votes
0
answers
72
views
Get latest message from chat conversations
I have this table which contains all messages created by users:
Schema::create('messages', function (Blueprint $table) {
$table->id();
$table->text('text');
$table->foreignIdFor(...
-1
votes
1
answer
112
views
Select MAX of each corresponding row [duplicate]
new to SQL but I'll try to be clear,
I have 3 table, which have corresponding key columns. I need to select the row the MAX Value of a set column, for EACH of it's corresponding column.
Table1
ID1 ...
1
vote
1
answer
60
views
How to aggregate date by minutes in postgres having latest row
I am trying to aggregate data by minute while having multiple records for certain minute, but I am interested in only last record. For example :
id | date | data ...
0
votes
0
answers
39
views
Picking duplicates with a higher column value [duplicate]
In SQL Server, I have a table that includes a list of duplicate tasks. Those tasks have the same id and name, but each one of them has a different value in a third column. How can I pick duplicates ...
0
votes
0
answers
19
views
getting the biggest and second biggest id per client in mysql [duplicate]
I have a table that contains the ids of receipts, each associated with a the id of a client. I have been trying for a while now, to get the biggest id and the second biggest id of the receits ...
0
votes
1
answer
49
views
Kindly anyone tell me the logic for oracle sql [duplicate]
User requirement:- 2 policies in each product code with Highest (Maximum) SUM INSURED
How do I get 2 policies in each product code with Highest (Maximum) SUM INSURED
SELECT
PROD....
0
votes
1
answer
87
views
Get last record by month/year and id
I need to get the last record of each month/year for each id.
My table captures daily, for each id, an order value which is cumulative. So, I need that at the end I only have the last record of the ...
0
votes
1
answer
91
views
SQL Server : get columns based on MAX(SUM()) of other column [duplicate]
I've ran into trouble trying to select columns based on the max of a sum of a column. I'm using SQL Server.
I have the following three tables (simplified)
DimensionTable
SupplierID
ProductID
Revenue
...
0
votes
1
answer
44
views
How to write SQL Query with group by and having [duplicate]
I have this table which has three columns: number, status and unix_time
number,status,Unix_time
01,Y,112
02,Y,112
01,Y,114
03,,114
01,,115
02,Y,115
04,Y,115
04,Y,119
05,Y,119
06,Y,120
05,Y,120
06,,121
...
0
votes
4
answers
172
views
Get only rows where data is the max value
I have a table like this:
treatment | patient_id
3 | 1
3 | 1
3 | 1
2 | 1
2 | 1
1 | 1
2 | ...
0
votes
1
answer
62
views
How to select latest value for each day in Oracle? [duplicate]
I have a table like this:
How do i find latest value of each day in oracle with sql?
I try:
select max(datetime), amount
from ORDER_ITEMS
group by datetime, amount
But it does not give a right ...
0
votes
2
answers
51
views
SQL QUERY to get previous date
PERIOD_SERV
PERSON_NUMBER DATE_sTART PERIOD_ID
10 06-JAN-2020 192726
10 04-APR-2019 12827
11 01-...
1
vote
1
answer
50
views
How to limit items inside a group in pgsql [duplicate]
I need to select data grouped by external_id and resolution and ordered by timestamp but limited to first two ids in each group. I don't know how to do the latter.
I tried to do something with simple ...
-3
votes
1
answer
64
views
Select only unique rows based on the first two columns
Here is a snippet of the table I am working with:
My goal is to only select one of these highlighted rows (doesn't matter which). They have duplicate approval, amount, and type columns but unique ...
-1
votes
1
answer
66
views
Selecting only the highest value in Oracle SQL [duplicate]
I have a query where I might get only one or possibly multiple records for each CONTROLNBR + LINENBR combo -
SELECT ACCTNBR, BOOKDATE, CONTROLNBR, LINENBR, TRANDETSEQ,
ING1, ING1AMT, ING2, ING2AMT, ...
0
votes
0
answers
59
views
Subquery to get the most recent row (based on timestamp) grouped by a single column value, MySQL [duplicate]
I've have several tables in a MySQL database that have rows with some duplicate column values and with a timestamp/created_at column for everyrow. As an example, I have an orders table that has a ...
0
votes
2
answers
55
views
SQL - Returning fields based on where clause then joining same table to return max value?
I have a table named Ticket Numbers, which (for this example) contain the columns:
Ticket_Number
Assigned_Group
Assigned_Group_Sequence_No
Reported_Date
Each ticket number could contain 4 rows, ...
0
votes
1
answer
32
views
Fetch record with max number in one column except if date in that column is > than today
I have a problem with fetching few exceptions from DB.
Example, table b:
sn
v_num
start_date
end_date
1
001
01-01-2019
31-12-2099
1
002
01-01-2021
31-01-2022
1
003
01-02-2022
31-12-2099
2
001
01-...