SQL find nearest number - sql

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)
;

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

THREE ROWS LEAD FUNCTION IN ORACLE PL 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';

How to select a number of rows according to a column

So I have got two columns in an Oracle database:
Name / count
I would like to print the name x times, x being the count.
E.g. Paul / 5 would mean Paul being printed 5 times.
Sam / 6 would mean Sam being printed 6 times
Tried row_number over but not sure how it works?
You can use connect by as following:
SQL> WITH YOUR_TABLE AS
2 (SELECT 'paul' as NAME, 5 AS COUNT FROM DUAL UNION ALL
3 SELECT 'sam' as NAME, 6 AS COUNT FROM DUAL
4 ) -- YOUR ACTUAL QUERY STARTS FROM LINE#5
5 Select t.name, m.lvl
6 from your_table t
7 join
8 (Select level as lvl
9 from
10 (Select max(count) as maxcount
11 from your_table)
12 Connect by level <= maxcount) m
13 On (t.count >= m.lvl)
14 ORDER BY 1,2;
NAME LVL
---- ----------
paul 1
paul 2
paul 3
paul 4
paul 5
sam 1
sam 2
sam 3
sam 4
sam 5
sam 6
11 rows selected.
SQL>
Cheers!!
you need recursive query to achieve this.
with cte(nam, ctr) as (
select 'Paul' as nam, 5 as ctr from dual
union all
select 'Sam', 6 as ctr from dual
),
cte2(nam, ct, ctr) as (
select nam, 1 as ct, ctr from cte
union all
select nam, ct + 1, ctr from cte2
where ct<ctr
)select nam, ct from cte2
order by nam asc
output:
See sqlfiddle
this will work:
select name
from Table1,
(select level lvl
from dual
connect by level <= (select max(cnt) from Table1 )
)
where lvl <= cnt
order by name;
check fiddle:http://sqlfiddle.com/#!4/14a67/1
Thanks!!!
Yet another option (your query starts at line #4):
SQL> with your_table as
2 (select 'paul' as name, 5 as count from dual union all
3 select 'sam' as name, 6 as count from dual)
4 select name
5 from your_table cross join table (cast (multiset (select level from dual
6 connect by level <= count
7 ) as sys.odcinumberlist));
NAME
----
paul
paul
paul
paul
paul
sam
sam
sam
sam
sam
sam
11 rows selected.
SQL>
You can use a connect by level <= some_number logic containing cross join to link with your table tab :
with tab(Name,"count") as
( select 'Paul', 5 from dual union all select 'Sam', 6 from dual )
select name, level as seq
from dual d
cross join tab t
connect by level <= t."count"
and prior name = name
and prior sys_guid() is not null;
Demo

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

How to do select count(*) group by and select * at same time?

For example, I have table:
ID | Value
1 hi
1 yo
2 foo
2 bar
2 hehe
3 ha
6 gaga
I want my query to get ID, Value; meanwhile the returned set should be in the order of frequency count of each ID.
I tried the query below but don't know how to get the ID and Value column at the same time:
SELECT COUNT(*) FROM TABLE group by ID order by COUNT(*) desc;
The count number doesn't matter to me, I just need the data to be in such order.
Desire Result:
ID | Value
2 foo
2 bar
2 hehe
1 hi
1 yo
3 ha
6 gaga
As you can see because ID:2 appears most times(3 times), it's first on the list,
then ID:1(2 times) etc.
you can try this -
select id, value, count(*) over (partition by id) freq_count
from
(
select 2 as ID, 'foo' as value
from dual
union all
select 2, 'bar'
from dual
union all
select 2, 'hehe'
from dual
union all
select 1 , 'hi'
from dual
union all
select 1 , 'yo'
from dual
union all
select 3 , 'ha'
from dual
union all
select 6 , 'gaga'
from dual
)
order by 3 desc;
select t.id, t.value
from TABLE t
inner join
(
SELECT id, count(*) as cnt
FROM TABLE
group by ID
)
x on x.id = t.id
order by x.cnt desc
How about something like
SELECT t.ID,
t.Value,
c.Cnt
FROM TABLE t INNER JOIN
(
SELECT ID,
COUNT(*) Cnt
FROM TABLE
GROUP BY ID
) c ON t.ID = c.ID
ORDER BY c.Cnt DESC
SQL Fiddle DEMO
I see the question is already answered, but since the most obvious and most simple solution is missing, I'm posting it anyway. It doesn't use self joins nor subqueries:
SQL> create table t (id,value)
2 as
3 select 1, 'hi' from dual union all
4 select 1, 'yo' from dual union all
5 select 2, 'foo' from dual union all
6 select 2, 'bar' from dual union all
7 select 2, 'hehe' from dual union all
8 select 3, 'ha' from dual union all
9 select 6, 'gaga' from dual
10 /
Table created.
SQL> select id
2 , value
3 from t
4 order by count(*) over (partition by id) desc
5 /
ID VALU
---------- ----
2 bar
2 hehe
2 foo
1 yo
1 hi
6 gaga
3 ha
7 rows selected.