SQL Server 2014:如何获取具有仅特定列的不同值的行的记录

时间:2017-07-04 23:31:45

标签: sql sql-server having

我们的客户在我们的车辆上有帐户。一些客户有多辆车,因此有多个车牌。有时,一些客户的车辆也有不同的保险账户。

我需要一个包含以下帐户的列表:

  1. 超过1个车牌和
  2. 超过1个保险帐户ID
  3. 我正在使用3张桌子:

    • Account表A,用于帐户信息
    • Plate表P代表车牌信息
    • EventLog表E发送通知信时的信息

    关系:

    A.AccountId = P.AccountId = E.AccountId
    

    到目前为止我的代码:

    SELECT 
        A.AccountNumber, A.AccountId, A.CurrentBalance,
        E.NotificationDt,
        P.LicPlateNo, A.RegistrationTypeId, P.InsuranceAccountId
    FROM 
        Account A
    INNER JOIN 
        Plate P ON A.AccountId = P.AccountId
    INNER JOIN  
        EventLog E ON A.AccountId = E.AccountId
    WHERE 
        A.RegistrationTypeId = 3
        AND P.EndDate IS NULL               
        AND A.AccountStatusId = 1           
        AND A.DelinquencyStatusId = 11
        AND E.EventId = 64
        AND P.PlateStatusId = 1
    ORDER BY 
        AccountNumber, A.AccountId, P.LicPlateNo
    

    我的示例数据如下所示:

    +---------+---------+---------+-------------+----------+---+--------+
    |AccNo    | AccId   |CurrBal  |NotifDt      |LicPlateNo|RTI|InsAccId|
    +---------+---------+---------+-------------+----------+---+--------+
    |21234561 |123456   |    56.79|   2017-01-01|ABC123    |  3|1234ABC |
    |21234572 |123457   |    83.25|   2017-01-03|DEF345    |  3|345DEF  |
    |22345672 |234567   |   104.38|   2017-01-03|GHI345    |  3|567GHI  |
    |22345672 |234568   |   104.38|   2017-01-03|JKL678    |  3|789MNO  |
    +---------+---------+---------+-------------+----------+---+--------+
    

    在我的示例数据中,最后两列是针对相同的AccountNumber,其中LicencePlateNosInsurance AccountIds不同。

    我希望我的数据看起来像这样:

    +---------+---------+---------+-------------+----------+---+--------+
    |22345672 |234567   |   104.38|   2017-01-03|GHI345    |  3|567GHI  |
    |22345672 |234568   |   104.38|   2017-01-03|JKL678    |  3|789MNO  |
    +---------+---------+---------+-------------+----------+---+--------+
    

    代码会给我什么数据?

    我尝试使用GROUP BY... HAVING子句,但只给出了具有多个牌照的唯一AccountNumber。我想展示不同的车牌号和不同的InsuranceAccountID

2 个答案:

答案 0 :(得分:1)

一种简单的方法是使用您的查询作为基础并使用多行计算值:

SELECT a.*
FROM (SELECT A.AccountNumber, A.AccountId, A.CurrentBalance, E.NotificationDt,
             P.LicPlateNo, A.RegistrationTypeId, P.InsuranceAccountId,
             COUNT(*) OVER (PARTITION BY A.AccountNumber) as cnt
      FROM Account A INNER JOIN
           Plate P
           ON A.AccountId = P.AccountId INNER JOIN
           EventLog E
           ON A.AccountId = E.AccountId
      WHERE A.RegistrationTypeId = 3 AND
            P.EndDate IS NULL AND  
            A.AccountStatusId = 1 AND        
            A.DelinquencyStatusId = 11 AND
            E.EventId = 64 AND
            P.PlateStatusId = 1
     ) a
WHERE cnt > 1;   
ORDER BY AccountNumber, AccountId, LicPlateNo;

答案 1 :(得分:1)

执行此操作的简单方法是加入一个结果集,以便检查其中一个或两个条件。只需包含适当的过滤条款:

...
INNER JOIN EventLog E ON A.AccountId = E.AccountId

INNER JOIN
(
    SELECT
        Plate.AccountId,
        LicensePlateCount = COUNT(DISTINCT Plate.LicPlateNo),
        InsuranceAccountCount = COUNT(DISTINCT Plate.InsuranceAccountId)
    FROM
        Plate
    WHERE
        Plate.EndDate IS NULL
        AND Plate.PlateStatusId = 1
    GROUP BY
        Plate.AccountId
) PlateAccounts ON A.AccountId = PlateAccounts.AccountId
    -- 1) More than 1 License Plate:
    AND PlateAccounts.LicensePlateCount > 1
    -- 2) More than 1 Insurance Account IDs:
    AND PlateAccounts.InsuranceAccountCount > 1

WHERE A.RegistrationTypeId = 3
...