我只需要返回拥有多个特许经营权的名称

时间:2011-11-23 14:02:36

标签: sql

我只需要返回拥有多个特许经营权的名称。我可以得到一个包含所有人的列表,我可以获得所有具有多个名称的列表,但是我无法让该列表显示多行,每个名称中的名称只有一行,其中包含名称和计数它在那里多少次。这里是我使用SQL Server的代码,Xref表看起来像这样

fraID|memERBCode|GLLevel|customerNumber|vendorNumber|DAVendorNumber|DAVindoeDisabled|status|di|ctyID
145     145     145         144020145   02PF0145       02DA0145     1                   I   WI   1
146     146     146         144020146   02PF0146       02DA0146     0                   I   3F   1

select [Last Name],[First Name], count(*)
from(
SELECT 
        conLastName as [Last Name], 
        conFirstName as [First Name],
        conMiddleInitial as [Middle Initial],   
        lawsonXRef.memERPCode as [Franchise],
        contactType.ctpDisplayName as [Type],
        contactStatus.ctpDisplayName as [Status]


    FROM Contacts   
    inner join lawsonXRef 
        on lawsonXRef.fraID = Contacts.fraID
    inner join SalesRepresentatives 
        on Contacts.conID = SalesRepresentatives.conID  
    inner join CategoryPopulation contactType
        on contactType.ctpID = Contacts.conTypeId   
    inner join CategoryPopulation contactStatus
        on contactStatus.ctpID = Contacts.conStatusId
    WHERE 
        srActive = 1 -- is Sales Rep.
        and 
            (Contacts.conLastName <> ''     and Contacts.conFirstName <> '')

)data1

    group by [Last Name],[First Name]
        having count(*)>1
    order by [Last Name]

2 个答案:

答案 0 :(得分:0)

您可以在查询中查看类似派生表的内容,只返回具有多个特许经营权的人员的ID。

猜测你的查询,如果一个联系人不止一次存在于lawsonXref中,那么这被认为具有多个特许经营权。 fraID是特许经营权ID,并且唯一标识特许经营权?

SELECT 
    conLastName as [Last Name], 
    conFirstName as [First Name],
    conMiddleInitial as [Middle Initial],   
    lawsonXRef.memERPCode as [Franchise],
    contactType.ctpDisplayName as [Type],
    contactStatus.ctpDisplayName as [Status]
FROM
    Contacts   
inner join 
(
    -- this should generate a list of all
    -- the franchise ids that exist more than
    -- once in the xref table
    SELECT 
        X.fraID
    FROM
        lawsonXRef X
    GROUP BY
        X.fraID
    HAVING
        count(1) > 1
    on lawsonXRef.fraID = Contacts.fraID
) AS lawsonXRef
inner join 
    SalesRepresentatives 
    on Contacts.conID = SalesRepresentatives.conID  
inner join 
    CategoryPopulation contactType
    on contactType.ctpID = Contacts.conTypeId   
inner join 
    CategoryPopulation contactStatus
    on contactStatus.ctpID = Contacts.conStatusId
WHERE 
    srActive = 1 -- is Sales Rep.
    and 
    (Contacts.conLastName <> ''
    and Contacts.conFirstName <> '')

如果这不起作用,请提供几行LawsonXref数据,显示单个特许经营记录的示例和一个具有多个记录的记录。

答案 1 :(得分:0)

您的联系人表格似乎会为一个联系人保留多条记录 - 每个特许经营权一个。

如果是这样,以下内容应该有效:

SELECT  con.conLastName as [Last Name], 
        con.conFirstName as [First Name],
        con.conMiddleInitial as [Middle Initial],   
        lawsonXRef.memERPCode as [Franchise],
        contactType.ctpDisplayName as [Type],
        contactStatus.ctpDisplayName as [Status]
    FROM (select conLastName, conFirstName, conMiddleInitial
          from Contacts
          group by conLastName, conFirstName, conMiddleInitial
          having count(distinct fraID) > 1) con
    inner join Contacts 
        on con.conLastName = Contacts.conLastName and 
           con.conFirstName = Contacts.conFirstName and 
           con.conMiddleInitial = Contacts.conMiddleInitial
    inner join lawsonXRef 
        on lawsonXRef.fraID = Contacts.fraID
    inner join SalesRepresentatives 
        on Contacts.conID = SalesRepresentatives.conID  
    inner join CategoryPopulation contactType
        on contactType.ctpID = Contacts.conTypeId   
    inner join CategoryPopulation contactStatus
        on contactStatus.ctpID = Contacts.conStatusId
    WHERE 
        srActive = 1 -- is Sales Rep.
        and 
            (Contacts.conLastName <> ''     and Contacts.conFirstName <> '')

顺便说一句,如果这是您的设计,那么我强烈建议您更改它。如果有几个人叫简·史密斯怎么办?