开发者

How can I separate results from query?

I have a table where I want to catch duplicate values.

After catching them I want to separate those results into separate rows.

The table:

CREATE TABLE [dbo].[ContactTable](
[contactid] [int] IDENTITY(1,1) NOT NULL,
[telephone1] [varchar](100) NULL,
[telephone2] [varchar](100) NULL,
[fullname] [varchar](100) NOT NULL
) ON [PRIMARY]

A few values:

insert into ContactTable(telephone1, telephone2,fullname) values('123','1234','danny')
insert into ContactTable(telephone1, telephone2,fullname) values('123','1234','danny1')
insert into ContactTable(telephone1, telephone2,fullname) values('123*','1234#','martin')
insert into ContactTable(telephone1, telephone2,fullname) values('1243*','15234#','martin')
insert into ContactTable(telephone1, telephone2,fullname) values('1243','15234','martin1')

The query for catching the duplicate values:

SELECT     Phones, COUNT(Phones) AS CountPhones
FROM    
(
    SELECT     Phones, contactid
    FROM
    (
        SELECT     telephone1 AS Phones, contactid
        FROM          ContactTable
        WHERE      (telephone1 IS NOT NULL) 
        UNION ALL
        SELECT     REPLACE(telephone1, '*', '') AS Phones, contactid
        FROM         ContactTable 
        WHERE     (telephone1 IS NOT NULL)  AND (telephone1 LIKE '%*')
        UNION ALL
        SELECT     REPLACE(telephone1, '#', '') AS Phones, contactid
        FROM         ContactTable 
        WHERE     (telephone1 IS NOT NULL)  AND (telephone1 LIKE '%#')
        UNION ALL
        SELECT     telephone2 AS Phones, contactid
        FROM          ContactTable
        WHERE      (telephone2 IS NOT NULL) 
        UNION ALL
        SELECT     REPLACE(telephone2, '*', '') AS Phone开发者_运维百科s, contactid
        FROM         ContactTable 
        WHERE     (telephone2 IS NOT NULL)  AND (telephone2 LIKE '%*')
        UNION ALL
        SELECT     REPLACE(telephone2, '#', '') AS Phones, contactid
        FROM         ContactTable 
        WHERE     (telephone2 IS NOT NULL)  AND (telephone2 LIKE '%#')
    )as Tel
    GROUP BY Phones, contactid
) as T
GROUP BY Phones
HAVING      (COUNT(*) > 1)
ORDER BY CountPhones DESC

The results:

telephone  Count
---------  -----
123        3
1234       3
1243       2
15234      2

But I want to get the results like this:

id  telephone  fullname
--  ---------  --------
1   123        danny
2   123        danny1
3   123        martin
3   1234       martin
1   1234       danny
2   1234       danny1
5   1243       martin1
4   1243       martin
4   15234      martin
5   15234      martin1

How can I to do it?


SELECT * FROM (
SELECT
    contactid AS id, 
    REPLACE(REPLACE(telephone1, '*', ''), '#', '') AS telephone, 
    fullname 
FROM
    ContactTable 
UNION ALL
SELECT 
    contactid AS id, 
    REPLACE(REPLACE(telephone2, '*', ''), '#', '') AS telephone, 
    fullname
    FROM ContactTable) AS all_phones 
WHERE all_phones.telephone in (
    SELECT phone FROM (
        SELECT 
            COUNT(*) AS pnumber, 
            phone 
        FROM (
            SELECT 
                REPLACE(REPLACE(telephone1, '*', ''), '#', '') AS phone
            FROM
                ContactTable 
            UNION ALL
            SELECT 
                REPLACE(REPLACE(telephone2, '*', ''), '#', '') AS phone
            FROM 
                ContactTable ) AS phones 
            GROUP BY phone) 
        AS phone_numbers WHERE pnumber > 1)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜