如何将查询结果分开?

时间:2011-06-22 08:22:50

标签: sql

我有一个表格,我想要捕捉重复的值。

在捕获它们之后,我想将这些结果分成不同的行。

表格:

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]

一些值:

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')

捕获重复值的查询:

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 Phones, 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

结果:

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

但我想得到这样的结果:

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

我该怎么做?

1 个答案:

答案 0 :(得分:0)

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)