How to Select Records Without Duplicate on Just One Field in SQL

How to select records without duplicate on just one field in SQL?

Try this:

SELECT MIN(id) AS id, title
FROM tbl_countries
GROUP BY title

How to select records without duplicates with the latest record in one field in SQL?

You can use EXISTS in the WHERE clause with a subquery only to only select the highest IDs:

SELECT Suppliers.[Supplier Name] AS Publisher,
[Software Details].Vendor,
[Software Details].ID AS MaxOfID
FROM Suppliers
RIGHT JOIN [Software Details] ON Suppliers.ID = [Software Details].Publisher
WHERE EXISTS
(SELECT 1
FROM Suppliers suppl
RIGHT JOIN [Software Details] sd ON suppl.ID = sd.Publisher
WHERE suppl.[Supplier Name] = Suppliers.[Supplier Name]
AND sd.Vendor = [Software Details].Vendor
HAVING Max(sd.ID) = [Software Details].ID)

How to select records without duplicate on just one field and duplicate field total in SQL

Try this to get the min id and sum of prices:

SELECT 
min(id) as ID, category, SUM(price)
FROM
expenses

GROUP BY category
ORDER by ID

SQLFiddle Example

How to select records without duplicate

If you need only distinct rows, you can use the distinct keyword:

select distinct * from charts

However, if you need distinct values for a single column, you need to use a group by in your clause, depending what column needs distinct values and agregate the other ones.

How to select rows without duplicates and partition only by some columns?

You can use ROW_NUMBER() to do this. Essentially what you have posted for your grouping instead becomes your partition, and you can then chose how each group is sorted (in the case of the below salary):

select *
from (select *,
row_number() over(partition by firstname, lastname, country
order by salary desc) AS rownum
from employee) AS e
where e.rownum = 1;

T-SQL How to select rows without duplicate values from one column?

You need to GROUP BY:

SELECT MAX(ID) as [ID], ID_PROJ_CSR
FROM my_table
GROUP BY ID_PROJ_CSR

SQL - select values from two tables but without duplicates

If you have several repeated rows you could try using DISTINCT

SELECT distinct uc.*,ul.NAME FROM UC AS uc
inner JOIN UL AS ul ON uc.PARTY_ID = ul.PARTY_ID
WHERE PID = '33322';

but based on your sample
if you want the result of all the names in a rows you could use group_concat

SELECT uc.*, group_concat(ul.NAME)
FROM UC AS uc
inner JOIN UL AS ul ON uc.PARTY_ID = ul.PARTY_ID
WHERE PID = '33322'
group by uc.id;

otherwise if the name value is not relevant you coul try using a fake aggregation function

SELECT uc.*, min(ul.NAME)
FROM UC AS uc
inner JOIN UL AS ul ON uc.PARTY_ID = ul.PARTY_ID
WHERE PID = '33322'
group by uc.id;


Related Topics



Leave a reply



Submit