How to use DISTINCT in WHERE CLAUSE to avoid repeated row returns
You should apply group by
clause.
SELECT
baseFeeds.siteURL, feedFinishes.timeTaken, feedFinishes.timeSubmitted
FROM feedFinishes
JOIN baseFeeds
ON feedFinishes.GUID = baseFeeds.GUID
WHERE baseFeeds.siteURL LIKE '%www.example.com%'
GROUP BY
baseFeeds.siteURL, feedFinishes.timeTaken, feedFinishes.timeSubmitted
SQL: distinct in where clause
You don't state RDBMS. One way that should work in all.
SELECT Name,
LasName,
MAX(City) AS City
FROM YourQuery
GROUP BY Name,
LasName
You say you don't care which city. This gives you the last alphabetically.
Or an alternative
SELECT Name,
LasName,
City
FROM (SELECT Name,
LasName,
City,
ROW_NUMBER() OVER (PARTITION BY Name, LasName ORDER BY (SELECT 0)) AS RN
FROM YourQuery) T
WHERE RN = 1
Is there a way to ensure WHERE clause happens after DISTINCT?
You can do this without using a subquery using LEFT JOIN
:
SELECT c.id, c.comment_id_no, c.text, c.show, c.inserted_at
FROM Comments AS c
LEFT JOIN Comments AS c2
ON c2.comment_id_no = c.comment_id_no
AND c2.inserted_at > c.inserted_at
WHERE c2.id IS NULL
AND c.show = 'true';
I think all other approaches will require a subquery of some sort, this would usually be done with a ranking function:
SELECT c.id, c.comment_id_no, c.text, c.show, c.inserted_at
FROM ( SELECT c.id,
c.comment_id_no,
c.text,
c.show,
c.inserted_at,
ROW_NUMBER() OVER(PARTITION BY c.comment_id_no
ORDER BY c.inserted_at DESC) AS RowNumber
FROM Comments AS c
) AS c
WHERE c.RowNumber = 1
AND c.show = 'true';
Since you have tagged with Postgresql you could also make use of DISTINCT ON ()
:
SELECT *
FROM ( SELECT DISTINCT ON (c.comment_id_no)
c.id, c.comment_id_no, c.text, c.show, c.inserted_at
FROM Comments AS c
ORDER By c.comment_id_no, inserted_at DESC
) x
WHERE show = 'true';
Examples on DB<>Fiddle
SQL DISTINCT clause with WHERE
this should do this, depends on your DBMS
SELECT MAX(ID), Mail_ID FROM Subscriptions
WHERE Klant_ID = '".$_GET["ID"]."' GROUP BY Mail_ID
then you have the right IDs, futher more you can get the rest information by
SELECT * FROM Subscriptions,
(SELECT MAX(ID) as ids, Mail_ID FROM Subscriptions
WHERE Klant_ID = '".$_GET["ID"]."' GROUP BY Mail_ID) table2
WHERE ID=table2.ids
SQL select Distinct with where clause
Try this,
SELECT DISTINCT PersonID
FROM tableName
WHERE PersonID NOT IN
(
SELECT PersonID
FROM tableName
WHERE KvalifikationId = 2
)
SQLFiddle Demo
SQL Distinct clause not working?
If you want the naming only appearing once, then group by
comes to mind. One method is:
SELECT c.fldContactName,
MAX(c.fldsignonlinesetup) as fldsignonlinesetup,
MAX(c.fldorderdate) as fldorderdate,
MAX(c.fldemail) as fldemail
FROM tblcustomers c LEFT JOIN
tblorders o
ON c.fldcustomerid = o.fldcustomerid
WHERE o.fldorderdate BETWEEN '2013-01-01' AND '2016-12-31' AND
c.fldemail <> 'NULL' AND c.fldcontactname <> 'NULL' AND
c.fldcontactname <> '' AND c.fldemail <> '' AND
c.fldsignonlinesetup = 0
GROUP BY c.fldcontactname
HAVING COUNT(*) = 1
ORDER BY c.fldcontactname ASC;
SELECT DISTINCT
just makes sure that all the columns in the result set are never duplicates. It has nothing to do with finding values with only one row. The HAVING
clause does this.
Notes:
- The use of table aliases is good, but abbreviations for table names make the query more understandable.
- The
MAX()
is really a no-op. With one row, it returns the value from the one row. - The
GROUP BY
is on the field you care about -- the one you don't want duplicates for. - The
HAVING
clause gets the values with only one row. - MySQL does not require the
MAX()
functions, but I strongly recommend using an aggregation function, so you don't learn bad habits that don't work in other databases and can behave unexpected in MySQL. - Do you really mean
fldemail <> 'NULL'
or do you intendA.fldemail IS NOT NULL
?
How does Select query work with Distinct clause
SELECT DISTINCT is used to get set of unique rows. By other words, DISTINCT option cuts repeatable rows from a result. Most of times (most of db's actualy) you can't use distinct on not all but just some columns due to uncertainty of data.
Just for example, if your employees table looks like
Emp_id Dept_id Job_id
1 24 117
2 24 117
3 24 118
4 25 117
and you want to get list of unique jobs for every department, first you should try
SELECT Dept_id, Job_id
FROM Employees
so result will be like:
24 117
24 117
24 118
25 117
Notice that now row "24 117" is doubled due to two employees with same job in one department. So, DISTINCT will cut duplicated rows in your result:
SELECT DISTINCT Dept_id, Job_id
FROM Employees
gives:
24 117
24 118
25 117
But if you want DB to give you result of
SELECT Dept_id, DISTINCT Job_id
your DB won't be able to guess what exactly Dept_id you want to recieve in pair with 117 Job_id (choice is like "24 or 24 or 25?"). So if you want to get only distinct job_id's and some dept_id's along with them you should specify which exactly dept_id you want to see in you result if there will be many of them. For example you may get a minimum dept_id or something like that:
SELECT MIN(Dept_id) AS Dept_id, Job_id
FROM Employees
GROUP BY Job_id
with a result like:
24 117
24 118
It's all about certainty and uncertainty. In a sql query you should always specify what exactly you want.
Related Topics
Set Identity_Insert Off for All Tables
Type to Use for "Status" Columns in a SQL Table
Retrieve the Maximum Length of a Varchar Column in SQL Server
How to Display Oracle Schema Size with SQL Query
How to Store a Tree in SQL Database
Design Redis Database Table Like SQL
Dbms_Lob.Getlength() VS. Length() to Find Blob Size in Oracle
Simple Graph Search Algorithm in SQL (Postgresql)
SQL Server:Get All String Occurences (Tags) from Nvarchar(Max) Variable Containing a JSON String
Select All Records Don't Meet Certain Conditions in a Joined Table
Select Count of Rows in Another Table in a Postgres Select Statement
What's the Best Way to Store Sort Order in SQL
Dynamic Pivot Queries with Dynamic Dates as Column Header in SQL Server
Copy Data from a Table in One Database to Another Separate Database
Correct Way to Select from Two Tables in SQL Server with No Common Field to Join On