I have two tables in my database. The columns in both tables and their datatypes are shown below. let say both tables store the data of 3 machines. Each machine has two s_id's and by using them i will select the required data of a particular machine.
the s_id's for each machine are
m1 59,07
m2 60,92
m3 95,109
Table "public.table_a"
Column | Type | Modifiers | Storage | Stats target | Description
--------------+-----------------------------+-----------+---------+--------------+-------------
ettime | timestamp without time zone | | plain | |
sn | numeric | | main | |
s_id1 | numeric | | main | |
e_id1 | numeric | | main | |
Indexes:
"table_a_sn_key" UNIQUE CONSTRAINT, btree (sn)
Has OIDs: no
Table "public.table_b"
Column | Type | Modifiers | Storage | Stats target | Description
--------------+-----------------------------+-----------+----------+--------------+-------------
sn | numeric | | main | |
ettime | timestamp without time zone | | plain | |
value | text | | extended | |
comment | text | | extended | |
l_id | numeric | | main | |
n_id | numeric | | main | |
ettime.y | timestamp without time zone | | plain | |
s_id2 | numeric | | main | |
e_id2 | numeric | | main | |
Indexes:
"table_b_sn_key" UNIQUE CONSTRAINT, btree (sn)
Has OIDs: no
by using the below script i will get the desired result.
library(RPostgreSQL)
M1 <- dbGetQuery(con, "select
a.r_date::date date,
downgraded,
total,
round(downgraded::numeric/total* 100, 2) percentage
from (
select date_trunc('day', eventtime) r_date, count(*) downgraded
from table_b
where s_id2 in (59,07)
group by 1
) b
join (
select date_trunc('day', eventtime) r_date, count(*) total
from table_a
where s_id1 in (59,07)
group by 1
) a
using (r_date)
order by 1")
since i am not from the programming background i am using the above complete query statement for each machine
M2 <- dbGetQuery(con, "select
a.r_date::date date,
downgraded,
total, ........
........
M3 <- dbGetQuery(con, "select
a.r_date::date date,
downgraded,
total,.....
.........
Instead of using query for each machine, is it possible to use a loop in my case. so that in one query i will get all machines data.
can someone tell me how to do these with my example. In reality i need to run the 6 seperate scripts and in each script i need data of three different machines.