Trying to find duplicate values ββin TWO rows and TWO columns - SQL Server
Using SQL Server I am not a DBA but can write some generic SQL. Pulled my hair for about an hour. Search I've found several solutions, but they all don't work because of the way GROUP BY works.
I have a table with two columns that I am trying to check for duplicates:
- user ID
- OrderDate
I am looking for lines that have BOTH userid
and orderdate
as duplicates. I want to display these lines.
If I am using a group, I cannot pull out any other data such as the order id because it is not in the group by clause.
+3
source to share
4 answers
You can get duplicates using groupby and having. For example:
SELECT
userid,orderdate, COUNT(*)
FROM
yourTable
GROUP BY
userid,orderdate
HAVING
COUNT(*) > 1
EDIT:
SELECT * FROM yourTable
WHERE CONCAT(userid,orderdate) IN
(
SELECT
CONCAT(userid,orderdate)
FROM
yourTable
GROUP BY
userid,orderdate
HAVING
COUNT(*) > 1
)
0
source to share