Our customers have accounts with us for their vehicles. Some customers have multiple vehicles, hence multiple licence plates. Sometimes, some customers have different Insurance Accounts for their vehicles too.
I need a list of accounts that have:
I'm using 3 tables:
Account
table A for account info Plate
table P for licence plate info EventLog
table E for info when notice letter was sent Relationship:
A.AccountId = P.AccountId = E.AccountId
My code so far:
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
My sample data looks like this:
+---------+---------+---------+-------------+----------+---+--------+
|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 |
+---------+---------+---------+-------------+----------+---+--------+
In my sample data, the last two columns are for the same AccountNumber
which has different LicencePlateNos
and different Insurance AccountIds
.
I would like my data to look like this:
+---------+---------+---------+-------------+----------+---+--------+
|22345672 |234567 | 104.38| 2017-01-03|GHI345 | 3|567GHI |
|22345672 |234568 | 104.38| 2017-01-03|JKL678 | 3|789MNO |
+---------+---------+---------+-------------+----------+---+--------+
What would the code be that would give me that data?
I tried using a GROUP BY... HAVING
clause but that gives me only the unique AccountNumber
s that have multiple licence plates. I would like to show the different licence plate nos and the different InsuranceAccountID
s as well.
A simple way is to use your query as a base and count the values with more than one row:
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;
An easy way to do this would be to JOIN a result set that would allow you for checking for either or both conditions. Just include the appropriate filter clause:
...
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
...
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.