Search
 
SCRIPT & CODE EXAMPLE
 

SQL

distinct For only one column

SELECT *
  FROM (
                SELECT  ID, 
                        Email, 
                        ProductName, 
                        ProductModel,
                        ROW_NUMBER() OVER(PARTITION BY Email ORDER BY ID DESC) rn
                    FROM Products
              ) a
WHERE rn = 1
Comment

choose only one for each distinct collumn regardless of other columns

WITH cte AS
(   SELECT *, ROW_NUMBER() OVER (PARTITION BY id ORDER BY val1 DESC) AS rn
    FROM MyTable
)
SELECT *
FROM cte
WHERE rn = 1
Comment

select other columns with distinct

SELECT * FROM table
WHERE id IN (
  SELECT MAX(id) FROM table GROUP BY name
)
Comment

PREVIOUS NEXT
Code Example
Sql :: copy a table mysql 
Sql :: power bi union columns 
Sql :: mysql insert rows to another database 
Sql :: mysql select case insensitive 
Sql :: best sql collation 
Sql :: foreign key on table oracle 
Sql :: mysql switch case 
Sql :: oracle undo tablespace schema 
Sql :: make parameters nullable in sql server 
Sql :: Oracle filter date column by year 
Sql :: mysqldump 1 table only 
Sql :: full outer join postgres 
Sql :: Create table with JSON column SQL Server 
Sql :: sqlite update query python 
Sql :: group_concat mysql 
Sql :: declare variable in mysql 
Sql :: ERROR: permission denied for table accounts postgresql 13 
Sql :: convert minutes to hours sql 
Sql :: forgot postgres password 
Sql :: sql float 3 decimal places 
Sql :: time in sql server 
Sql :: how to check common records in 2 table 
Sql :: sql server fn_dblog 
Sql :: show sql property syntax for jpa. 
Sql :: Host ' is not allowed to connect to this MySQL server 
Sql :: mssql unique key accept nulls 
Sql :: illuminate database queryexception could not find driver (sql select * from 
Sql :: mysql shell 
Sql :: merge command in sql 
Sql :: postgres create trigger if not exists 
ADD CONTENT
Topic
Content
Source link
Name
7+7 =