简体   繁体   中英

SQL select the records that have the same name but more than one type of te_source

在此处输入图片说明

this is my table and I would like to show the records that have the same name but more than one type of te_source and this will be result only

在此处输入图片说明

Here you have it (it just have to check that there are rows with the same name but a different source) :

SELECT * 
FROM MY_TABLE 
WHERE EXISTS (SELECT * 
              FROM MY_TABLE as ALTERNATE_SOURCE 
              WHERE MY_TABLE.tep_first_name = ALTERNATE_SOURCE.tep_first_name and 
                    MY_TABLE.tep_last_name = ALTERNATE_SOURCE.tep_last_name and
                    MY_TABLE.te_source <> ALTERNATE_SOURCE.te_source)
SELECT 
    *
FROM  
    MyTable
WHERE 
    tep_PK IN
    (
        SELECT 
            tep_PK
        FROM 
            MyTable
        GROUP BY 
            tep_PK
        HAVING 
            COUNT(*) > 1
    )

Use the below query .

   SELECT *
   FROM YourTable a
   JOIN (  SELECT tep_lastname,tep_FirstName
            FROM YourTable
                    GROUP BY tep_lastname,tep_FirstName
            HAVING COUNT(DISTINCT te_source)>1)b
    ON  a.tep_lastname=b.tep_lastname AND a.tep_FirstName=b.tep_FirstName

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM