I would like to access the value of the "current row" on which I write the analytic expression on. For example, given the following sample data:
DROP TABLE emp PURGE;
CREATE TABLE emp (
empno NUMBER(4) CONSTRAINT pk_emp PRIMARY KEY,
ename VARCHAR2(10),
job VARCHAR2(9),
mgr NUMBER(4),
hiredate DATE,
sal NUMBER(7,2),
comm NUMBER(7,2),
deptno NUMBER(2)
);
INSERT INTO emp VALUES (7369,'SMITH','CLERK',7902,to_date('17-12-1980','dd-mm-yyyy'),800,NULL,20);
INSERT INTO emp VALUES (7499,'ALLEN','SALESMAN',7698,to_date('20-2-1981','dd-mm-yyyy'),1600,300,30);
INSERT INTO emp VALUES (7521,'WARD','SALESMAN',7698,to_date('22-2-1981','dd-mm-yyyy'),1250,500,30);
INSERT INTO emp VALUES (7566,'JONES','MANAGER',7839,to_date('2-4-1981','dd-mm-yyyy'),2975,NULL,20);
INSERT INTO emp VALUES (7654,'MARTIN','SALESMAN',7698,to_date('28-9-1981','dd-mm-yyyy'),1250,1400,30);
INSERT INTO emp VALUES (7698,'BLAKE','MANAGER',7839,to_date('1-5-1981','dd-mm-yyyy'),2850,NULL,30);
INSERT INTO emp VALUES (7782,'CLARK','MANAGER',7839,to_date('9-6-1981','dd-mm-yyyy'),2450,NULL,10);
INSERT INTO emp VALUES (7788,'SCOTT','ANALYST',7566,to_date('13-7-87','dd-mm-rr')-85,3000,NULL,20);
INSERT INTO emp VALUES (7839,'KING','PRESIDENT',NULL,to_date('17-11-1981','dd-mm-yyyy'),5000,NULL,10);
INSERT INTO emp VALUES (7844,'TURNER','SALESMAN',7698,to_date('8-9-1981','dd-mm-yyyy'),1500,0,30);
INSERT INTO emp VALUES (7876,'ADAMS','CLERK',7788,to_date('13-7-87', 'dd-mm-rr')-51,1100,NULL,20);
INSERT INTO emp VALUES (7900,'JAMES','CLERK',7698,to_date('3-12-1981','dd-mm-yyyy'),950,NULL,30);
INSERT INTO emp VALUES (7902,'FORD','ANALYST',7566,to_date('3-12-1981','dd-mm-yyyy'),3000,NULL,20);
INSERT INTO emp VALUES (7934,'MILLER','CLERK',7782,to_date('23-1-1982','dd-mm-yyyy'),1300,NULL,10);
COMMIT;
Let's say I would like to calculate the average (using the deptno for partitioning) only if the salary is smaller than the salary value of the "outer row"
Here is the query that calculates the average for everyone in the specific window, the row that is commented out what I would like to be able to do, "pseudocode".
SELECT t.empno, t.deptno, t.sal
,AVG(t.sal) OVER (PARTITION BY t.deptno) AS avg_dept_sal
--,AVG(CASE WHEN t.sal < OUTER_VALUE(t.sal) THEN t.sal ELSE NULL END) OVER (PARTITION BY t.deptno) AS avg_dept_sal_2
FROM emp t
;
So, while avg_dept_sal returns ~2916 for deptno = 10, for each row, with avg_dept_sal_2 should return:
1300 for empno = 7782
1875 for empno = 7839
NULL for empno = 7934
What would be the best approach to achieve this?
Use a RANGE window in the analytic function:
SELECT empno,
deptno,
sal,
AVG(sal) OVER (PARTITION BY deptno) AS avg_dept_sal,
AVG(sal) OVER (
PARTITION BY deptno
ORDER BY sal
RANGE BETWEEN UNBOUNDED PRECEDING AND 0.01 PRECEDING
) AS avg_dept_sal_2
FROM emp;
Which, for the sample data, outputs:
EMPNO
DEPTNO
SAL
AVG_DEPT_SAL
AVG_DEPT_SAL_2
7934
10
1300
2916.666666666666666666666666666666666667
null
7782
10
2450
2916.666666666666666666666666666666666667
1300
7839
10
5000
2916.666666666666666666666666666666666667
1875
7369
20
800
2175
null
7876
20
1100
2175
800
7566
20
2975
2175
950
7788
20
3000
2175
1625
7902
20
3000
2175
1625
7900
30
950
1566.666666666666666666666666666666666667
null
7654
30
1250
1566.666666666666666666666666666666666667
950
7521
30
1250
1566.666666666666666666666666666666666667
950
7844
30
1500
1566.666666666666666666666666666666666667
1150
7499
30
1600
1566.666666666666666666666666666666666667
1237.5
7698
30
2850
1566.666666666666666666666666666666666667
1310
db<>fiddle here
Im using view on oracle plsql. In the table showing my sales,
I want to have a column showing the sequence number next to the sales of top 50 products.
The best selling products should be listed and followed by the sequence number in the row.
how can i do that?
Thanks.
This is my related query
NVL (
(SELECT ROUND (
SUM (
CASE DOCUMENT_TYPE
WHEN 2
THEN
(CASE TRANSACTION_TYPE
WHEN 0 THEN 0 - AMOUNT
ELSE AMOUNT
END)
ELSE
(CASE TRANSACTION_TYPE
WHEN 1 THEN 0 - AMOUNT
ELSE AMOUNT
END)
END),
8)
FROM TBL_TRANSACTION_LINES
WHERE STORE_NO = tbl_location.locationno
AND (TRANSACTION_TYPE NOT IN (10, 30))
AND TRANSACTION_DATE >
TO_DATE ('2020-09-27 0:0:0',
'yyyy-mm-dd HH24:MI:SS')
AND TRANSACTION_DATE <=
TO_DATE ('2020-10-04 0:0:0',
'yyyy-mm-dd HH24:MI:SS')
AND (URUNID = TBL_URUNLER.URUNID)),
0)
AS RESULTS
I don't have your tables nor data, so - here's an example based on Scott's sample schema; ranking employees by salary within their departments. Apply that to your case.
SQL> with temp as
2 (select deptno, ename, sal,
3 rank() over (partition by deptno order by sal desc) rnk
4 from emp
5 )
6 select deptno, ename, sal, rnk
7 from temp
8 order by deptno, rnk;
DEPTNO ENAME SAL RNK
---------- ---------- ---------- ----------
10 KING 10000 1
10 CLARK 2450 2
10 MILLER 1300 3
20 SCOTT 3000 1
20 FORD 3000 1
20 JONES 2975 3
20 ADAMS 1100 4
20 SMITH 920 5
30 BLAKE 2850 1
30 ALLEN 1600 2
30 TURNER 1500 3
30 MARTIN 1250 4
30 WARD 1250 4
30 JAMES 950 6
14 rows selected.
SQL>
I want to select the top 5 rows and additional a 6th row named Others with rest aggregated.
with
Anzahl as
(SELECT
NVL (parse_listener_log_line (connect_string, 'HOST'), 'n/a') host, COUNT(*) cnt
FROM listener_log
WHERE ID_MANDANT = :P100_MANDANT
AND ID_SERVER = :P100_SERVER
GROUP BY parse_listener_log_line (connect_string, 'HOST')
ORDER BY cnt DESC),
client as
(select
case
when rownum > 4 then 'Others'
else host
end as client, cnt
FROM Anzahl)
SELECT client, cnt
FROM client;
CLIENT CNT
jdbc 118553
server2 106170
server1 101710
server4 13370
Others 8734
Others 1760
Others 1365
Others 1058
A little bit of analytic functions (row_number) along with set operations (union) might do what you're looking for.
Scott's EMP table contains these data:
SQL> select ename, sal from emp order by sal desc;
ENAME SAL
---------- ----------
KING 5000
FORD 3000
SCOTT 3000
JONES 2975
BLAKE 2850
CLARK 2450
ALLEN 1600
TURNER 1500
MILLER 1300
WARD 1250
MARTIN 1250
ADAMS 1100
JAMES 950
SMITH 800
14 rows selected.
Now: find each row's ordinal number (using row_number) and union the first 5 rows (take each of them as is) with the sixth one that contains aggregated salaries:
SQL> with temp as
2 (select ename,
3 sal,
4 row_number() over (order by sal desc) rn
5 from emp
6 )
7 select rn,
8 ename,
9 sal
10 from temp
11 where rn <= 5
12 union all
13 select 6,
14 'Other',
15 sum(sal)
16 from temp
17 where rn > 5
18 order by rn;
RN ENAME SAL
---------- ---------- ----------
1 KING 5000
2 SCOTT 3000
3 FORD 3000
4 JONES 2975
5 BLAKE 2850
6 Other 12200
6 rows selected.
SQL>
Does anyone know why this select statement on this query runs just fine but when i add cte as in front of the same select statement it gives this error:
SQL Error: ORA-00957: duplicate column name)
CREATE TABLE t1 AS
SELECT *
FROM NS_F3
LEFT JOIN NS_FA2
ON NS_F3.PI_CANDIDATE_NUM = NS_FA2.PI_CANDIDATE_NUM
WHERE REGEXP_LIKE(NS_F3.TITLE, 'intern($|ship|[^a-z])', 'i');
It is because tables NS_F3 and NS_FA2 contain columns with the same name - in that case, you should use column alias to avoid duplicate column names.
Here's an example: I'm creating a simple table, as extract of several columns from Scott's EMP table:
SQL> create table t_first as select deptno, empno, ename from emp where rownum < 5;
Table created.
Join it to DEPT table:
SQL> select * from t_first e join dept d on e.deptno = d.deptno;
DEPTNO EMPNO ENAME DEPTNO DNAME LOC
---------- ---------- ---------- ---------- -------------- -------------
20 7369 SMITH 20 RESEARCH DALLAS
20 7566 JONES 20 RESEARCH DALLAS
30 7521 WARD 30 SALES CHICAGO
30 7499 ALLEN 30 SALES CHICAGO
^^^^^^^^^ ^^^^^^^^
this is DEPTNO column ... ... and here's another one
As long as it works in SELECT, it won't work in CREATE TABLE:
SQL> create table test as
2 select * from t_first e join dept d on e.deptno = d.deptno;
select * from t_first e join dept d on e.deptno = d.deptno
*
ERROR at line 2:
ORA-00957: duplicate column name
The solution is to use a column alias, such as:
SQL> create table test as
2 select e.deptno emp_deptno, --> first alias
3 e.empno,
4 e.ename,
5 d.deptno, dept_deptno --> second alias
6 d.dname,
7 d.loc
8 from t_first e join dept d on e.deptno = d.deptno;
Table created.
SQL> select * From test;
EMP_DEPTNO EMPNO ENAME DEPT_DEPTNO DNAME LOC
---------- ---------- ---------- ----------- -------------- -------------
20 7369 SMITH 20 RESEARCH DALLAS
20 7566 JONES 20 RESEARCH DALLAS
30 7521 WARD 30 SALES CHICAGO
30 7499 ALLEN 30 SALES CHICAGO
SQL>
I have table : tb_user.
id|name|value
10|boy|500
20|Ony|200
10|boy|500
When I execute the following query:
Select id,name,sum(value) as grant_total from tb_user group by id,name
the result is:
id|name|grant_total
10|boy |1000
20|Ony |200
I want to add 1 column --> ranking
id|name|grant_total|ranking
10|boy |1000 |1
20|Ony |200 |2
how to make ranking?
Try:
Select id,name,sum(value) as grant_total, rownum as ranking from tb_user group by id,name
You could use ROWNUM and a subquery to first order the rows:
SQL> WITH DATA AS(
2 SELECT 10 ID, 'boy' NAME, 1000 grand_total FROM dual UNION ALL
3 SELECT 20, 'Ony', 200 grand_total from dual
4 )
5 SELECT t.*,
6 rownum ranking
7 FROM
8 ( SELECT * FROM DATA ORDER BY grand_total DESC
9 ) t
10 /
ID NAM GRAND_TOTAL RANKING
---------- --- ----------- ----------
10 boy 1000 1
20 Ony 200 2
SQL>
Or,
You could use the Analytic functions:
For example,
SQL> WITH DATA AS(
2 SELECT 10 ID, 'boy' NAME, 1000 grand_total FROM dual UNION ALL
3 SELECT 20, 'Ony', 200 grand_total from dual
4 )
5 SELECT t.*, row_number() OVER(ORDER BY grand_total DESC) ranking FROM DATA t;
ID NAM GRAND_TOTAL RANKING
---------- --- ----------- ----------
10 boy 1000 1
20 Ony 200 2
SQL>
Depending on the requirement, you need to use:
ROW_NUMBER
RANK
DENSE_RANK
Let's see an example,
SQL> SELECT empno, ename, sal FROM emp;
EMPNO ENAME SAL
---------- ---------- ----------
7369 SMITH 800
7499 ALLEN 1600
7521 WARD 1250
7566 JONES 2975
7654 MARTIN 1250
7698 BLAKE 2850
7782 CLARK 2450
7788 SCOTT 3000
7839 KING 5000
7844 TURNER 1500
7876 ADAMS 1100
7900 JAMES 950
7902 FORD 3000
7934 MILLER 1300
14 rows selected.
SQL> SELECT empno,
2 ename,
3 sal,
4 ROW_NUMBER() OVER(ORDER BY sal) rnum,
5 RANK() OVER(ORDER BY sal DESC) rank,
6 DENSE_RANK() OVER(ORDER BY sal) drank
7 FROM emp
8 ORDER BY empno
9 /
EMPNO ENAME SAL RNUM RANK DRANK
---------- ---------- ---------- ---------- ---------- ----------
7369 SMITH 800 1 14 1
7499 ALLEN 1600 8 7 7
7521 WARD 1250 4 10 4
7566 JONES 2975 11 4 10
7654 MARTIN 1250 5 10 4
7698 BLAKE 2850 10 5 9
7782 CLARK 2450 9 6 8
7788 SCOTT 3000 12 2 11
7839 KING 5000 14 1 12
7844 TURNER 1500 7 8 6
7876 ADAMS 1100 3 12 3
7900 JAMES 950 2 13 2
7902 FORD 3000 13 2 11
7934 MILLER 1300 6 9 5
14 rows selected.
SQL>
The easiest way is to use the ROW_NUMBER() analytic function (or RANK() if you want to rank ties equally):
SELECT id, name, SUM(value) AS grand_total
, ROW_NUMBER() OVER ( ORDER BY SUM(value) DESC ) AS ranking
FROM tb_user
GROUP BY id, name
ORDER BY ranking;