How to compare 2 successive row values in a resultset object using python

2024/10/5 15:40:20

I have a table issue_logs:

 id | issue_id | from_status | to_status |             up_date              |  remarks  
----+----------+-------------+-----------+----------------------------------+-----------29 |       20 |          10 |        11 | 2018-09-14 11:43:13.907052+05:30 | UPDATED28 |       20 |           9 |        10 | 2018-09-14 11:42:59.612728+05:30 | UPDATED27 |       20 |             |         9 | 2018-09-11 17:45:35.13891+05:30  | NEW issue26 |       19 |           9 |        11 | 2018-09-06 16:37:05.935588+05:30 | UPDATED25 |       19 |             |         9 | 2018-09-06 16:27:40.543001+05:30 | NEW issue24 |       18 |          11 |        10 | 2018-09-05 17:13:37.568762+05:30 | UPDATED

and rt_status:

 id |   description    | duration_in_min 
----+------------------+-----------------1 | new              |               12 | working          |               13 | approval pending |               14 | resolved         |               15 | initial check    |               16 | parts purchase   |               17 | shipment         |               18 | close            |               19 | initial check    |               110 | parts purchase   |               111 | shipment         |               112 | close            |               1

For a date range from_datetime = '2018-09-06T16:34' to to_datetime = '2018-09-14T12:27' I want to select all the issues that have exceeded the duration_of_time set for each status value defined in the rt_status table. I should get from issue logs the records with ids 29, 27, and 26. The records with ids 29, and 26 should consider the time elapsed between their last up_date and to_datetime.

I would like to use the func.lag and over to do it, but I'm unable to get the correct records. I am using Postgresql 9.6, and Python 2.7. How exactly can I get the func.lag or func.lead to work using SQLAlchemy Core only?

What I tried:

    s = select([rt_issues.c.id.label('rtissue_id'),rt_issues,rt_status.c.duration_in_min,rt_status.c.id.label('stage_id'),issue_status_logs.c.id.label('issue_log_id'),issue_status_logs.c.up_date.label('iss_log_update'),(issue_status_logs.c.up_date - func.lag(issue_status_logs.c.up_date).over(issue_status_logs.c.issue_id)).label('mdiff'),]).\where(and_(*conditions)).\select_from(rt_issues.outerjoin(issue_status_logs,rt_issues.c.id == issue_status_logs.c.issue_id).outerjoin(rt_status,issue_status_logs.c.to_status == rt_status.c.id)).\order_by(asc(issue_status_logs.c.up_date),issue_status_logs.c.issue_id).\group_by(issue_status_logs.c.issue_id,rt_issues.c.id,issue_status_logs.c.id)rs = g.conn.execute(s)mcnt =  rs.rowcountprint mcnt, 'rowcont'if rs.rowcount > 0:for r in rs:print dict(r)

This yields results that include wrong records, i.e. issue log with id 28. Can anyone help with rectifying the error?

Answer

Though you yourself managed to solve your question, here's one take on it that does not use window functions, namely lag() or lead(). In order to compare differences between the up_date timestamps of consecutive issue logs you could self left join. In SQL the query could look like

select    ilx.id
from      issue_logs ilx
join      rt_status rsx on rsx.id = ilx.to_status
left join issue_logs ily on  ily.from_status = ilx.to_statusand ily.issue_id = ilx.issue_id
where     ilx.up_date >= '2018-09-06T16:34'
and       ilx.up_date <= ( coalesce(ily.up_date, '2018-09-14T12:27') -interval '1 minute' * rsx.duration_in_min );

and the same in SQLAlchemy SQL Expression Language:

from_datetime = '2018-09-06T16:34'
to_datetime = '2018-09-14T12:27'ilx = issue_status_logs.alias()
ily = issue_status_logs.alias()
rsx = rt_statusquery = select([ilx.c.id]).\select_from(ilx.join(rsx, rsx.c.id == ilx.c.to_status).outerjoin(ily, and_(ily.c.from_status == ilx.c.to_status,ily.c.issue_id == ilx.c.issue_id))).\where(and_(ilx.c.up_date >= from_datetime,ilx.c.up_date <= (func.coalesce(ily.c.up_date, to_datetime) -cast('1 minute', Interval) *rsx.c.duration_in_min)))
https://en.xdnf.cn/q/120177.html

Related Q&A

Getting all possible combination for [1,0] with length 3 [0,0,0] to [1,1,1]

from itertools import combinationsdef n_length_combo(arr, n):# using set to deal# with duplicates return list(combinations(arr, n))# Driver Function if __name__ == "__main__":arr = 01n = 3pri…

Compare values under multiple conditions of one column in Python

I have the following data:data = {"index": [1, 2, 3, 4, 5],"name": ["A", "A", "B", "B", "B"],"type": [s1, s2, s1, s2, s3]…

Python: Tkinter :Dynamically Create Label

I am trying to create Label Dynamically , I am getting invalid Syntax. Can you please help me what i am missing or any alternativecrsr = cnxn.execute(query)row_num=2column_num=0Variable_Number=1for row…

TypeError: str object is not callable when trying to click datepicker

The relevant HTML<div id="datepickerbox" class="ym-gbox-left"><div class="datepick_label"><div id="datepicker" class="hasDatepicker">…

Stanford parser with NLTK produces empty output

I am trying to use the Stanford parser in a small application written in Python with the NLTK interface. I tried the code given below.Everything seems to work right, no errors, Java is launched but I s…

How do you return a list of the matched item in string with regex? [duplicate]

This question already has answers here:Regular expression to match a dot [duplicate](8 answers)Closed 3 years ago.I made this simple functions that searches for emails in the source code of a page , th…

Indentation Error [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable…

open csv file in python to customize dictionary [duplicate]

This question already has answers here:Creating a dictionary from a CSV file(4 answers)Closed 9 years ago.I would like to know to load this csv file:Epitope,ID,Frequency,AssayAVNIVGYSNAQGVDY,123431,27.…

How does UserPassesTestMixin in django work?

views.pyclass ProfileEdit(UserPassesTestMixin, UpdateView):model = Userform_class = ProfileFormtemplate_name="profile/profile_new.html"def test_func(self):x = self.request.user.idprint (x)y =…

How to extract URL from HTML anchor element using Python3? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 9 years ago.Improve…