Sql server select rows by distinct column

sql server select rows by distinct column SELECT a.* FROM emails a INNER JOIN (SELECT email, MIN(id) as id FROM emails GROUP BY email ) AS b ON a.email = b.email AND a.id = b.id;
sql server select rows by distinct column SELECT a.* FROM emails a INNER JOIN (SELECT email, MIN(id) as id FROM emails GROUP BY email ) AS b ON a.email = b.email AND a.id = b.id;
sql server query for datediff — DOES NOT ACCOUNT FOR LEAP YEARS DECLARE @date1 DATETIME, @date2 DATETIME, @result VARCHAR(100); DECLARE @years INT, @months INT, @days INT, @hours INT, @minutes INT, @seconds INT, @milliseconds INT; SET @date1 = ‘1900-01-01 00:00:00.000’ SET @date2 = ‘2018-12-12 07:08:01.123’ SELECT @years = DATEDIFF(yy, @date1, @date2) IF DATEADD(yy, [email protected], @date2) < […]
sql last week select min(date), max(date) from calendar where week = datepart(week, getdate() – 7) and year = datepart(year, getdate() – 7) sql select data from last week select id from tbname where date between date_sub(now(),INTERVAL 1 WEEK) and now();
how to use rank function in sql BY LOVE SINGH, Here, name and id is the column name of the table tbltest. SELECT name, RANK () OVER ( ORDER BY id DESC ) price_rank FROM tbltest; sql how to partition rank SELECT * FROM ( SELECT product_id, product_name, brand_id, list_price, RANK () OVER ( PARTITION […]
sql drop with dependencies DROP TABLE tableName CASCADE; — Table can also be schema or whatever else.