Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
HiCan anyone help me with a query i have please?I have a table which contains telephone numbers likeTable1ID Tel1 Tel2 Tel3 Tel41 07790123456 07790654321 07790111111 079742222222 012338383833 01238383838 038383827274 07790123456i am trying to write a query which selects all the records where a telephone number occurs more than onceso if i queried the above table the result set would beID Tel1 Tel2 Tel3 Tel41 07790123456 07790654321 07790111111 079742222224 07790123456i have tried to wrtie the query using having statement select Tel1, Tel2, Tel3, Tel4 from table1 group by Tel1, Tel2, Tel3, Tel4 Having count(*) > 1but this will only work if the two rows are identicalCan anyone help at allthanksSi
Kristen
Test
22859 Posts
Posted - 2010-01-16 : 06:00:16
This perhaps?
SELECT ID, TelFROM( SELECT ID, Tel1 AS Tel FROM table1 WHERE Tel1 IS NOT NULL UNION ALL SELECT ID, Tel2 AS Tel FROM table1 WHERE Tel2 IS NOT NULL UNION ALL SELECT ID, Tel3 AS Tel FROM table1 WHERE Tel3 IS NOT NULL UNION ALL SELECT ID, Tel4 AS Tel FROM table1 WHERE Tel4 IS NOT NULL) AS XGROUP BY ID, TelHAVING COUNT(*) > 1