Select statement in Case statement in Oracle - sql

SELECT (CASE WHEN T.ID = ( SELECT cte.REFERENCE FROM trans cte WHERE T.ID
= CTE.PARENT_ID) THEN cte.REFERENCE ELSE null END) AS name
FROM trans T
Example: I am picking one transaction value as an example. In trans table whose ID=1 then in the same table I need to look for PARENT_ID=1.
when I look for parent_ID=1 then it's ID value will be different.
This is not ID=Parent_ID.
once I look for parent_ID=1 then print its corresponding reference value as name.
I tried above sql statement in oracle, but it didn't work. Could you please let me know, how to write this statement in case statement.

Instead of a subquery, why not try a self-join?
SELECT CASE
WHEN nvl(t1.id,-1) = nvl(t2.reference, -1) THEN t2.reference
ELSE 1
END AS number_col
FROM trans t LEFT JOIN trans t2 ON (t.id = t2.parent_id);
You can also try it as a subquery without a case statement
SELECT t.id,
NVL ((SELECT t2.reference
FROM trans t2
WHERE t.id = t2.parent_id AND t.id = t2.reference AND ROWNUM = 1),
1) AS number_val
FROM trans t

Related

Updating the SQL query from Count(*) to EXISTS in SQL Server

I have the following SQL query,
CASE
WHEN (SELECT COUNT(*)
FROM MyTable AS Parameter
INNER JOIN Table ON Parameter.Attribute1 = Table.Attribute2
WHERE FD.DefID= Parameter.DefID AND Parameter.VTypeID = 1) = 0
THEN (
SELECT * from Table2)
ELSE
NULL
END AS Items
Basically, I would like to ensure that conditional execution is only if the query result count is 0.
How should I modify it to use EXISTS/NOT EXISTS keyword?
You can use exists to do that like so:
SELECT * from Table2
WHERE NOT EXISTS(
SELECT 1
FROM MyTable AS Parameter
INNER JOIN Table ON Parameter.Attribute1 = Table.Attribute2
WHERE FD.DefID= Parameter.DefID AND Parameter.VTypeID = 1)

Oracle Updates with left outer join when we have case statement in select

I wanted to know how to write this query in Oracle SQL:
UPDATE address
SET phone1 = sp.phone,
is_avlbl = ( CASE
WHEN sp.name IS NULL THEN 1
ELSE 0
END )
FROM address ad
LEFT JOIN speaker sp
ON sp.addressid = ad.id
The above query format is from MS SQL Server but I want to achieve similar functionality with Oracle.
Already seen Update and left outer join statements, which is for T-SQL.
EDIT
I have tried the following solution:
update
table1 t1
set
(
t1.column1,
t1.column2,
t1.column3
) = (
select
t2.column1,
t2.column2,
( CASE
WHEN t2.column2 IS NULL THEN 1
ELSE 0
END )
from
table2 t2
where
t2.column1 = t1.column1
);
But the problem is that When there is no record in t2 corresponding to t1, then the above sql inserts null values into t1 where as i need some other value inserted into it when there is no such record. I apologize if this part of the requirement was not clear earlier.
Something like this maybe:
merge into address
using
(
SELECT ad.id,
sp.phone,
sp.name
FROM address ad
LEFT JOIN speaker sp ON sp.addressid = ad.id
) t on (address.id = t.id)
when matched then update
set phone1 = t.phone,
is_avlbl = case
when t.name is null then 1
else 0
end;
(This assumes that address.id is the primary key)
Not tested though, there might be typos that cause syntax errors.

SQL - using a value in a nested select

Hope the title makes some kind of sense - I'd basically like to do a nested select, based on a value in the original select, like so:
SELECT MAX(iteration) AS maxiteration,
(SELECT column
FROM table
WHERE id = 223652
AND iteration = maxiteration)
FROM table
WHERE id = 223652;
I get an ORA-00904 invalid identifier error.
Would really appreciate any advice on how to return this value, thanks!
It looks like this should be rewritten with a where clause:
select iteration,
col
from tbl
where id = 223652
and iteration = (select max(iteration) from tbl where id = 223652);
You can circumvent the problem alltogether by placing the subselect in an INNER JOIN of its own.
SELECT t.iteration
, t.column
FROM table t
INNER JOIN (
SELECT id, MAX(iteration) AS iteration
FROM table
WHERE id = 223652
) tm ON tm.id = t.id AND tm.iteration = t.iteration
Since you're using Oracle, I'd suggest using analytic functions for this:
SELECT * FROM (
SELECT col,
iteration,
row_number() over (partition by id order by iteration desc) rn
FROM tab
WHERE id = 223652
) WHERE rn = 1
do it like this:
with maxiteration as
(
SELECT MAX(iteration) AS maxiteration
FROM table
WHERE id = 223652
)
select
column,
iteration
from
table
where
id = 223652
AND iteration = maxiteration
;
Not 100% sure on Oracle syntax, but isn't it something like:
select iteration, column from table where id = 223652 order by iteration desc limit 1
I would approach this problem in a slightly different way. You're basically looking for the row that has no other iterations greater than it. There are at least 3 ways I can think of to do this:
SELECT
T1.iteration AS maxiteration,
T1.column
FROM
Table T1
WHERE
T1.id = 223652 AND
NOT EXISTS
(
SELECT *
FROM Table T2
WHERE
T2.id = 223652 AND
T2.iteration > T1.iteration
)
Or...
SELECT
T1.iteration AS maxiteration,
T1.column
FROM
Table T1
LEFT OUTER JOIN Table T2 ON
T2.id = T1.id AND
T2.iteration > T1.iteration
WHERE
T1.id = 223652 AND
T2.id IS NULL
Or...
SELECT
T1.iteration AS maxiteration,
T1.column
FROM
Table T1
INNER JOIN (SELECT id, MAX(iteration) AS maxiteration FROM Table T2 GROUP BY id) SQ ON
SQ.id = T1.id AND
SQ.maxiteration = T1.iteration
WHERE
T1.id = 223652
EDIT: I didn't see the ORA error the first time reading the question and it wasn't tagged as Oracle specific. I think that there may be some differences in the syntax and use of aliases in Oracle, so you may need to tweak some of the above queries.
The Oracle error is telling you that it doesn't know what maxiteration is, because the column alias isn't available yet inside the subquery. You need to refer to it by the table alias and column name instead of the column alias I believe.
You do something like
select maxiteration,column from table a join (select max(iteration) as maxiteration from table where id=1) b using (id) where b.maxiteration=a.iteration;
This could of course return multiple rows for one maxiteration unless your table has a constraint against it.

JOIN without double values

I've got query
SELECT frst.Date,t1.Value
from
[ArchiveAnalog] frst
LEFT JOIN
(SELECT Date,Value FROM
[ArchiveAnalog] scnd
WHERE scnd.ID = 1) t1
ON t1.Date = frst.Date
order by frst.Date
but Join inserts values second time (not group with a main node)
if I do GROUP BY frst.Date , I've got error that I can't use t1 Value with it.
How can I make this JOIN without adding additional rows ?
Martin I want to show full Date and Value only if ID = 1 , also then I want to add t2 value column etc like here :
SELECT frst.Date,t1.Value,t2.Value
from
[ArchiveAnalog] frst
LEFT JOIN
(SELECT Date,Value FROM
[ArchiveAnalog] scnd
WHERE scnd.ID = 1) t1
ON t1.Date = frst.Date
LEFT JOIN
(SELECT Date,Value FROM
[ArchiveAnalog] scnd
WHERE scnd.ID = 2) t2
ON frst.Date = t2.Date
here I have x2 additional rows :S with doubling values, so I need to group them all with a some way.
You either need DISTINCT in the main query (if there's up to one row in t1 that you need), or GROUP BY. If you use GROUP BY, you need to aggregate t1.Value (e.g. sum, concatenate etc.).
Does this do what you need?
SELECT Date ,
MAX(CASE WHEN ID=1 THEN Value END) AS val1,
MAX(CASE WHEN ID=2 THEN Value END) AS val2
FROM [ArchiveAnalog]
WHERE ID IN (1,2) /*<-- Not sure if you need this line without seeing your data*/
GROUP BY Date

How do I compare 2 rows from the same table (SQL Server)?

I need to create a background job that processes a table looking for rows matching on a particular id with different statuses. It will store the row data in a string to compare the data against a row with a matching id.
I know the syntax to get the row data, but I have never tried comparing 2 rows from the same table before. How is it done? Would I need to use variables to store the data from each? Or some other way?
(Using SQL Server 2008)
You can join a table to itself as many times as you require, it is called a self join.
An alias is assigned to each instance of the table (as in the example below) to differentiate one from another.
SELECT a.SelfJoinTableID
FROM dbo.SelfJoinTable a
INNER JOIN dbo.SelfJoinTable b
ON a.SelfJoinTableID = b.SelfJoinTableID
INNER JOIN dbo.SelfJoinTable c
ON a.SelfJoinTableID = c.SelfJoinTableID
WHERE a.Status = 'Status to filter a'
AND b.Status = 'Status to filter b'
AND c.Status = 'Status to filter c'
OK, after 2 years it's finally time to correct the syntax:
SELECT t1.value, t2.value
FROM MyTable t1
JOIN MyTable t2
ON t1.id = t2.id
WHERE t1.id = #id
AND t1.status = #status1
AND t2.status = #status2
Some people find the following alternative syntax easier to see what is going on:
select t1.value,t2.value
from MyTable t1
inner join MyTable t2 on
t1.id = t2.id
where t1.id = #id
SELECT COUNT(*) FROM (SELECT * FROM tbl WHERE id=1 UNION SELECT * FROM tbl WHERE id=2) a
If you got two rows, they different, if one - the same.
SELECT * FROM A AS b INNER JOIN A AS c ON b.a = c.a
WHERE b.a = 'some column value'
I had a situation where I needed to compare each row of a table with the next row to it, (next here is relative to my problem specification) in the example next row is specified using the order by clause inside the row_number() function.
so I wrote this:
DECLARE #T TABLE (col1 nvarchar(50));
insert into #T VALUES ('A'),('B'),('C'),('D'),('E')
select I1.col1 Instance_One_Col, I2.col1 Instance_Two_Col from (
select col1,row_number() over (order by col1) as row_num
FROM #T
) AS I1
left join (
select col1,row_number() over (order by col1) as row_num
FROM #T
) AS I2 on I1.row_num = I2.row_num - 1
after that I can compare each row to the next one as I need