I have a meteorological data set.
1. Query: First query returns me a list a all ids and idmms for active automatic measuring stations
sql_boberamp = """select a.id, a.idmm
from id_obs a, idmm b
where a.tip=4 and
a.datum_konca='10000-1-1'
and b.idmm=a.idmm and a.id in
(select c.id
from parametri c
where c.id_parametra>3005 and
c.datum_konca='10000-1-1') order by
a.idmm"""
cur1.execute(sql_boberamp)
boberamp_lst = cur1.fetchall()
Columns id and idmm can be turned into dictionary
2. Query: I pass in 3 parameters. Query counts for one amp how many times each observed parameter was measured for specific day.
ids = 2210
idmms = 11
params = idmms, idmms, idmms, ids
testtable_sql = []
sql_stevec = """select a.id_parametra,
case when a.id_parametra in
(3005,3010,3011,3040,3045,3046,3047, 3391)
then (select count(*) from amp_o b where
b.datum='2018-04-16' and b.par=a.id_parametra
and b.tip=4 and b.idmm=%s)
when a.id_parametra in (3120, 3121, 3124, 3420)
then (select count(*) from amp_p_10min b where
b.datum='2018-04-16' and b.par=a.id_parametra
and b.tip=4 and b.idmm=%s)
when a.id_parametra in (3180, 3181, 3188, 3189)
then (select count(*) from amp_v b where
b.datum='2018-04-16' and b.par=a.id_parametra
and b.tip=4 and b.idmm=%s)
end as stevec
from parametri a
where a.id=%s and datum_konca='10000-1-1' and
a.id_parametra>3000
order by a.id_parametra"""
cur.execute(sql_stevec,params)
testlist2_sql = cur.fetchall()
testtable_sql.append(testlist2_sql)
My goal is to get query 3 running.
3 Query: This FOR loop should run query 2 (query counts for one amp how many times each measured parameter was measured for specific day) for all my working amps.
ids = [2210, 2662,872]
idmms = [11,1948, 907]
testtable_for_sql = []
for row in ids:
ids_row = ids[0]
for row in idmms:
idmms_row = idmms[0]
params = idmms_row, idmms_row, idmms_row, ids_row
print(params)
cur.execute(sql_stevec,params)
testlist_for_sql = cur.fetchall()
testtable_for_sql.append(testlist_for_sql)
print('---- inside FOR loop ----',testtable_for_sql)
print('---- outside FOr loop ----',testtable_for_sql)
This is my result:
(11, 11, 11, 2210)
('---- inside FOR loop ----', [])
(11, 11, 11, 2210)
('---- inside FOR loop ----', [])
(11, 11, 11, 2210)
('---- inside FOR loop ----', [])
(11, 11, 11, 2210)
('---- inside FOR loop ----', [])
(11, 11, 11, 2210)
('---- inside FOR loop ----', [])
(11, 11, 11, 2210)
('---- inside FOR loop ----', [])
(11, 11, 11, 2210)
('---- inside FOR loop ----', [])
(11, 11, 11, 2210)
('---- inside FOR loop ----', [])
(11, 11, 11, 2210)
('---- inside FOR loop ----', [])
('---- outside FOr loop ----', [])
I don't understand, why it doesn't go through all three rows that are in my data for ids and idmms. I just can' crack this FOR loop in python script. Thank you very much for your help.