简体   繁体   English

如何使用over()函数列出出现次数,但只显示结果中列出的出现3次或多次?

[英]How do I list the count of occurrences using the over() function, but only display the occurrences that are listed in the result 3 or more times?

How do I list the amount of times each account number has occurred if I only want to see the ones that have occurred 3 times or more? 如果我只想查看已出现3次以上的次数,如何列出每个帐号发生的次数?

I'm using a window OVER() function to get the number of times the account number is listed. 我正在使用窗口OVER()函数来获取列出帐号的次数。

SELECT a.ACCOUNTNUMBER AS [Account Number]
    , CONCAT(n.FIRST, ' ', n.MIDDLE, ' ', n.LAST) AS [Member Name]
    , l.id AS [Loan ID]
    , COUNT(a.ACCOUNTNUMBER)
        OVER(partition by a.ACCOUNTNUMBER) as [Number of 
Tracking Record] 
    , n.EMAIL AS [Email]
    , n.HOMEPHONE AS [Phone Number]
FROM dbo.account a
INNER JOIN dbo.LOAN l
ON a.ACCOUNTNUMBER = l.PARENTACCOUNT
INNER JOIN dbo.LOANTRACKING lt
ON l.PARENTACCOUNT = lt.PARENTACCOUNT
AND l.ID = lt.ID
INNER JOIN dbo.NAME n 
ON a.ACCOUNTNUMBER = n.PARENTACCOUNT
WHERE lt.type = 46
AND l.ProcessDate = CONVERT(VARCHAR(8), dateadd(day,-1, getdate()), 
112)
AND lt.ProcessDate = CONVERT(VARCHAR(8), dateadd(day,-1, getdate()), 
112)
AND a.CLOSEDATE IS NULL
AND lt.EXPIREDATE IS NULL
GROUP BY a.ACCOUNTNUMBER, n.FIRST, n.MIDDLE, n.LAST, l.id, n.email, 
n.HOMEPHONE
ORDER BY [Account Number]

Right now my result is giving me the number of times all of the accounts are listed in the "Number of Tracking Record" column. 现在,我的结果是给我所有帐户在“跟踪记录数”列中列出的次数。 I want to see only the account numbers that have "3" occurrences or above. 我只想查看出现“ 3”次或以上的帐号。

My current result: 我目前的结果:

我目前的结果

My Desired Result: 我想要的结果:

我想要的结果

SELECT *   -- edit to include only your relevant columnns
FROM
(
SELECT a.ACCOUNTNUMBER AS [Account Number]
    , CONCAT(n.FIRST, ' ', n.MIDDLE, ' ', n.LAST) AS [Member Name]
    , l.id AS [Loan ID]
    , COUNT(a.ACCOUNTNUMBER)
        OVER(partition by a.ACCOUNTNUMBER) as [Number of 
Tracking Record] 
    , n.EMAIL AS [Email]
    , n.HOMEPHONE AS [Phone Number]
FROM dbo.account a
INNER JOIN dbo.LOAN l
ON a.ACCOUNTNUMBER = l.PARENTACCOUNT
INNER JOIN dbo.LOANTRACKING lt
ON l.PARENTACCOUNT = lt.PARENTACCOUNT
AND l.ID = lt.ID
INNER JOIN dbo.NAME n 
ON a.ACCOUNTNUMBER = n.PARENTACCOUNT
WHERE lt.type = 46
AND l.ProcessDate = CONVERT(VARCHAR(8), dateadd(day,-1, getdate()), 
112)
AND lt.ProcessDate = CONVERT(VARCHAR(8), dateadd(day,-1, getdate()), 
112)
AND a.CLOSEDATE IS NULL
AND lt.EXPIREDATE IS NULL
GROUP BY a.ACCOUNTNUMBER, n.FIRST, n.MIDDLE, n.LAST, l.id, n.email, 
n.HOMEPHONE
) MyQuery
WHERE MyQuery.[Number of Tracking Record] >= 3
ORDER BY [Account Number]

I added an "outer" query around yours, using yours as a subquery, and filtered it for those with just the >=3 condition. 我在您的查询周围添加了一个“外部”查询,将您的查询用作子查询,并为那些条件为> = 3的查询过滤了该查询。 And I moved the ORDER BY from your inside query to the outer query, as it's just used for ordering the results and not used in the computation itself. 我将ORDER BY从内部查询移至外部查询,因为它仅用于排序结果,而不用于计算本身。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM