select * from table1 t1 where t1.column1 = 'someValue' and ((t1.column2 =1) OR (sysdate < select t1.DateColumn2 + t2.DateColumn2/1440
from
table2 t2 where t1.column3 = t2.column3));
if t1.column2 =1 evaluates to false, I want to check another condition if time t1.DateColumn2 + t2.DateColumn2 is < sysdate . Oracle is throwing syntax error near or condition. Cant sysdate be used directly like that? Not sure where I am going wrong. Thanks
If I am guessing your intention correctly, you want an exists clause
select *
from table1 t1
where t1.column1 = 'someValue'
and ( (t1.column2 =1)
OR exists( select 1
from table2 t2
where t2.column3 = t1.column3
and sysdate < t1.DateColumn2 + t2.DateColumn2/1440 ));
Or just join the two tables in the outer query assuming there is at most 1 row in t2 per t1 row (if there is exactly 1 row you should do an inner join rather than a left outer join)
select t1.*
from table1 t1
left outer join table2 t2
on( t1.column3 = t2.column3 )
where t1.column2 = 1
or sysdate < t1.DateColumn2 + t2.DateColumn2/1440;
Related
How would the following START WITH / CONNECT BY hierarchical query look like when translated into a RECURSIVE SUBQUERY FACTORING hierarchical query with WITH clause:
SELECT t1.id
FROM table1 t1, table2 t2
WHERE t1.version_id = t2.id
AND t1.baseline_date = TRIM (TO_DATE ('2015-05-26', 'yyyy-mm-dd'))
AND t2.entry_date = t1.baseline_date
START WITH t1.id IN (SELECT id
FROM table1
WHERE parent_id = 101015)
CONNECT BY PRIOR t1.id = t1.parent_id
ORDER SIBLINGS BY t1.child_index;
I think you want:
WITH rsqfc (id, child_index, baseline_date) AS (
SELECT t1.id,
t1.child_index,
t1.baseline_date
FROM table1 t1
INNER JOIN table2 t2
ON ( t1.version_id = t2.id
AND t2.entry_date = t1.baseline_date )
WHERE t1.parent_id = 101015
UNION ALL
SELECT t1.id,
t1.child_index,
t1.baseline_date
FROM rsqfc r
INNER JOIN table1 t1
ON (r.id = t1.parent_id)
INNER JOIN table2 t2
ON ( t1.version_id = t2.id
AND t2.entry_date = t1.baseline_date )
)
SEARCH DEPTH FIRST BY child_index SET order_id
SELECT id
FROM rsqfc
WHERE baseline_date = DATE '2015-05-26';
However, without sample data it is difficult to be sure.
I'm trying to create an update query that concatenates 3 fields from a table to 1 field in another table
The first table called table1
ID DESC
12 left:Middle:Right
The second table Table 2
ID FLD1 FLD2 FLD3
12 left Middle Right
Trying to update all the desc field on Table1 with the values of table2 where table1.id = table2.id
update table1 A SET A.DESC = (SELECT CONCAT(B.fld1, ':', B.fld2, ':', B.fld3)
from table2 B
where A.ID = B.ID)
Where A.id = 12;
However, I'm getting an error from the above query saying "invalid number of arguments" Any idea what am I doing wrong? or how can I get this done in a better way?
CONCAT accepts only two parameters, which means that you have to use nested CONCATs.
Though, you'd rather use the double pipe || operator which doesn't have such a restriction. So:
update table1 A SET A.DESC = (SELECT B.fld1 ||':'|| B.fld2 ||':'|| B.fld3 --> this
from table2 B
where A.ID = B.ID)
Where A.id = 12;
To update all matching rows, you could
update table1 A SET A.DESC = (SELECT B.fld1 ||':'|| B.fld2 ||':'|| B.fld3 --> this
from table2 B
where A.ID = B.ID)
Where exists (select null
from table2 b
where a.id = b.id);
or MERGE:
merge into table1 a
using table2 b
on (b.id = a.id)
when matched then update set a.desc = b.fld1 ||':'|| b.fld2 ||':'|| b.fld3;
As you got duplicates, DISTINCT might help, e.g.
update table1 a set
a.desc = (select distinct b.fld1 ||':'|| b.fld2 ||':'|| b.fld3
from table2 b
where a.id = b.id
)
where exists ...
If not, then you'll have to see what to do with these duplicates. If possible, use yet another column(s) in WHERE clause. Or, if you don't really care which concatenated combination fits, use aggregate function(s) such as MIN or MAX, e.g.
update table1 a set
a.desc = (select max(b.fld1 ||':'|| b.fld2 ||':'|| b.fld3)
from table2 b
where a.id = b.id
)
where exists ...
I have two tables, T1 and T2 with same set of columns. I need to issue a query which will return me value of columns from either table whichever is not null. If both columns are null return null as the value of that column.
The columns are c1,c2,c3,cond1.
I issued the following query. The problem is that if one subquery fails the whole query fails. Somebody please help me. Probably there is another simple way.
SELECT NVL(T1.c1, T2.c1) c1,NVL(T1.c2, T2.c2) c2,NVL(T1.c3, T2.c3) c3
FROM (SELECT c1,c2,c3
FROM T1
WHERE cond1 = 'T10') T1
,(SELECT c1,c2,c3
FROM T2
WHERE cond1 = 'T200') T2 ;
You need something like this:
SELECT NVL((SELECT T1.c1
FROM T1
WHERE T1.c2 = 'T10'),
(SELECT T2.c1
FROM T2
WHERE T2.c2 = 'T200')) AS c1
FROM dual
Or you may prefer a full outer join:
SELECT NVL(T1.c1, T2.c1) AS c1
FROM T1 FULL OUTER JOIN T2 ON 1=1
WHERE T1.c2 = 'T10'
AND T2.c2 = 'T200'
Your result is logical. If the first table is null no combination of values will exist in the natural join.
EDIT. After some new requirements we can use a hack to get the row. Lets get all three possibilities, T1, T2 or all nulls and select the first one:
SELECT *
FROM ( (SELECT T1.*
FROM T1
WHERE T1.c2 = 'T10')
UNION ALL
(SELECT T2.*
FROM T2
WHERE T2.c2 = 'T200')
UNION ALL
(SELECT T2.*
FROM dual
LEFT JOIN T1 ON 1 = 0 ) )
WHERE ROWNUM = 1
I have following problem:
I want to count data in one table, count data in second table and compare countings in having clause, and display only that rows which have the same countings
Something like that:
SELECT bla
FROM T1 t1 JOIN T2 t2
ON t1.id = t2.id
HAVING COUNT(counted data from table1) = COUNT(counted data from table2)
Do you have any idea?
Cheers
Standard SQL:
SELECT t1.bla, t1.id, t1.counter, t2.counter
FROM (SELECT t1.bla, t1.id, COUNT(counted_data_from_t1) AS counter
FROM t1
GROUP BY t1.bla, t1.id
) AS t1
JOIN (SELECT t2.id, COUNT(counted_data_from_t2) AS counter
FROM t2
GROUP BY t2.id
) AS t2
ON t1.id = t2.id AND t1.counter = t2.counter
Oracle SQL (because Oracle doesn't like AS before table aliases):
SELECT t1.bla, t1.id, t1.counter, t2.counter
FROM (SELECT t1.bla, t1.id, COUNT(counted_data_from_t1) AS counter
FROM t1
GROUP BY t1.bla, t1.id
) t1
JOIN (SELECT t2.id, COUNT(counted_data_from_t2) AS counter
FROM t2
GROUP BY t2.id
) t2
ON t1.id = t2.id AND t1.counter = t2.counter
You just have to decide where bla comes from; I nominated t1. I'm assuming that for any given value of t1.id, there is a single value of t1.bla. If there isn't, then you need to explain much more clearly what you're counting and where the various columns are, and what the keys of the tables are.
Update: Apologies for not noticing the Oracle tag and giving invalid Oracle syntax.
WITH jezyki as
(SELECT pseudo_wampira, COUNT(*) AS counter
FROM Jezyki_obce_w
GROUP BY pseudo_wampira
)
,sprawnosc as
(SELECT pseudo_wampira, sprawnosc, COUNT(*) AS counter
FROM Sprawnosci_w
GROUP BY pseudo_wampira, sprawnosc
)
SELECT jezyki.pseudo_wampira, sprawnosc.counter
FROM jezyki,sprawnosc
WHERE jezyki.pseudo_wampira = sprawnosc.pseudo_wampira
AND jezyki.counter = sprawnosc.counter
i m having a little syntax problem in my query (simplified) :
select *
from table1 t1
inner join table2 t2 using (pk1)
inner join table3 t3 using (pk2)
where not exists (select1 from table4 t4 where t4.pk1 = t1.pk1)
By using the "using" keyword, oracle doesnt allow table identifier in front of the column name (eg : t1.pk1, only pk1 can be used)
If i write :
select *
from table1 t1
inner join table2 t2 using (pk1)
inner join table3 t3 using (pk2)
where not exists (select1 from table4 t4 where t4.pk1 = pk1)
This query will not give the expected results.
But since i am using an "exists" subquery, how can i join this subquery ?
Of course, i suppose i could write this query another way and avoid the exists, or i could NOT use "using".
But is it possible to have "join / using" combined with a subquery in the where clause ?
Edit : using Oracle 10gR2
Interesting problem! The best I can manage while still using USING is:
select * from
( select *
from table1 t1
inner join table2 t2 using (pk1)
inner join table3 t3 using (pk2)
) v
where not exists (select1 from table4 t4 where t4.pk1 = v.pk1)
You cannot use the table qualifiers with natural joins.
This query:
select 1 from table4 t4 where t4.pk1 = pk1
is being parsed as
select 1 from table4 t4 where t4.pk1 = t4.pk1
and NOT EXISTS over it always returns false if there is but a single record in table4.
Just use explicit JOIN conditions:
WITH table1 AS
(
SELECT 1 AS pk1
FROM dual
),
table2 AS
(
SELECT 1 AS pk1, 1 AS pk2
FROM dual
),
table3 AS
(
SELECT 1 AS pk2
FROM dual
),
table4 AS
(
SELECT 2 AS pk1
FROM dual
)
SELECT *
FROM table1 t1
JOIN table2 t2
ON t2.pk1 = t1.pk1
JOIN table3 t3
ON t3.pk2 = t2.pk2
WHERE NOT EXISTS
(
SELECT 1
FROM table4 t4
WHERE t4.pk1 = t1.pk1
)