Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
adddate interval in python
#1
Using mysql I can get this to work. I
want to get all missing days between 2 dates.
The table was last updated on 4-10-2020. Today is 4-16-2020
As of today below is the correct output.

Output:
date field_id_id 2020-04-11 21 2020-04-12 21 2020-04-13 21 2020-04-14 21 2020-04-15 21 2020-04-16 21
Output:
set @i = -1; Select B.date, 21 as field_id_id from ( SELECT DATE(ADDDATE('2020-03-18', INTERVAL @i := @i + 1 DAY)) AS date FROM dummy HAVING @i < DATEDIFF('2020-04-16', '2020-03-18') )B left join ( Select date from dummy where field_id_id = 21 and date between '2020-03-18' and '2020-04-16' )C ON B.date = C.date where C.date is NULL
-- Below is the python giving me an error on "Interval %s "

            v_sql_tx = 'Select  B.date, %s as field_id_id '\
            ' from  ( '\
            '   SELECT DATE(ADDDATE(%s, INTERVAL %s:= %s + 1 DAY)) AS '\
            ' date '\ 
            ' FROM dummy '\
            ' HAVING %s < DATEDIFF(%s, %s) '\
            ' )B left join( '\
            '   Select  date '\
            ' from dummy '\
            '  where field_id_id = %s '\
            ' and date  between %s and %s '\
            ' )C ON B.date = C.date '\
            ' where C.date is NULL '


python version 3.6.8
Any suggestions to get this to work in python ?

TIA
Steve42
Reply
#2
The code you've posted is just a string assignment. It wouldn't perform a sql query.

Line 3 also has a problem that there is a space after the backslash on the end, so as pasted, it would produce an error instead of completing the assignment. I'm not sure if that's in your code or a cut&paste error.
Reply
#3
You need to learn how to use SQL for MySql find a good tutorial and run through it
search google for 'python 3 and mysql tutorial'
one example: https://www.mysqltutorial.org/python-mysql/
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Mapping a value to an interval JazonKuer 12 1,845 Mar-17-2023, 07:59 PM
Last Post: Gribouillis
  Confidence interval after doing penalised cox regression HatemAli 0 1,111 Feb-23-2022, 11:02 PM
Last Post: HatemAli
  Complex X Tick Interval JoeDainton123 0 1,443 Oct-05-2020, 07:27 PM
Last Post: JoeDainton123
  Updating a matrix in a time interval inside a for loop vp1989 4 2,849 May-17-2020, 07:15 PM
Last Post: vp1989
  Assigning Data from one column to another with different associated timing interval alexafshari 1 1,924 Apr-30-2020, 03:59 PM
Last Post: pyzyx3qwerty
  save data in .txt after certain interval Shaswat 1 2,058 Oct-13-2019, 07:07 AM
Last Post: Gribouillis
  How define iteration interval increment SriMekala 5 4,253 Jun-01-2019, 01:06 PM
Last Post: ichabod801
  Python data prep help for confidence interval eslearner 0 2,331 Feb-24-2018, 05:13 AM
Last Post: eslearner

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020