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;
Related
The join returns the view I want but when I go to create the table I get 0 rows processed. I'm sure I'm missing something simple but this is my first time creating tables using queries. Help is appreciated.
THE JOIN:
Select
a.*,
b.acct_sk,
b.voice_estbd_dt,
b.data_estbd_dt,
b.video_estbd_dt,
b.voice_term_dt,
b.data_term_dt,
b.video_term_dt,
b.voice_svc_type_id,
b.voice_acct_status_id,
b.data_svc_type_id,
b.data_acct_status_id,
b.video_svc_type_id,
b.video_acct_status_id,
b.video_svc_type,
b.data_down_spd_mb,
b.data_up_spd_mb,
b.dsl_down_spd_mb,
b.dsl_up_spd_mb,
b.fios_down_spd_mb,
b.fios_up_spd_mb,
b.prov_speed,
b.nbr_voice_lines,
b.tot_trouble_tkts,
b.trouble_tkts_rslvd,
b.trouble_tkts_not_rslvd,
b.tot_truck_rolls,
b.run_date,
b.snapshot_dt,
b.hsi_line_count
from xxx.COVID19_LTRS a
left joinxx.CAR_SERVICE b
ON a.acct_sk = b.acct_sk
AND snapshot_dt = DATE '2020-02-01'
THE CREATE TABLE:
CREATE TABLE xxx.aw_smbsuspend_dev_1 as
(Select
a.acct_sk,
a.product_id,
a.order_save_date,
a.comments,
a.b_name,
a.bill_addr,
a.bill_city,
a.bill_state,
a.bill_zip5,
a.bill_zip4,
a.marketing_primary_segment,
a.ord_creatd_dt,
b.voice_estbd_dt,
b.data_estbd_dt,
b.video_estbd_dt,
b.voice_svc_type_id,
b.voice_acct_status_id,
b.data_svc_type_id,
b.data_acct_status_id,
b.video_svc_type_id,
b.video_acct_status_id,
b.video_svc_type,
b.data_down_spd_mb,
b.data_up_spd_mb,
b.dsl_down_spd_mb,
b.dsl_up_spd_mb,
b.fios_down_spd_mb,
b.fios_up_spd_mb,
b.prov_speed,
b.nbr_voice_lines,
b.run_date,
b.snapshot_dt,
b.hsi_line_count
from xxx.COVID19_LTRS a
left join xx.CAR_SERVICE b
ON a.acct_sk = b.acct_sk
and b.snapshot_dt = DATE '2020-02-01') with data;
I have tried adding a group by clause and changing and to where...I am now just adding text because I'm being told to add more explanation but it is a very straightforward problem with a lot of columns.
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
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 need to select data from four tables based on only one.
In my 'calculated' table, I have all the records I need.
But I need to retrieve some other info for each record, from 'programs', 'term' and 'imported' tables.
'calculated' has ID from 'programs'.
But, to achieve a record from 'imported', I need to join the 'item' table, because 'item' has ID from 'programs' and from 'imported'.
'term' has ID from 'imported'.
So, I tried this:
select c.date,
p.name,
c.name1,
c.name2,
t.date,
i.version,
c.price1,
c.price2,
c.price3
from calculated c, programs p, term t, imported i, item it
where c.programs_id = p.programs_id
and c.programs_id = it.programs_id
and it.imported_id = i.imported_id
and i.term_id = t.term_id;
But when I use count(*) on 'calculated', I get 30k of records, and from my select statement I get more than 130 millions of records.
What am I doing wrong?
What should I do for this to work?
If all duplicates rows are equivalent, u can try smth like this
select c.date,
p.name,
c.name1,
c.name2,
t.date,
i.version,
c.price1,
c.price2,
c.price3
from calculated c, programs p, term t, imported i
where c.programs_id = p.programs_id and
(select imported_id from item it where c.programs_id = it.programs_id and rownum = 1) = i.imported_id
and i.term_id = t.term_id;
where "rownum = 1" is restriction on the selection of one line for oracle.
you forgot to join term table.
Probably you need to add
and t.term_id = i.term_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