THREE ROWS LEAD FUNCTION IN ORACLE PL SQL - sql

My Table:
ID NAME
1 SIVA
2 RAJA
3 PYTHON
4 SQL
5 ODI
I need to lead by 3 rows.
My SQL Query:
SELECT LEAD(NAME,3) OVER (ORDER by NAME) as NAME FROM TEST_TABLE where NAME='SIVA'
EXPECTED OUTPUT:
--------
| NAME |
--------
| SQL |
--------
Example:
If I pass value as SIVA, then I need to get SQL as output.
Similarly if I pass value as Raja, then I need to get ODI as output
Is there any query to get the expected output?

This might be one option: it uses row_number analytic function which calculates row numbers so that you wouldn't have to rely on ID values. What if they are acquired by a sequence? It is not gapless. Basically - it is used for safety.
SQL> WITH test (id, name)
2 AS (SELECT 1, 'siva' FROM DUAL
3 UNION ALL
4 SELECT 2, 'raja' FROM DUAL
5 UNION ALL
6 SELECT 3, 'python' FROM DUAL
7 UNION ALL
8 SELECT 4, 'sql' FROM DUAL
9 UNION ALL
10 SELECT 5, 'odi' FROM DUAL),
11 temp AS (SELECT id, name, ROW_NUMBER () OVER (ORDER BY id) rn FROM test)
12 SELECT b.name
13 FROM temp a JOIN temp b ON b.rn = a.rn + 3
14 WHERE a.name = '&name';
Enter value for name: siva
NAME
------
sql
SQL> /
Enter value for name: raja
NAME
------
odi
SQL> /
Enter value for name: sql
no rows selected
SQL>

You can simply use sub-query :
SELECT Next_NAME
FROM (SELECT NAME, LEAD(NAME, 3) OVER (ORDER by id) AS Next_NAME
FROM TEST_TABLE
) t
WHERE NAME = 'SIVA';

Related

case statement after where clause to omit the row of data if satisfied

Hi I have a table as below and I'm trying to extract the data from them if and only if the below condition is satisfied.
ID Rank
45689 1
54789 2
98765 1
96541 2
98523 3
92147 4
96741 2
99999 10
If the ID starts with 4 and 9 or 5 and 9 and have same Rank then omit them. If ID starts with 9 and no matching Rank with other ID (starting with 4 or 5) then show them as result.
So My Output should look like
ID Rank
98523 3
92147 4
99999 10
How can I use case statement in where clause to filter the data?
If I understand correctly, you want to select only those ID that begin with a 9, and have a rank that is not also the rank of (another) ID that begins with 4 or 5. Is that correct?
The query below is for the case ID is of string data type (although it will work OK, probably, if ID is numeric data type - through implicit conversion).
select *
from your_table
where id like '9%'
and rank not in (
select rank
from your_table
where substr(id, 1, 1) in ('4', '5')
)
;
One option would be using COUNT() analytic function along with a conditional aggregation such as
WITH t2 AS
(
SELECT SUM(CASE WHEN SUBSTR(id,1,1) IN ('5','9') OR
SUBSTR(id,1,1) IN ('4','9') THEN 1 END ) OVER
(PARTITION BY Rank) AS count, t.*
FROM t -- your original table
)
SELECT id, rank
FROM t2
WHERE count = 1
Demo
You can use an analytic function to only query the table once:
SELECT id,
rank
FROM (
SELECT t.*,
COUNT( CASE WHEN id LIKE '4%' OR id LIKE '5%' THEN 1 END )
OVER ( PARTITION BY Rank )
AS num_match
FROM table_name t
WHERE id LIKE '4%'
OR id LIKE '5%'
OR id LIKE '9%'
)
WHERE id LIKE '9%'
AND num_match = 0;
Which, for the sample data:
CREATE TABLE table_name ( ID, Rank ) AS
SELECT 45689, 1 FROM DUAL UNION ALL
SELECT 54789, 2 FROM DUAL UNION ALL
SELECT 98765, 1 FROM DUAL UNION ALL
SELECT 96541, 2 FROM DUAL UNION ALL
SELECT 98523, 3 FROM DUAL UNION ALL
SELECT 92147, 4 FROM DUAL UNION ALL
SELECT 96741, 2 FROM DUAL UNION ALL
SELECT 99999, 10 FROM DUAL;
Outputs:
ID | RANK
----: | ---:
98523 | 3
92147 | 4
99999 | 10
db<>fiddle here

sql function to add each row values

table a
id
1
2
3
4
i want output to be like this:
id id_new
1 1
2 3
3 6
4 10
basically we are adding row values of a column in each row
i.E 2=1+2 ,3=1+2+3 and so on
you can use any cursor,function or query
You seem to want a cumulative sum. The ANSI standard function is a window function, supported by Oracle:
select id, sum(id) over (order by id) as id_new
from t;
It seems strange to do this with a column called id, though. Usually, cumulative sums are on counts or amounts or something measurable.
If you need to actually update the data, then you can use a correlated subquery:
update t
set id_new = (select sum(id) from t t2 where t2.id <= t.id);
This will work on a small table. For anything larger, use merge.
The comment by Pradeep is probably the way to go on Oracle. But there is an alternative to that, which is using a correlated subquery to compute the rolling sum:
SELECT
id,
(SELECT SUM(t2.id) FROM yourTable t2 WHERE t2.id <= t1.id) id_new
FROM yourTable t1;
This answer may have some merit because not every database supports analytic functions. And even if we are using Oracle in the context of, e.g., a Java application, an ORM interface like JPA or Hibernate may not support analytic functions.
Piece of cake.
SQL> with test (id) as
2 (select 1 from dual union
3 select 2 from dual union
4 select 3 from dual union
5 select 4 from dual
6 )
7 select id, sum(id) over (order by id) id_new
8 from test
9 order by id;
ID ID_NEW
---------- ----------
1 1
2 3
3 6
4 10
SQL>
[EDIT, a generalized option]
SQL> select level id, sum(level) over (order by level) new_Id
2 from dual
3 connect by level <= &n;
Enter value for n: 5
old 3: connect by level <= &n
new 3: connect by level <= 5
ID NEW_ID
---------- ----------
1 1
2 3
3 6
4 10
5 15
SQL> /
Enter value for n: 3
old 3: connect by level <= &n
new 3: connect by level <= 3
ID NEW_ID
---------- ----------
1 1
2 3
3 6
SQL>

Oracle select pivot query to put row adjacent to their counter part using generic column name

I have a table which each entry has a counter pair
Customer
Name Value
Bob 3
Bob 4
Sam 0
Sam 1
Joe 9
I want the following result
Customer
Name Value1 Value2
Bob 3 4
Sam 0 1
Joe 9
I have read this thread, Oracle query to put rows at odd number adjacent to even number, but I want to avoid using the MOD function instead possible using pivot instead.
You can't use the pivot statement here, if you have only two value for each name (it also works with dates, because we can use max and min for dates):
select name, min(value) value1, nullif(max(value), min(value)) value2
from customer_tables
group by name
If Bob, Sam and other have more that two value:
with t (Name, Value) as (
select 'Bob',3 from dual union all
select 'Bob',4 from dual union all
select 'Sam',0 from dual union all
select 'Sam',1 from dual union all
select 'Joe',9 from dual
), t1 (name, value, rn) as (
select name, value, ROW_NUMBER() OVER(partition by name order by value) from t
)
select * from t1
pivot XML (
max(value)
for rn in (ANY)
)
SQL> l
1 with t (Name, Value) as (
2 select 'Bob',3 from dual union all
3 select 'Bob',4 from dual union all
4 select 'Sam',0 from dual union all
5 select 'Sam',1 from dual union all
6 select 'Joe',9 from dual
7 ), t1 (name, value, rn) as (
8 select name, value, ROW_NUMBER() OVER(partition by name order by value) from t
9 )
10 select * from t1
11 pivot XML (
12 max(value)
13 for rn in (ANY)
14* )
SQL> /
NAM RN_XML
--- --------------------------------------------------------------------------------
Bob <PivotSet><item><column name = "RN">1</column><column name = "MAX(VALUE)">3</col
umn></item><item><column name = "RN">2</column><column name = "MAX(VALUE)">4</co
lumn></item></PivotSet>
Joe <PivotSet><item><column name = "RN">1</column><column name = "MAX(VALUE)">9</col
umn></item></PivotSet>
Sam <PivotSet><item><column name = "RN">1</column><column name = "MAX(VALUE)">0</col
umn></item><item><column name = "RN">2</column><column name = "MAX(VALUE)">1</co
lumn></item></PivotSet>
Read more about pivot here

SQL find nearest number

Say I have a table like the following (I'm on Oracle 10g btw)
NAME VALUE
------ ------
BOB 1
BOB 2
BOB 4
SUZY 1
SUZY 2
SUZY 3
How can I select all rows where value is closest to, but not greater than, a given number. For example if I want to find all the rows where value is closest to 3 I would get:
NAME VALUE
------ ------
BOB 2
SUZY 3
This seems like it should be simple... but I'm having no luck.
Thanks!
SELECT name, max(value)
FROM tbl
WHERE value <= 3
GROUP BY name
This works (SQLFiddle demo):
SELECT name, max(value)
FROM mytable
WHERE value <= 3
GROUP BY name
Based on hagensofts answer:
SELECT name, max(value)
FROM tbl
WHERE value <= 3 AND ROWNUM <=2
GROUP BY name
With ROWNUM you can limit the output rows, so if you want 2 row, then you can limit the rownum.
WITH v AS (
SELECT 'BOB' NAME, 1 value FROM dual
UNION ALL
SELECT 'BOB', 2 FROM dual
UNION ALL
SELECT 'BOB', 4 FROM dual
UNION ALL
SELECT 'SUZY', 1 FROM dual
UNION ALL
SELECT 'SUZY', 2 FROM dual
UNION ALL
SELECT 'SUZY', 3 FROM dual
)
SELECT *
FROM v
WHERE (name, value) IN (SELECT name, MAX(value)
FROM v
WHERE value <= :num
GROUP BY name)
;

Get next row value based on returned list of rows

In Oracle, suppose I have a query that returns the following list:
ID Sequence#
12 1
15 3
25 5
All I know in this case is the ID of some row (let's suppose 12), I need to return the ID of a row with the next sequence number which in this case is 3 (id = 15)
How can I do it? I know there's a Oracle function lead, but I wasn't able to successfully impement is.
Yes, you can use lead function to get the next value. Here is an example of how it can be done.
-- sample of data from your question
SQL> with t1(ID, Sequence#) as
2 (
3 select 12, 1 from dual union all
4 select 15, 3 from dual union all
5 select 25, 5 from dual
6 )
7 select *
8 from (select id
9 , sequence#
10 , lead(sequence#) over(order by id) next_sequence#
11 , lead(id) over(order by id) next_id#
12 from t1
13 )
14 where id = 12
15 ;
ID SEQUENCE# NEXT_SEQUENCE# NEXT_ID#
---------- ---------- -------------- ----------
12 1 3 15
SELECT * FROM table1 where ID in (SELECT min(ID) FROM table1 WHERE ID > 12)
Select sequence from my_ table where id=(select min(id) from my_table where sequence> 1)
Replace (1) in the above query with any value that you are searching for its next