How to match two queries of postgres in python - python

I have two queries and I want to match the rows of those two queries. That is I want to execute the same number of rows in both the queries. Below code executes the number of dates of the present month and score I have to change it manually every day which is not possible
cursor.execute("select TO_CHAR(i :: DATE, 'dd/mm/yyyy') from generate_series(date_trunc('month', current_date), current_date, '1 day'::interval) i ")
# data = cursor.fetchone()
rows = cursor.fetchall()
labels6 = list()
i = 0
for row in rows:
labels6.append(row[i])
Above is the code which executes dates of the current month
cursor.execute("select score*100 from daily_stats1 where user_id=102")
rows = cursor.fetchall()
# Convert query to objects of key-value pairs
presentmonth1 = list()
i = 0
for row in rows[:28]:
presentmonth1.append(row[i])
Above is the code which executes present month score.'28' is given manually I have to change it every day which is not possible.so I want a solution where the date rows match with the score rows

I assume the excess indentation in your code is a mistake.
If that is the case, I think this will solve your problem:
cursor.execute("select TO_CHAR(i :: DATE, 'dd/mm/yyyy') from "
"generate_series(date_trunc('month', current_date), current_date, '1 day'::interval) i ")
labels6 = cursor.fetchall()
cursor.execute("select score*100 from daily_stats1 where user_id=102")
presentmonth1 = cursor.fetchall()[:len(labels6)]
I removed some unneeded code, but the result should be correct.

Related

Python: cx_Oracle does not like how I am entering date

I am trying to do a simple select all query in python using the Cx_oracle module. When I do a select all for the first ten rows in a table I am able to print our the output. However when I do a select all for the first ten rows for a specific date in the table all that gets printed out is a blank list like this: [].
Here is the query select all query that prints out all the results:
sql_query = "select * from table_name fetch first 10 rows only"
cur = db_eng.OpenCursor()
db_eng.ExecuteQuery(cur, sql_query)
result = db_eng.FetchResults(cur)
print(result)
The above query works and is able to print out the results.
Here is the query that I am having trouble with and this query below works in sql developer:
sql_query = "select * from table_name where requested_time = '01-jul-2021' fetch first 10 rows only"
cur = db_eng.OpenCursor()
db_eng.ExecuteQuery(cur, sql_query)
result = db_eng.FetchResults(cur)
print(result)
I also tried this way where I define the date outside of the query.
specific_date = '01-jul-2021'
sql_query = "select * from table_name where requested_time = '{0}' fetch first 10 rows only".format(specific_date)
cur = db_eng.OpenCursor()
db_eng.ExecuteQuery(cur, sql_query)
result = db_eng.FetchResults(cur)
print(result)
Oracle dates have a time portion. The query
select * from table_name where requested_time = '01-jul-2021' fetch first 10 rows only
Will only give you the rows for which the value for the column requested_time is 01-jul-2021 00:00. Chances are that you have other rows for which there is a time portion as well.
To cut off the time portion there are several options. Note that I explicitly added the a TO_DATE function to the date - you're assuming that the database is expecting a dd-mon-yyyy format and successfully will do the implicit conversion but it's safer to let the database know.
TRUNC truncate the column - this will remove the time portion
SELECT *
FROM table_name
WHERE TRUNC(requested_time) = TO_DATE('01-jul-2021','DD-mon-YYYY')
FETCH FIRST 10 ROWS ONLY
Format the column date to the same format as the date you supplied and compare the resulting string:
SELECT *
FROM table_name
WHERE TO_CHAR(requested_time,'DD-mon-YYYY') = '01-jul-2021'
FETCH FIRST 10 ROWS ONLY
Example:
pdb1--KOEN>create table test_tab(requested_time DATE);
Table TEST_TAB created.
pdb1--KOEN>BEGIN
2 INSERT INTO test_tab(requested_time) VALUES (TO_DATE('08-AUG-2021 00:00','DD-MON-YYYY HH24:MI'));
3 INSERT INTO test_tab(requested_time) VALUES (TO_DATE('08-AUG-2021 01:00','DD-MON-YYYY HH24:MI'));
4 INSERT INTO test_tab(requested_time) VALUES (TO_DATE('08-AUG-2021 02:10','DD-MON-YYYY HH24:MI'));
5 END;
6 /
PL/SQL procedure successfully completed.
pdb1--KOEN>SELECT COUNT(*) FROM test_tab WHERE requested_time = TO_DATE('08-AUG-2021','DD-MON-YYYY');
COUNT(*)
----------
1
--only 1 row. That is the rows with time 00:00. Other rows are ignored
pdb1--KOEN>SELECT COUNT(*) FROM test_tab WHERE TRUNC(requested_time) = TO_DATE('08-AUG-2021','DD-MON-YYYY');
-- all rows
COUNT(*)
----------
3

can I get only the updated data from database instead of all the data

I am using sqlite3 in python 3 I want to get only the updated data from the database. what I mean by that can be explained as follows: the database already has 2 rows of data and I add 2 more rows of data. How can I read only the updated rows instead of total rows
Note: indexing may not help here because the no of rows updating will change.
def read_all():
cur = con.cursor()
cur.execute("SELECT * FROM CVT")
rows = cur.fetchall()
# print(rows[-1])
assert cur.rowcount == len(rows)
lastrowids = range(cur.lastrowid - cur.rowcount + 1, cur.lastrowid + 1)
print(lastrowids)
If you insert rows "one by one" like that
cursor.execute('INSERT INTO foo (xxxx) VALUES (xxxx)')
You then can retrieve the last inserted rows id :
last_inserted_id = cursor.lastrowid
BUT it will work ONLY if you insert a single row with execute. It will return None if you try to use it after a executemany.
If you are trying to get multiple ids of rows that were inserted at the same time see that answer that may help you.

How efficently change fetched data from django raw query

For my project, I needed to run a raw query to increase performance. The problem is here that for one part I need to change value of fetched data to sth else(gregorian date to jalali) and this causes my performance to decrease a lot.
cursor.execute("select date,number from my_db)
while True:
row = cursor.fetchone()
if row is None:
break
data.append(row)
This section longs about 1 min for 4 million data, but I need to chanfe date like this:
cursor.execute("select date,number from my_db)
while True:
row = cursor.fetchone()
if row is None:
break
row = list(row)
row[0] = (jdatetime.datetime.fromgregorian(datetime=row[0]).strftime( '%y/%m/%d, %H:%m'))
data.append(row)
this causes my code to run in 7 min. I wonder if there is a way to do this change efficiently
First part should not take 1 min to finish. Instead of raw sql and a loop, do
data = YourModel.objects.values_list('date', 'number')
For the second part, you can make your own SQL function.
https://raresql.com/tag/iranian-calendar-to-georgian-calendar-in-sql-server/

Returning more than one column between two criteria

I have an SQLite database containing three columns; EmpName, theTime, and theDate. I'm trying to run a query that will return rows with EmpName and theTime that fit a specific range of dates. My goal is to add all these times up and give an end-of-week total time. However, when I run my code, it returns a list of 0.
I've tried queries such as:
SELECT * FROM Swipes WHERE theDate>=:starting<=:endof", {'starting': fstartingDate, 'endof': formatted}
and
SELECT EmpName AND theTime FROM Swipes WHERE theDate >=? <=?, (fstartingdate, formatted,)
and
SELECT EmpName AND theTime FROM Swipes WHERE thedate BETWEEN ? AND ?", (fstartingDate, formatted)
and other variations of that approach, but it feels like I'm running in circles.
This is my code:
def weekSummary(endofthatweek):
formatted = dt.strptime(endofthatweek, '%Y-%m-%d')
startingDate = formatted - td(days=7)
fstartingDate = startingDate.date()
con = sqlite3.connect(r'C:\Users\zstrickland.RANDSMACHINE\Documents\PymeClock\testTimeclock.db')
cur = con.cursor()
cur.execute("SELECT EmpName AND theTime FROM Swipes WHERE theDate>=:starting<=:endof", {'starting': fstartingDate, 'endof': formatted})
tupes = cur.fetchall()
con.close()
detuped = [x[0] for x in tupes]
print(detuped)
I hope to get a list(Probably a list of tuples..) in the following format:
[(EmpName, theTime), (EmpName, theTime), (EmpName, theTime), (EmpName, theTime)].
Any help or suggestions on how to make this calculation would be helpful. Thank you!
These conditions:
theDate>=:starting<=:endof
and:
theDate >=? <=?
are not valid SQL conditions.
You need BETWEEN:
theDate BETWEEN :starting AND :endof
or:
theDate BETWEEN ? AND ?

how to find the sum of multiple numbers from a column in a sql database in python?

I have a database that has a bookings table in. One of the columns in the bookings table is 'incomes', and another one is 'date_of_booking,' which stores dates in 'DD/MM/YYYY' format. I am trying to write a feature that lets a user input a month, and from that will calculate all the incomes from that month. So far I have this:
validMonth = False
while not validMonth:
lookForMonth = input('What month, please? (number from 1 through 12):')
try:
validMonth = 1<=int(lookForMonth)<=12
except:
pass
sqlCmd = 'SELECT date FROM bookings WHERE SUBSTR(date,4,2)="%.2i"' % int(lookForMonth)
for row in conn.execute(sqlCmd):
print (row)
With this code, I am able to output the date of bookings for a particular month. However I want to output the total incomes for a particular month. What do I need to add so that this works out the total incomes for a particular month and outputs it? Any help would be gladly appreciated, thanks.
Replace one statement.
SELECT sum(income) FROM bookings where SUBSTR(date,4,2)='04'
As in:
import sqlite3
conn = sqlite3.connect(':memory:')
c = conn.cursor()
c.execute('CREATE TABLE bookings (date text, income real)')
c.execute('''INSERT INTO bookings VALUES ('01/04/2017', 19.22)''')
c.execute('''INSERT INTO bookings VALUES ('15/04/2017', 19.22)''')
c.execute('''INSERT INTO bookings VALUES ('22/04/2017', 19.22)''')
validMonth = False
while not validMonth:
lookForMonth = input('What month, please? (number from 1 through 12):')
try:
validMonth = 1<=int(lookForMonth)<=12
except:
pass
sql = '''SELECT sum(income) FROM bookings where SUBSTR(date,4,2)="%.2i"''' % int(lookForMonth)
for row in c.execute(sql):
print (row)
Resulting output:
What month, please? (number from 1 through 12):4
(57.66,)
First of all, you want to select both in your sql statement.
sqlCmd = 'SELECT date_of_booking,incomes FROM bookings WHERE SUBSTR(date,4,2)="%.2i"' % int(lookForMonth)
income_sum = 0
for (row_date, row_income) in conn.execute(sqlCmd):
income_sum += row_income
print row_date
print income_sum
Then you can specify both date and income of the row in your loop like above.

Categories

Resources