我的数据采用这种格式
CALENDAR CLIENTID TOTAL
20170801 1700 2
20170801 1800
20170801 1900 2
20170801 1990
20170801 2000 0
20170801 2090 0
20170802 2090 0
20170803 2090 0
当一个特定的客户连续3个总数为零时,我试图获得最小的calendar day
。因此,在上面的示例中,我的输出为20170801
,因为客户端2090
在total
,20170801
和20170802
天的20170803
为零。到目前为止,我有以下查询,但是a
和b
的计数不准确。
WITH cte AS (
SELECT *,COUNT(1) OVER(PARTITION BY clientid) b FROM
(
SELECT tt.*
,(SELECT COUNT(CALENDAR) FROM STATS
WHERE total = 0
) AS a
FROM STATS tt
WHERE total = 0
) t1
)
SELECT * FROM cte WHERE b >= 3
答案 0 :(得分:1)
使用lead()
。以下查询获取所有此类日期,按日期排序:
select s.*
from (select s.*,
lead(total) over (partition by clientid order by calendar) as total_1,
lead(total, 2) over (partition by clientid order by calendar) as total_2
from stats s
) s
where total = 0 and total_1 = 0 and total_2 = 0
order by date;
您可以为数据库添加fetch first 1 row only
或等同名称,以仅获得一行。
答案 1 :(得分:0)
方法是
from PyQt4 import QtGui, QtCore
class Window(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
layout = QtGui.QVBoxLayout(self)
self.combo = QtGui.QComboBox()
self.combo.setEditable(True)
self.combo.lineEdit().setAlignment(QtCore.Qt.AlignCenter)
self.combo.addItems('One Two Three Four Five'.split())
layout.addWidget(self.combo)
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
结果
WITH cte AS (
Select * From dbo.Consequtive Where TOTAL = 0
)
SELECT r1.*, r2.*, r3.*
FROM cte as r1
Left Join cte as r2
On r1.CLIENTID = r2.CLIENTID and DATEADD(day, 1, r1.CALENDAR) = r2.CALENDAR
Left Join cte as r3
On r1.CLIENTID = r3.CLIENTID and DATEADD(day, 2, r1.CALENDAR)= r3.CALENDAR
Where r1.TOTAL = 0 and r2.TOTAL = 0 and r3.TOTAL = 0