SQL-需要编写查询,以获取所有具有计数>的地址簿条目我被卡住了

时间:2016-10-04 15:24:47

标签: sql

SELECT  
    a.AddressBookID, a.TaxID, a.BusinessName, 
    a.VendorName1, a.VendorName2, a.FirstName, a.LastName, 
    a.Address1, a.Address2, a.City, a.[State], a.ZipCode, 
    a.WorkPhone, a.MobilePhone, a.Active
    (SELECT COUNT(ab.taxid) AS totaltaxid
     FROM addressbook ab (NOLOCK)
     JOIN AddressBook abc ON ab.AddressBookID = abc.AddressBookID
     HAVING COUNT(ab.taxid) > 1) 
FROM 
    AddressBook AS a (NOLOCK)
JOIN  
    Payment AS p (NOLOCK) ON a.AddressBookID = p.EntityID
WHERE    
    a.active = 1

尝试编写查询,该查询将提取已多次输入的详细信息的地址簿中的所有条目。

select 
    ab.AddressBookID, ab.AddressBookTypeName, 
    COUNT(ab.AddressBookTypeName) AS 'Name-Dups', 
    ab.TaxID, COUNT(ab.taxid) AS 'TaxID-Dups',
    --, p.PaymentID
    --, p.EntityID
    ISNULL(COUNT(p.CheckNumber),0) AS 'NumPmtsToEntity'
from 
    AddressBook ab (NOLOCK)
left outer join 
    Payment p (NOLOCK) on ab.AddressBookID = p.EntityID -- left outer join because we want AddressBook dups listed even if there is no pmt to vendor
group by 
    ab.AddressBookID, ab.TaxID, ab.AddressBookTypeName
    --, p.PaymentID--, p.EntityID
having COUNT(ab.taxid) > 1
order by ab.TaxID

0 个答案:

没有答案