简体   繁体   中英

I need to return only the names that have more than one franchise

I need to return only the names that have more than one franchise. I can get a list with everyone and i can get a list of all the names that have more than one but i am unable to have that list show multiple rows with the name in each i only get one row with the name and the count of how many times it is in there. here is the code i have I am using a SQL Server the Xref table look like this

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]

You can look at doing something like a derived table in your query that only returns the id of people with multiple franchises.

Guessing at your query, if a Contact exists in lawsonXref more than once, that's considered having muliple franchises. fraID is franchise id and that uniquely identifies a franchise?

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

if that's not working, provide a few rows of LawsonXref data showing an example of a single franchise record and one with multiple.

It looks as though your contact table will hold multiple records for a single contact - one for each franchise.

If so, the following should work:

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

Incidentally, if this is your design then I strongly recommend changing it. What if there are several people called Jane Smith?

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