I have a problem in this code:
select idingrediente
into niding
from ingredientes
inner join RECHASING on ingredientes.IDINGREDIENTE = RECHASING.IDINGREDIENTE
inner join receta on RECHASING.RIDRECETA = receta.IDRECETA;
Error:
Column ambiguously defined
Table ingredientes:
idingrediente
ingrediente
cantingrediente
Table rechasing:
idingrediente
idreceta
Table receta:
idReceta
Cantingrediente
rechasing is a intermediate table because receta has n:n ingredientes.
Thanks
Just give aliases to your tables, then identify the idingrediente column with an alias:
SELECT t1.idingrediente -- or t2.idingrediente
INTO niding
FROM ingredientes t1
INNER JOIN rechasing t2
ON t1.idingrediente = t2.idingrediente
INNER JOIN receta t3
ON t2.ridreceta = t3.idreceta
Related
I have a large query which combines a lot of tables. I don't want to make this question to complicate so I will try to ask it easy:
suppose I have 3 columns:
table1_device_id, table2_device_id, table3_device_id
I know want to achieve this:
Do a left join on table1_device_id, if that column is null, do a left join on table2_device_id,.. (same for table 3..)
I have following example for 1 table now:
left join query_505727 as inside_weather_data on y.datetime_weather = inside_weather_data.time AND (a.monitor_id is null or a.monitor_id = inside_weather_data.device_id)
but how to do that for multiple?
my query:
select y.datetime_weather, a.site_name as site_name,a.name as house_name, a.name1 as floor_name,a.airspace_name as room_name, a.registration,a.monitor_id, device_color_table.device_id as device_color_device_id, inside_weather_data.device_id as weather_device_id, rdi_data.device_id as rdi_device_id, a.monitor_code as monitor_code, a.alarm_type,a.created_at_date as alarm_date,device_color_table.colors as LED_color_status, rdi_data.value as RDI, ROUND(inside_weather_data.min_inside_temp, 2) as min_inside_temp, ROUND(inside_weather_data.max_inside_temp, 2) as max_inside_temp, y.max_temp as max_outside_temp, y.min_temp as min_outside_temp, y.average_temp as avg_temp, a.alarm_type, a.priority, a.translation_code,a.created_at
from query_505641 as y
left join query_505127 as a on a.created_at_date = y.datetime_weather
left join query_505241 as device_color_table on device_color_table.datetime = y.datetime_weather AND device_color_table.device_id = a.monitor_id
left join query_506556 as rdi_data on rdi_data.time = y.datetime_weather and rdi_data.device_id = a.monitor_id AND device_color_table.device_id is null
left join query_505727 as inside_weather_data on y.datetime_weather = inside_weather_data.time AND inside_weather_data.device_id = a.monitor_id AND coalesce(device_color_table.device_id, rdi_data.device_id) is null
If the table that you want to join to these 3 tables is table then use these LEFT joins:
....
from table t
left join table1 t1 on t1.table1_device_id = t.device_id
left join table2 t2 on t2.table2_device_id = t.device_id and t1.table1_device_id is null
left join table3 t3 on t3.table3_device_id = t.device_id and coalesce(t1.table1_device_id, t2.table2_device_id) is null
In SQLite, I have the following query
SELECT x.nameIndex, y.nameIndex
FROM relation x, relation y
WHERE x.label=y.label AND x.feature=1 AND y.feature=0;
which returns all pairs of x.nameIndex,y.nameIndex with the same label where x has feature 1 and y has feature 0.
Now I have another table index2name where I store the name for each index, where I could do like:
SELECT name FROM index2name WHERE nameIndex=...;
How can I change the top query such that it looks up the name for the respective indeces and returns pairs of names instead?
Use a CTE which returns the name instead of the indexes and the group for each row in relation (by a join to index2name) and do a self join on that:
WITH cte AS (
SELECT i.name, r.label, r.feature
FROM relation r INNER JOIN index2name i
ON i.nameIndex = r.nameIndex
)
SELECT c1.name, c2.name
FROM cte c1 INNER JOIN cte c2
ON c2.label = c1.label
WHERE c1.feature=1 AND c2.feature=0;
Or without the CTE:
SELECT i1.name, i2.name
FROM relation r1 INNER JOIN relation r2
ON r2.label = r1.label
INNER JOIN index2name i1 ON i1.nameIndex = r1.nameIndex
INNER JOIN index2name i2 ON i2.nameIndex = r2.nameIndex
WHERE r1.feature=1 AND r2.feature=0;
I need any help getting data from three tables.
This is my setup:
TableA with two columns: id, name
TableB with three columns: id_a, id_c1, id_c2
TableC with two columns: id, name
I want as result the following table:
TableA.name,TableC.name,TableC.name
where the TableC.name(s) are expansion of id_c1, id_c2 of TableB.
Any idea?
Very thanks
You need to join the 3 tables like this:
select a.name, c1.name, c2.name
from tablea a inner join tableb b
on b.id_a = a.id
inner join tablec c1
on c1.id = b.id_c1
inner join tablec c2
on c2.id = b.id_c2
Maybe instead of inner joins you need left joins, but I can't tell for sure.
I'm trying to get the result into a variable (is it possible to do it as a %rowtype of an existing table? probably not because of conflicting columns) where it would display me all the values where the two refs overlap and the i_ref(which is inputted ) also overlaps with the ref from t1.
select *
into aRow
from table1 t1
where t1.ref = i_ref
and (select * from table2 t2 where t1.ref = t2.ref);
What am I doing wrong with my select?
You can join tables instead of nested subquery:
select t1.*
into aRow
from table1 t1 join table2 t2 on t1.ref = t2.ref
where t1.ref = i_ref
I have a below working query:
`select distinct t1.ExNo,
t3.ExDesc from table1 t1
left join table2 t2 on t1.ExNo = t2.ExNo
join table3 t3 on t3.ExTyp = case
when t1.ExTyp = 'CONT'
and t2.ExTyp is not null
then t2.ExTyp
else t1.ExTyp
end
order by t1.ExNo;`
But if the same query modified as below and use, I am getting this error. Could you please rectify:
`select distinct t1.ExNo,
t3.ExDesc
from table1 t1 t1, table2 t2,table3 t3 where
t1.ExNo *= t2.ExNo and
t3.ExTyp = case
when t1.ExTyp = 'CONTNR'
and t2.ExTyp is not null
then t2.ExTyp
else t1.ExTyp
end
order by t1.ExNo;`
Error:
The table is an inner member of an outer-join clause. This is not allowed if the table also participates in a regular join clause.