how to select first row order by date from a group by

mysql select first record of each group
select first row of each group sql server
oracle select first record in group
sql select most recent record for each id
oracle select top n rows for each group
sql select top 1 from each group
sql earliest date for each id
sql group by first occurrence

I have a table like:

City       date      Person 
A        2018/05/01   peter
A        2018/03/01   Jack
B        2018/02/16    TOM
C        2018/03/01   Mike

Now I want to find the earliest person for each city. The result will be

A        2018/03/01   Jack
B        2018/02/16    TOM
C        2018/03/01   Mike

How to do it?


You could use a subquery with min date for city in join with you table

select m.* from my_table m
inner join (
    select city, min(date) as min_date 
    from my_table  
    group by city 
) t  on t.city = m.city and t.min_date = m.date 

SQL: How do I use group by and order by together?, , we can use ORDER BY clause with desc (descending) property and Limit 1. Let us first create a table and insert some records with the help of insert command. The query is as follows. After creating the above table, we will insert records with the help of insert command. SELECT * FROM SALES ORDER BY SaleDate, Salesperson ; This example orders the rows first by SaleDate and then by Salesperson. All these ordering examples are in ascending (ASC) order, which is the default sort order. The last SELECT shows earlier sales first — and, within a given date, shows sales for ‘Adams’ before ‘Baker’.


You can use TOP 1 WITH TIES

select top 1 with ties
   City
  ,date
  ,Person
from MyTable
order by row_number() over (partition by City order by date asc)

SQL Fidddle

How to select last row in MySQL?, ; You could also use the analytic functions RANK and/or DENSE_RANK, but ROWNUM is probably the easiest. Selecting the one maximum row from each group. Let’s say I want to select the most recent log entry for each program, or the most recent changes in an audit table, or something of the sort. This question comes up over and over on IRC channels and mailing lists. I’ll re-phrase the question in terms of fruits.


Using First_Value () In Sql server

;WITH CTE(City,[date],Person )
AS
(
SELECT 'A','2018/05/01','peter' UNION ALL
SELECT 'A','2018/03/01','Jack'  UNION ALL
SELECT 'B','2018/02/16','TOM'   UNION ALL
SELECT 'C','2018/03/01','Mike'
)
SELECT DISTINCT City, 
    FIRST_VALUE([date])OVER (PARTITION BY City ORDER BY [date]) AS [date],
    FIRST_VALUE(Person)OVER (PARTITION BY City ORDER BY [date])AS Person
FROM CTE

Result,Demo: http://rextester.com/DLPE49383

City    date        Person
--------------------------
A     2018/03/01    Jack
B     2018/02/16    TOM
C     2018/03/01    Mike

How do I do top 1 in Oracle?, In this article we will show you, How to Select First Row in each SQL Group By we sort the partitioned data in the descending order using their [yearly income]. "Pick the row with the smallest id from each group sharing the highest total." To order results in a way that disagrees with the sort order determining the first per group, you can nest above query in an outer query with another ORDER BY.


You can use this approach, it will work faster.

select City,[date],Person from CTE a where [date] = (select min([date]) from CTE as b where b.City = a.City);

Select First Row in each SQL Group By group, How to find the oldest or most recent record associated with a And let's say you need to display a list of users next to the date of their most recent order. SELECT user_id, MAX(created_at) FROM orders GROUP BY user_id. ASP.NET Forums / Data Access / ADO.NET, Entity Framework, LINQ to SQL, NHibernate / LINQ query to get the top 1 from each group by first date LINQ query to get the top 1 from each group by first date [Answered] RSS


select City, min(date) as date, Person 
from T
group by City, Person

Finding the Oldest/Youngest Records Within a Group, WITH ORDERED AS ( SELECT ID , Val , kind , ROW_NUMBER() OVER (​PARTITION BY Val ORDER BY Kind ASC) AS rn FROM mytable )  Hi Tom I have just begun using analytic functions, but have come up short on this: In a query where I group by a field, I would like to select the first values from a specific row.


How to select the first row of each group?, Selecting the First Row for each Group. Sometimes you just want to graph We want a table with one row per date with the highest count, like this: row_number() over (partition by dt order by ct desc) row_num. We'll plug it  The select first n rows only selects the first n rows. In the following example, there's an ambiguity: the third row might be where the num 3 is translated to german ( drei ) or where it is english ( three ).


Selecting Only One Row Per Group, For efficient execution, the ranking must be done with a pipelined order by . The simplest way to fetch only the first rows of a query is fetching the required rows the statement before fetching all rows so it can select the best execution plan. SELECT DISTINCT ON with ORDER BY. The undecorated SELECT DISTINCT ON selects one row for each group but you don’t know which of the rows will be selected. Each execution of the query can return different rows. Use the ORDER BY clause if you want to select a specific row. SELECT DISTINCT ON (location) location, time, report FROM weather_reports ORDER BY location, time DESC;


Top-N queries: fetch only the first N rows, For this reason, the order of rows in the result set is undetermined or In case the SELECT statement contains a WHERE clause, the ORDER BY clause The following statement retrieves the employee id, first name, last name, hire date, and SQL CROSS JOIN · SQL GROUP BY · SQL GROUPING SETS · SQL ROLLUP  SQL to return the first record in a group. There are at least two different approaches which can be taken to return the first record in a group - in our working example the date when each page was first viewed and the IP address associated with that event. The first approach is to use an embedded select.