Jerry
Jerry

Reputation: 3

Multiple Column Duplicate in SQL

I have a table with fields (id , gid, uid) For some rows (gid, uid) are same. There is duplicate.

Example 1:(repeated)

id      gid     uid

5802    1032    1247

5806    1032    1247

5807    1032    1247

5808    1032    1247

Example 2: (occurs once)

id      gid     uid

5103    1132    1290

In my case I want to pass uid and gid and check if there are duplicates - Return True if (gid,uid) doesnot occur in the table - Otherwise it return false.( for Example 1 and Example 2 )

I tried this query to return true or false.

SELECT  CASE WHEN EXISTS  (SELECT A.*
FROM table1 A
INNER JOIN (SELECT gid, uid
            FROM table1
            GROUP BY gid, uid
            HAVING COUNT(*) = 0) 
ON A.gid = '1011' AND A.uid = '1086') THEN 'TRUE' ELSE 'FALSE' END from dual;

But it always returns True ; Please help in solving the same

Upvotes: 0

Views: 58

Answers (2)

Hesam Akbari
Hesam Akbari

Reputation: 1151

select 
    gid
    ,uid
,case when [count]>1 then 'False' else 'True' end Result
from (
    select 
    gid
    ,uid
    ,count(*) [count]
    from Table1 
    group by 
    gid
    ,uid
) temp

Result

Upvotes: 1

Popeye
Popeye

Reputation: 35930

You can use aggregation as follows:

SELECT CASE WHEN COUNT(1) = 1 THEN 'TRUE' else 'FALSE' END FROM YOUR_tABLE
WHERE gid = '1011' AND uid = '1086'

Upvotes: 2

Related Questions