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
Related
I need Only one unique result from tableB.Field to tableA.Field
I am using sdo operator sdo_nn, this is the code:
UPDATE table1 t1
SET t1.fieldA = (SELECT T2.fieldB,SDO_NN_DISTANCE(1) distance
FROM table1 T1, table2 T2
WHERE
(sdo_nn(t1.geometry,t2.geometry,'SDO_NUM_RES=1',1)= 'TRUE')
ORDER BY DIST
)
WHERE EXISTS(
SELECT 1
FROM table2 t2
WHERE sdo_nn(t1.geometry, t2.geometry,'SDO_NUM_RES=1',1)='TRUE'
AND(t2.cell_name = 'string1' or t2.cell_name = string2')AND t1.fieldA = NULL
);
In the select sentence of the subquery i get an error because i only use one field(t1.fieldA), but in the sentence i use the operator SDO_NN_DISTANCE(1) and the sql developer count this operator like another field. What is the correct way to write this sentence? I only use sql because i need to insert this code in vba
Thanks!!!
Obviously, you can't (simplified)
set t1.fieldA = (t2.fieldB, distance) --> you want to put two values into a single column
Therefore, get fieldB alone from the subquery which uses analytic function (row_number) to "sort" rows by sdo_nn_distance(1) desc; then get the first row's fieldB value.
Something like this (I hope I set the parenthesis right):
UPDATE table1 t1
SET t1.fieldA =
(SELECT x.fieldB --> only fieldB
FROM (SELECT T2.fieldB, --> from your subquery
SDO_NN_DISTANCE (1) distance,
ROW_NUMBER ()
OVER (ORDER BY sdo_nn_distance (1) DESC) rn
FROM table1 T1, table2 T2
WHERE (sdo_nn (t1.geometry,
t2.geometry,
'SDO_NUM_RES=1',
1) = 'TRUE')) x
WHERE rn = 1) --> where RN = 1
WHERE EXISTS
(SELECT 1
FROM table2 t2
WHERE sdo_nn (t1.geometry,
t2.geometry,
'SDO_NUM_RES=1',
1) = 'TRUE'
AND ( t2.cell_name = 'string1'
OR t2.cell_name = 'string2')
AND t1.fieldA 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;
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 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.
My scenario is this:
I have a table with a structure like this (simplified) -
CREATE TABLE [dbo].[pe_mem](
[pm_member] [int] NULL,
[pm_surname] [char](50) NULL,
[pm_forename] [char](50) NULL,
[pm_rsi_num] [char](11) NULL
) ON [PRIMARY]
I need to run a query to find all rows that have an identical pm_rsi_num but a different pm_surname.
Can anyone help me out with this?
Thanks!
You can use a self join for that:
select *
from pe_mem t1
join pe_mem t2
on t1.pm_rsi_num = t2.pm_rsi_num
and t1.pm_surname <> t2.pm_surname
Exists variant:
select *
from pe_mem t1
where exists
(select null
from pe_mem t2
where t1.pm_rsi_num = t2.pm_rsi_num
and t1.pm_surname <> t2.pm_surname)
Single table scan version:
select pm_rsi_num
from pe_mem
group by pm_rsi_num
having count(distinct pm_surname) > 1
Just join the table back on itself and use your criteria as the join criteria:
select * from pe_mem as p1
inner join pe_mem as p2
on p1.pm_rsi_num = p2.pm_rsi_num
and p1.pm_surname <> p2.pm_surname