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
SQL for Ordering by Number - 1,2,3,4 etc Instead of 1,10,11,12
SQL - Call Stored Procedure for Each Record
Why Don't Dbms's Support Assertion
Determine What User Created Objects in SQL Server
Opinions on Sensor/Reading/Alert Database Design
How to Retrieve the Current Value of an Oracle Sequence Without Increment It
Determine If One Coordinate Is in Radius of Another
SQL Row_Number() Function in Where Clause
What Is the Purpose for Using Option(Maxdop 1) in SQL Server
SQL Update Fields of One Table from Fields of Another One
Inserting into Oracle and Retrieving the Generated Sequence Id
Getting the Id of a Row I Updated in SQL Server
Getting Warning: Null Value Is Eliminated by an Aggregate or Other Set Operation
Functions with Variable Number of Input Parameters
Checking If a String Is Found in One of Multiple Columns in MySQL
Two Single-Column Indexes VS One Two-Column Index in MySQL
Use Variable with Top in Select Statement in SQL Server Without Making It Dynamic