A very typical approach to this type of problem is to use row_number()
:
select t.*
from (select t.*,
row_number() over (partition by number order by id) as seqnum
from t
) t
where seqnum = 1;
This is more generalizable than using a comparison to the minimum id. For instance, you can get a random row by using order by newid()
. You can select 2 rows by using where seqnum <= 2
.
Related Contents:
- How to select only the first rows for each unique value of a column?
- How to create a unique index on a NULL column?
- Eliminating duplicate values based on only one column of the table
- Produce DISTINCT values in STRING_AGG
- Get top 1 row of each group
- Solutions for INSERT OR UPDATE on SQL Server
- How to select all records from one table that do not exist in another table?
- How to insert a line break in a SQL Server VARCHAR/NVARCHAR string
- Select statement to find duplicates on certain fields
- Difference between numeric, float and decimal in SQL Server
- How to set variable from a SQL query?
- Convert Datetime column from UTC to local time in select statement
- Inserting data into a temporary table
- Change Schema Name Of Table In SQL
- How to calculate age (in years) based on Date of Birth and getDate()
- How do I use ROW_NUMBER()?
- Calculate a Running Total in SQL Server
- Real life example, when to use OUTER / CROSS APPLY in SQL
- Getting result of dynamic SQL into a variable for sql-server
- How to assign an exec result to a sql variable?
- How do I delete from multiple tables using INNER JOIN in SQL server
- How to print GETDATE() in SQL Server with milliseconds in time?
- How to force a SQL Server 2008 database to go Offline
- How to find third or nᵗʰ maximum salary from salary table?
- Rename a constraint in SQL Server?
- Group by minimum value in one field while selecting distinct rows
- Why does Sql Server keep executing after raiserror when xact_abort is on?
- Conditional WHERE clause in SQL Server
- SQL Server Invalid Column name after adding new column
- Create parameterized VIEW in SQL Server 2008
- SQL Join Table Naming Convention [closed]
- Efficient latest record query with Postgresql
- Limit on the WHERE col IN (…) condition
- Inner Joining three tables
- SQL Server find and replace specific word in all rows of specific column
- How to select data of a table from another database in SQL Server?
- How to get DATE from DATETIME Column in SQL? [duplicate]
- How to list files inside a folder with SQL Server
- How do I determine if a database role exists in SQL Server?
- Postgres Case Sensitivity
- Comparing two bitmasks in SQL to see if any of the bits match
- T-SQL: Selecting Column Based on MAX(Other Column)
- How do you strip a character out of a column in SQL Server?
- SQL Server Convert Varchar to Datetime
- Is there a opposite function to ISNULL in sql server? To do Is not null?
- CTE within a CTE
- How to get last 7 days data from current datetime to last 7 days in sql server
- Can I set ignore_dup_key on for a primary key?
- SQL Last 6 Months
- how to declare global variable in SQL Server..?