Looking to Inner join the results from one query with another in the SQLite Package in R. So far I have the following code:
library(SQLite)
sql<-
"SELECT
telecast.telecast_id,
telecast.series_name,
affiliates.network_name
FROM telecast a
JOIN affiliates b
ON a.network_id = b.network_id limit 10;"
res <- dbSendQuery(con, sql)
df_ti <- dbFetch(res)
print(df_ti)
When I run the code, it says there is no table found but when I run just
SELECT telecast.telecast_id FROM telecast
The information shows up. I am not too good with troubleshooting in SQLite in R, any thoughts on how to fix this??
You create aliasses for your tables.
If you change your select query to use these, I had it working.
library(SQLite)
sql<-
"SELECT
a.telecast_id,
a.series_name,
b.network_name
FROM telecast a
JOIN affiliates b
ON a.network_id = b.network_id limit 10;"
res <- dbSendQuery(con, sql)
df_ti <- dbFetch(res)
print(df_ti)
I was able to figure it out. Your suggestion of creating aliases for the tables was spot on. I just declared each table an alias using AS. The code I was able to come up with is below, thanks for the help!
SELECT
t.series_name,
ti.num_views_per_telecast
FROM
(SELECT
ti.telecast_id,
ti.network_id,
count(*) as num_views_per_telecast
FROM
tunein AS ti
INNER JOIN
affiliates AS a
ON
ti.network_id = a.network_id
WHERE
ti.dvr_time_shift = 'L+SD' and
a.network_name = 'ABC'
group by
ti.telecast_id,
ti.network_id
)ti
inner join telecast AS t
On
t.telecast_id = ti.telecast_id
ORDER BY ti.num_views_per_telecast DESC
Related
how can i update a table column from another table. here is my code :
update table1 hn
set hn.changeColumn=es.changeColumn
from table1 hn
inner join table2 es on es.x=hn.xand es.rol_id=hn.rol_id
where hn.x= es.x and hn.rol_id = es.rol_id
i wanna set table1's column(changeColumn) values with table2's column(changeColumn) values
how can i do this. thanks
If you want to convert this query to Oracle, You need a MERGE INTO statement -
MREGE INTO table1 hn
USING table2 es
ON (es.x=hn.x and es.rol_id=hn.rol_id)
WHEN MATCHED THEN
UPDATE
SET hn.changeColumn=es.changeColumn
One valid way to write this query on Oracle would be:
UPDATE table1 t1
SET changeColumn = (SELECT t2.changeColumn
FROM table2 t2
WHERE t1.x = t2.x AND t1.rol_id = t2.rol_id);
This assumes that your join condition would only generate at most a single pair of mstching records from the self join. If not, then we would have to modify the logic.
I have two tables: stop_times and trips -- both tables share trip_id. I want to build a query so that I extract information from both tables using trip_id, but then "filter" that information based on something from the first table.
I came up with this to get my information without the "filtering":
SELECT stop_times.trip_id, stop_times.arrival_time, trips.route_id
FROM stop_times, trips
WHERE stop_times.trip_id = trips.trip_id;
This works fine, but then I try to "filter" using stop_id:
SELECT stop_times.trip_id, stop_times.arrival_time, trips.route_id
FROM stop_times, trips
WHERE stop_times.stop_id IN (SELECT stop_id
FROM stop_times
WHERE stop_id = '81');
but it merges information incorrectly.
Why did you remove WHERE stop_times.trip_id = trips.trip_id from the second query? This might work.
SELECT stop_times.trip_id, stop_times.arrival_time, trips.route_id
FROM stop_times, trips
WHERE stop_times.trip_id = trips.trip_id
AND stop_times.stop_id = 81;
But you'd be better off using ANSI join syntax.
SELECT stop_times.trip_id, stop_times.arrival_time, trips.route_id
FROM stop_times
INNER JOIN trips ON stop_times.trip_id = trips.trip_id
WHERE stop_times.stop_id = 81;
Using PL-SQL, I need to find the record with the lastest INVC_LN_ITEM_STAT_START_DT value within a group of records that share the same value for SHPMNT_LN_ITEM_KEY and RPT_PER_KEY.
How else might this be done? Are there analytical functions for this type of query?
SELECT
m1.*
FROM
HD_INVC_LN_ITEM_STAT m1
LEFT OUTER JOIN HD_INVC_LN_ITEM_STAT m2
ON (
m1.SHPMNT_LN_ITEM_KEY = m2.SHPMNT_LN_ITEM_KEY
AND m1.RPT_PER_KEY = m2.RPT_PER_KEY
AND m1.INVC_LN_ITEM_STAT_START_DT < m2.INVC_LN_ITEM_STAT_START_DT)
WHERE
m2.SHPMNT_LN_ITEM_KEY IS NULL
ORDER BY
m1.SHPMNT_LN_ITEM_KEY
,m1.RPT_PER_KEY
,m1.INVC_LN_ITEM_STAT_CD
,m1.INVC_LN_ITEM_STAT_START_DT
How about this?
SELECT
HD_INVC_LN_ITEM_STAT1.*
FROM
HD_INVC_LN_ITEM_STAT HD_INVC_LN_ITEM_STAT1
INNER JOIN
(
SELECT
HD_INVC_LN_ITEM_STAT2.SHPMNT_LN_ITEM_KEY
,HD_INVC_LN_ITEM_STAT2.RPT_PER_KEY
,MAX(HD_INVC_LN_ITEM_STAT2.INVC_LN_ITEM_STAT_START_DT) AS MAX_INVC_LN_ITEM_STAT_START_DT
FROM
HD_INVC_LN_ITEM_STAT HD_INVC_LN_ITEM_STAT2
GROUP BY
HD_INVC_LN_ITEM_STAT2.SHPMNT_LN_ITEM_KEY
,HD_INVC_LN_ITEM_STAT2.RPT_PER_KEY
) HD_INVC_LN_ITEM_STAT2
ON
HD_INVC_LN_ITEM_STAT1.SHPMNT_LN_ITEM_KEY = HD_INVC_LN_ITEM_STAT2.SHPMNT_LN_ITEM_KEY
AND HD_INVC_LN_ITEM_STAT1.RPT_PER_KEY
= HD_INVC_LN_ITEM_STAT2.RPT_PER_KEY
AND HD_INVC_LN_ITEM_STAT1.INVC_LN_ITEM_STAT_START_DT = HD_INVC_LN_ITEM_STAT2.MAX_INVC_LN_ITEM_STAT_START_DT
ORDER BY
HD_INVC_LN_ITEM_STAT1.SHPMNT_LN_ITEM_KEY
,HD_INVC_LN_ITEM_STAT1.RPT_PER_KEY
,HD_INVC_LN_ITEM_STAT1.INVC_LN_ITEM_STAT_CD
,HD_INVC_LN_ITEM_STAT1.INVC_LN_ITEM_STAT_START_DT;
It's longer but arguably more intuitive. I would like other people's opinions on which is more efficient.
I am trying to update a field in a table from another table using INNER JOIN. Here is the code:
UPDATE TestResults
INNER JOIN Distractors
ON TestResults.DistractorID = Distractors.ID
SET TestResults.DistractorValue = Distractors.DistractorValue
This does not work I don't know why! Any idea? When I run the query I get the following error
There was an error parsing the query. [ Token line number = 2,Token line offset = 1,Token in error = INNER ]
Not all databases support join syntax with update. And when they do, the syntax differs. Here is a way to do your query without an explicit join using standard SQL:
UPDATE TestResults
set DistractorValue = (select max(d.DistractorValue)
from Distractors d
where TestResults.DistractorValue = d.DistractorValue
)
where exists (select 1
from Distractors d
where TestResults.DistractorValue = d.DistractorValue
);
The max() is only needed if there could be more than one matching row.
The where is only needed if the join is intended to do filtering as well as matching.
UPDATE TestResults
SET TestResults.DistractorValue = Distractors.DistractorValue
FROM TestResults
INNER JOIN Distractors
ON TestResults.DistractorID = Distractors.ID
You use the Updated table in the Inner join clause
ex:
UPDATE TestResults
SET TestResults.DistractorValue = Distractors.DistractorValue
FROM TestResults INNER JOIN Distractors
ON TestResults.DistractorID = Distractors.ID
I have three tables doodhiya, doodhdata and cashdata.
I am trying to join these and fetch some needed data by this code but not succeed
SELECT dname,ddate,dmonth,dyear,dmilk,uid
FROM doodhiya
INNER JOIN doodhiya.dhid = doodhdata.ddhid
INNER JOIN doodhdata.dhid = cashdata.uid
WHERE (dname='$mik' AND dmonth='$mikdatem' AND dyear='$mikdatey')
ORDER BY ddate ASC
What I have to do?
You are missing the table name and the ON keyword in your join
SELECT dname,ddate,dmonth,dyear,dmilk,uid
FROM doodhiya
INNER JOIN doodhdata ON doodhiya.dhid = doodhdata.ddhid
INNER JOIN cashdata ON doodhdata.dhid = cashdata.uid
WHERE (dname='$mik' AND dmonth='$mikdatem' AND dyear='$mikdatey')
ORDER BY ddate ASC
A note on this part of the question:
Pls suggest me what I have to do
The syntax for a join is documented in the manual (and millions of SQL references in the web). So the best thing you can do the next time is to first consult the manual and/or a SQL reference.
SELECT retailername,
productname,
qty,
stock,
price,
discount
FROM temptablename,
productmaster,
retailermaster
WHERE temptablename.pid = productmaster.productid
AND temptablename.rid = retailermaster. retailercode
SELECT dname,
ddate,
dmonth,
dyear,
dmilk,
uid
FROM doodhdata,
cashdata
WHERE doodhiya.dhid = doodhdata.ddhid
AND doodhdata.dhid = cashdata.uid
AND dname='$mik'
AND dmonth='$mikdatem'
AND dyear='$mikdatey'
ORDER BY ddate ASC