简体   繁体   English

SQL计数记录取决于条件

[英]SQL count records depending on criteria

I have a table which has account information for each individual working day. 我有一张桌子,上面有每个工作日的帐户信息。 Each record shows the balance on the account for that particular day. 每条记录都会显示该天特定日期的帐户余额。 I need to create a field that has a running count on how many days it has been since the balance on that particular account was zero. 我需要创建一个字段,该字段自该特定帐户的余额为零以来已经运行了多少天。

For example: I have an account which is number 00000001. It was opened a week ago. 例如:我有一个号码为00000001的帐户。它是在一周前开设的。 The database has created a record for the account for last Tuesday, Wednesday, Thursday, Friday and Monday. 数据库已为该帐户创建了上一个星期二,星期三,星期四,星期五和星期一的记录。 The account did not have a balance until Friday and the balance was the same for Monday. 直到星期五该帐户都没有余额,星期一的余额相同。 I want the field to show '2' as a result. 我希望该字段显示为“ 2”。 Further to this if the balance drops to '0' today, I need the count to reset on the next record and start again if the account has a balance the following day. 此外,如果今天余额下降到“ 0”,我需要在下一条记录上重置计数,如果第二天该帐户有余额,则需要重新开始计数。 There are several accounts in this table so I need this to work for each one individually. 该表中有几个帐户,因此我需要分别为每个帐户工作。

Below is as far as I have got: 以下是我所得到的:

SELECT
pos.EffDate,
cus.CNA1,
pos.ACNO,
pos.CCY,
pos.LDBL,
pos.LDBLUSD,
MIN(pos3.effdate) AS 'First Post Date',
pos3.Balcount

FROM[dbo].[Account] AS pos
JOIN [dbo].[Customer] AS cus ON ((pos.CNUM=cus.CUST_NO) AND (cus.effdate=pos.effdate))
LEFT JOIN (SELECT pos2.effdate, pos2.ACNO, SUM(CASE pos2.LDBL WHEN 0 THEN 0 ELSE 1 END) AS 'Balcount' FROM [dbo].[Account] AS pos2 GROUP BY pos2.ACNO, pos2.Effdate HAVING pos2.effdate BETWEEN pos2.effdate AND MIN(pos2.effdate)) pos3 ON pos3.ACNO = pos.ACNO
WHERE pos.effdate >='1 Dec 2015' AND pos.Effdate <'30 Dec 2015'
AND pos.srcsys = 'MP_UK'
AND pos.LDBL <= 0
AND pos.CNUM <> '000020'
AND pos.intbear <> 'N'
AND pos.blockdeb IS null

GROUP BY pos.EffDate,cus.CNA1,pos.ACNO,pos.CCY,pos.LDBL,pos.LDBLUSD, pos3.Balcount
ORDER BY 1,2,3

If you need me to clarify anything please let me know. 如果您需要我澄清任何事情,请告诉我。 All help is greatly appreciated. 非常感谢所有帮助。

Thanks, Ben 谢谢,本

Basically, you want to group the data, based on the number of time the account is zero before a given row. 基本上,您希望根据给定行之前帐户为零的时间对数据进行分组。 Then, within each group, you want to enumerate the records. 然后,在每个组中,您要枚举记录。

In SQL Server 2012+, you can do these things with window functions. 在SQL Server 2012+中,您可以使用窗口函数执行这些操作。 I am not sure exactly what your sample query has to do with the question, but here is the basic idea: 我不确定您的示例查询与该问题有什么关系,但这是基本思想:

select a.*, row_number() over (partition by cust_no, grp order by eff_date) as seqnum
from (select a.*,
             sum(case when balance = 0 then 1 else 0 end) over 
                 (partition by cust_no order by eff_date) as grp
      from Account a
     ) a;

In earlier versions of SQL Server, you can do something very similar using apply . 在早期版本的SQL Server中,您可以使用apply非常相似的操作。

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

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