How to get average of the row with minimun and maximun excluded in Oracle? - oracle

There are five colums.
How to get average of the row (not column) and
the average should be made with minimum and maximum excluded.
If there are duplicate maximum or(and) minimum, how to exlude them all?
The result of my data should be like this.
Average_MIN_MAX_excluded
-------------------------
3.33333333
5.33333333
My data set is as below;
WITH DATAA AS
(SELECT 3 c1,5 c2,4 c3,3 c4 ,1 c5 FROM DUAL
UNION
SELECT 1 c1,3 c2,6 c3,9 c4 ,7 c5 FROM DUAL)
SELECT c1, c2, c3, c4, c5 FROM DATAA;

select ((c1 + c2 + c3 + c4 + c5) -
greatest( c1, c2, c3, c4, c5 ) -
least( c1, c2, c3, c4, c5 ))/ 3
from DATAA
would be one way. Here's a liveSQL link

It is a good place to use LATERAL JOIN aka CROSS APPLY:
SELECT *
FROM t
CROSS APPLY (
SELECT AVG(c) AS Average_MIN_MAX_excluded
FROM (
SELECT c, ROW_NUMBER() OVER(ORDER BY c) rn
FROM (
SELECT c1 c FROM dual UNION ALL
SELECT c2 FROM dual UNION ALL
SELECT c3 FROM dual UNION ALL
SELECT c4 FROM dual UNION ALL
SELECT c5 FROM dual)
)
WHERE rn NOT IN (1,5)
) s;
This method allows to easily exclude 1,2,3 highest/lowest values if necessary.
db<>fiddle demo

I think you can use unpivot and analytical function with group by and average aggregate function as following:
WITH DATAA AS
(
SELECT 3 c1,5 c2,4 c3,3 c4 ,1 c5 FROM DUAL
UNION
-- case with same value at min for two columns
SELECT 1 c1,5 c2,4 c3,3 c4 ,1 c5 FROM DUAL
UNION
SELECT 1 c1,3 c2,6 c3,9 c4 ,7 c5 FROM DUAL)
-- your query starts from here
select rn, avg(val) from
(select rn, val,
max(val) over (partition by rn) maxval,
min(val) over (partition by rn) minval
from
(SELECT rownum rn, c1, c2, c3, c4, c5
FROM DATAA)
unpivot
(val for vals in (c1,c2,c3,c4,c5)))
where val not in (maxval, minval)
group by rn
See db<>fiddle demo.
Cheers!!

Related

oracle ROW_NUMBER()

I would like to write a query to display this kind of results
column0
column1
column2
column3
a
x
1
1
a
y
1
2
a
z
1
3
b
x
2
1
b
y
2
2
c
x
3
1
c
y
3
2
c
z
3
3
column2 -> row number of column0
column3 -> row number of partition column0 & column1
I have tried this but not working
SELECT ROW_NUMBER() OVER (PARTITION BY column0 ORDER BY column0 ) column2,
ROW_NUMBER() OVER (PARTITION BY column0 , column1 ORDER BY column0 , column1) column3
FROM DUAL
have you got any ideas ?
Use DENSE_RANK for column2 and ROW_NUMBER for column3:
SELECT
column0,
column1,
DENSE_RANK() OVER (ORDER BY column0) AS column3,
ROW_NUMBER() OVER (PARTITION BY column0 ORDER BY column1) AS column4
FROM yourTable;
With row_number:
SQL> with test (col0, col1) as
2 (select 'a', 'x' from dual union all
3 select 'a', 'y' from dual union all
4 select 'a', 'z' from dual union all
5 select 'b', 'x' from dual union all
6 select 'b', 'y' from dual
7 )
8 select col0, col1,
9 row_number() over (partition by col1 order by col0) col2,
10 row_number() over (partition by col0 order by col1) col3
11 from test
12 order by col0, col1
13 /
COL0 COL1 COL2 COL3
----- ------ ---------- ----------
a x 1 1
a y 1 2
a z 1 3
b x 2 1
b y 2 2
SQL>

Is there any to add one loop row in connect by oracle with nocycle?

Just like Oracle continues to follow a path beyond a cyclical loop when the cycle occurs at the top node (root node connected right back to root node), is there any way to do the same with in between cycle.
Like if i have some data like below
create table t1 ( c1 varchar2(2), c2 varchar2(2));
insert into t1 values ('A', 'B');
insert into t1 values ('B', 'C');
insert into t1 values ('C', '**A**');
and execute below query:
select * from (
select distinct
connect_by_root c1 as c3,
c1,
c2
from t1
connect by nocycle c1 = prior c2
) where c3='A';
It will give me this results
c3 c1 c2
A A B
A B C
**A** **C** **A**
It gives me the root looped valued. But if i have data like below.
create table t2 ( c1 varchar2(2), c2 varchar2(2));
insert into t2 values ('A', 'B');
insert into t2 values ('B', 'C');
insert into t2 values ('C', '**B**');
select * from (
select distinct
connect_by_root c1 as c3,
c1,
c2
from t2
connect by nocycle c1 = prior c2
) where c3='A';
this gives me
c3 c1 c2
A A B
A B C
But i need third row also that is A C B.
So wondering if this could be done?
You can use a recursive sub-query factoring clause:
WITH rsqfc ( c3, c1, c2 ) AS (
SELECT c1, c1, c2
FROM t2
WHERE c1 = 'A'
UNION ALL
SELECT r.c3, t.c1, t.c2
FROM t2 t
INNER JOIN rsqfc r
ON ( t.c1 = r.c2 )
)
CYCLE c1, c2 SET is_cycle TO 1 DEFAULT 0
SELECT c3, c1, c2
FROM rsqfc
WHERE is_cycle = 0;
Which, for your sample data:
create table t2 ( c1, c2 ) AS
SELECT 'A', 'B' FROM DUAL UNION ALL
SELECT 'B', 'C' FROM DUAL UNION ALL
SELECT 'C', 'B' FROM DUAL;
Outputs:
C3 | C1 | C2
:- | :- | :-
A | A | B
A | B | C
A | C | B
db<>fiddle here

How can I query to get the rows according to the certain column's value in oracle? [duplicate]

This question already has an answer here:
how to duplicate my sql results? [duplicate]
(1 answer)
Closed 2 years ago.
Table A is:
--------------
C1 C2
--------------
A 3
B 2
--------------
select * from
(
select 'A' as C1, 3 as C2 from dual
union all
select 'B' as C1, 2 as C2 from dual
)
I want to get the following result view with one query statement:
--------------
C1 N1
--------------
A 1
A 2
A 3
B 1
B 2
--------------
I need to generate rows as many as C2 value
Is this possible?
Thank you.
We can handle this via the use of a calendar/sequence table. Consider:
WITH nums AS (
SELECT 1 AS val FROM dual UNION ALL
SELECT 2 FROM dual UNION ALL
SELECT 3 FROM dual
)
SELECT
a.C1,
n.val AS N1
FROM TableA a
INNER JOIN nums n
ON n.val <= a.C2
ORDER BY
a.C1,
n.val;
Demo
Note that in practice, you might use a dedicated table containing a sequence of numbers to cover all possible values in your table. Or, you might use an Oracle sequence.
Alternatively:
SQL> with test as
2 (select 'A' as C1, 3 as C2 from dual
3 union all
4 select 'B' as C1, 2 as C2 from dual
5 )
6 select c1, column_value n1
7 from test cross join table(cast(multiset(select level from dual
8 connect by level <= c2
9 ) as sys.odcinumberlist))
10 order by c1, column_value;
C N1
- ----------
A 1
A 2
A 3
B 1
B 2
SQL>

Hive - Select unique rows based on some columns

I am trying to group rows that have the save value across two columns and have the result ranked/sorted based on a third column.
The result should contain all the other columns.
For the table:
with sample as (
select 'A' as c1, 'B' as c2, '22:00' as c3, 'Da' as c4
union all
select 'A' as c1, 'B' as c2, '23:00' as c3, 'Db' as c4
union all
select 'A' as c1, 'B' as c2, '09:00' as c3, 'Dc' as c4
union all
select 'A' as c1, 'C' as c2, '22:00' as c3, 'Dd' as c4
union all
select 'B' as c1, 'C' as c2, '09:00' as c3, 'De' as c4
)
Grouping or filtering by column c1 and c2 ranked by time on c3, the output would be:
row_number() over (partition by c1, c2 order by c3) as rnk
| c1, c2, c3, c4, rnk|
-----------------------
| A | B |09:00| Dc| 1 |
| A | B |22:00| Da| 2 |
| A | B |23:00| Db| 3 |
| A | C |22:00| Dd| 1 |
| B | C |09:00| De| 1 |
All the other columns like c4, c5.. should be kept but don't have any effect on the group criteria or rank.
A believe a window function with partition on c1 and c2 and order by c3 could work, but not sure if it's the best way in case of very large tables and the need to group by more columns.
The final output would be a UNIQUE row where rank is 1 (top). The columns should be exactly the same as the sample table (no rank).
Select * from tableX where rnk = 1 would do the work but keep colum 'rnk'.
I want to avoid writing all the columns in the select, excluding the rnk.
| c1, c2, c3, c4 |
-------------------
| A | B |09:00| Dc|
| A | C |22:00| Dd|
| B | C |09:00| De|
*Edited, add final table
select inline(array(rec))
from (select struct(*) as rec
,row_number() over
(
partition by c1,c2
order by c3
) as rn
from sample t
) t
where rn = 1
;
+------+------+-------+------+
| col1 | col2 | col3 | col4 |
+------+------+-------+------+
| A | B | 09:00 | Dc |
| A | C | 22:00 | Dd |
| B | C | 09:00 | De |
+------+------+-------+------+
P.s.
Please note that the columns names were aliased, due to the use of struct
I think you just want row_number():
select t.*,
row_number() over (partition by c1, c2 order by c3) as rnk
from sample t;
The question seems to have changed since I answered it -- a rather rude thing to happen. If you want the top ranked column, then use a subquery:
select t.*
from (select t.*,
row_number() over (partition by c1, c2 order by c3) as rnk
from sample t
) t
where rnk = 1;
This returns one row for each combination of c1/c2 in the data. If you want all rows in the event of ties, then use rank() instead of row_number().

sum of column based on distinct value of other column in Oracle

Table A
A1 A2
1 7
2 8
1 9
Table B
A1 B2
1 2
2 3
i want something like this
select A.A1,sum(case when distinct A.A1 then B2),sum(A.A2) from
A,B
where A.A1=B.A1(+)
group by A.A1
After joining my table will be
A1 A2 B2
1 7 2
2 8 3
1 9 2
Resulting Table
A1 A2 B2
1 7+9 2(only once)
2 8 3
how to get sum of B2 when distinct A1 after joining the tables as stated above.
Thanks in advance
Use JOIN and GROUP BY.
Query
SELECT t1.A1, SUM(t1.A2) AS A1, SUM(t2.B2) AS B2
FROM TableA t1
JOIN TableB t2
ON t1.A1 = t2.A1
GROUP BY t1.A1;
Since table_b.a1 is unique, the best way to do this would be to work out the sum of table_a.a2 first to reduce the number of rows you're joining against, and then join to table_b. Then you don't need to worry about summing the distinct table_b.b2 values, which you would otherwise have to do.
WITH table_a AS (SELECT 1 a1, 7 a2 FROM dual UNION ALL
SELECT 2 a1, 8 a2 FROM dual UNION ALL
SELECT 1 a1, 9 a2 FROM dual),
table_b AS (SELECT 1 a1, 2 b2 FROM dual UNION ALL
SELECT 2 a1, 3 b2 FROM dual)
-- end of mimicking your two tables with sample_data in them;
-- see the sql below:
SELECT ta.a1,
ta.a2,
tb.b2
FROM (SELECT a1, SUM(a2) a2
FROM table_a
GROUP BY a1) ta
INNER JOIN table_b tb ON ta.a1 = tb.a1;
A1 A2 B2
---------- ---------- ----------
1 16 2
2 8 3
If you absolutely must join the two tables first (I don't recommend; this is making more work for the database to do), then you could do something like:
WITH table_a AS (SELECT 1 a1, 7 a2 FROM dual UNION ALL
SELECT 2 a1, 8 a2 FROM dual UNION ALL
SELECT 1 a1, 9 a2 FROM dual),
table_b AS (SELECT 1 a1, 2 b2 FROM dual UNION ALL
SELECT 2 a1, 3 b2 FROM dual)
SELECT ta.a1,
SUM(ta.a2) a2,
MAX(tb.b2) b2
FROM table_a ta
INNER JOIN table_b tb ON ta.a1 = tb.a1
GROUP BY ta.a1;
A1 A2 B2
---------- ---------- ----------
1 16 2
2 8 3
Since there can only be one distinct value for table_b.b2 per table_a.a1, we can just pick one of the values to use via MAX (we could have used MIN or SUM(distinct tb.b2) instead, fyi).

Resources