ROW_NUMBER()
Hi
I have one report in 10g then migrated 11g.In 10g dont have row_number while checking report DB query.But 11g it write below logic while executing DB query
ROW_NUMBER() OVER (PARTITION BY D1.c2, D1.c3, D1.c4, D1.c5 ORDER BY D1.c2 ASC, D1.c3 ASC, D1.c4 ASC, D1.c5 ASC) as c7
How to remove this logic in 11g.
b'cos in 10g values return 2 duplicated values but 11g it resticted duplicate value b'cos if more than one value return it restrict row_number.
Try updating the database funcion in repository.
Go to Repository->Physical Layer->Double click Your Database->Function->Unclick the ROWNUM_SUPPORTED
Not sure if this will work.
Similar Messages
-
Not right data when row_number used in inner-view sql query...
Hi ,
I use the below sql statement which displays the right data
select CODE_FARMAKOU,EMP_NAME,PACKTYPE,PACKSIZE , SUM_POSOTITA , row_number() over(partition by code_farmakou order by sum_posotita desc) from
(SELECT CODE_FARMAKOU,EMP_NAME,PACKTYPE,PACKSIZE, TO_DATE(DATES_EKTELESIS,'DD/MM/RRRR') DATES ,SUM(POSOTITA) SUM_POSOTITA
FROM EKTELESI_AT_SINT_CLINIC A, MITROO_FARMAKOU B
WHERE CODE_FARMAKOU = FARMAK_CODE
GROUP BY CODE_FARMAKOU,EMP_NAME,PACKTYPE,PACKSIZE, DATES_EKTELESIS
UNION ALL
SELECT CODE_FARMAKOU,EMP_NAME,PACKTYPE,PACKSIZE, TO_DATE(DATES_EKTELESIS,'DD/MM/RRRR') DATES,SUM(POSOTITA) SUM_POSOTITA
FROM EKTELESI_AT_SINT_EX_IATR A, MITROO_FARMAKOU B
WHERE CODE_FARMAKOU = FARMAK_CODE
GROUP BY CODE_FARMAKOU,EMP_NAME,PACKTYPE,PACKSIZE, DATES_EKTELESIS
UNION ALL
SELECT CODE_FARMAKOU,EMP_NAME,PACKTYPE,PACKSIZE, TO_DATE(DATES_EKTELESIS,'DD/MM/RRRR') DATES,SUM(POSOTITA) SUM_POSOTITA
FROM EKTELESI_AT_SINT_FOREON_MS A, MITROO_FARMAKOU B
WHERE CODE_FARMAKOU = FARMAK_CODE
GROUP BY CODE_FARMAKOU,EMP_NAME,PACKTYPE,PACKSIZE, DATES_EKTELESIS
UNION ALL
SELECT CODE_FARMAKOU,EMP_NAME,PACKTYPE,PACKSIZE, TO_DATE(DATES_EKTELESIS,'DD/MM/RRRR') DATES,SUM(POSOTITA) SUM_POSOTITA
FROM EKTELESI_GEN_SINT_KLIN A, MITROO_FARMAKOU B
WHERE CODE_FARMAKOU = FARMAK_CODE
GROUP BY CODE_FARMAKOU,EMP_NAME,PACKTYPE,PACKSIZE, DATES_EKTELESIS
UNION ALL
SELECT CODE_FARMAKOU,EMP_NAME,PACKTYPE,PACKSIZE, TO_DATE(DATES_EKTELESIS,'DD/MM/RRRR') DATES,SUM(POSOTITA) SUM_POSOTITA
FROM EKTELESI_GEN_SINT_EX_IATR A, MITROO_FARMAKOU B
WHERE CODE_FARMAKOU = FARMAK_CODE
GROUP BY CODE_FARMAKOU,EMP_NAME,PACKTYPE,PACKSIZE, DATES_EKTELESIS)The data are:
CODE_FARMAKOU EMP_NAME PACKTYPE PACKSIZE SUM_POSOTITA ROW_NUMBER()OVER(PARTITIONBYCO
0000002419 FACTREL INJECTION VIAL 2 ML 5 1
0000014071 DOPAMINE HYDROCHLORIDE VIAL 5 ML X 25 30 1
0000086289 DETUSSIN EXPECTORANT BOT 120 ML 3 1
1000000760 DEPON BT 20(BLIST2X10) 2 1
1000000760 DEPON BT 20(BLIST2X10) 1 2
1000003279 MOXACEF BT 40(BLIST10X4) 45 1
1000003279 MOXACEF BT 40(BLIST10X4) 1 2
1000003279 MOXACEF BT 40(BLIST10X4) 1 3
1000014127 DEPON VIT. C BT 2TUBX10 6 1
1000014127 DEPON VIT. C BT 2TUBX10 2 2
1000016655 KABIVEN BT 50ÖÕÓ.×1,7ML 21 1
1000016655 KABIVEN BT 50ÖÕÓ.×1,7ML 2 2However , when i use the below statement , in order not to display the row_number (so i use row_number function in inner-view) the data are different--in different order... why is that????
select CODE_FARMAKOU,EMP_NAME,PACKTYPE,PACKSIZE , SUM_POSOTITA from
select CODE_FARMAKOU,EMP_NAME,PACKTYPE,PACKSIZE , SUM_POSOTITA , row_number() over(partition by code_farmakou order by sum_posotita desc) from
(SELECT CODE_FARMAKOU,EMP_NAME,PACKTYPE,PACKSIZE, TO_DATE(DATES_EKTELESIS,'DD/MM/RRRR') DATES ,SUM(POSOTITA) SUM_POSOTITA
FROM EKTELESI_AT_SINT_CLINIC A, MITROO_FARMAKOU B
WHERE CODE_FARMAKOU = FARMAK_CODE
GROUP BY CODE_FARMAKOU,EMP_NAME,PACKTYPE,PACKSIZE, DATES_EKTELESIS
UNION ALL
SELECT CODE_FARMAKOU,EMP_NAME,PACKTYPE,PACKSIZE, TO_DATE(DATES_EKTELESIS,'DD/MM/RRRR') DATES,SUM(POSOTITA) SUM_POSOTITA
FROM EKTELESI_AT_SINT_EX_IATR A, MITROO_FARMAKOU B
WHERE CODE_FARMAKOU = FARMAK_CODE
GROUP BY CODE_FARMAKOU,EMP_NAME,PACKTYPE,PACKSIZE, DATES_EKTELESIS
UNION ALL
SELECT CODE_FARMAKOU,EMP_NAME,PACKTYPE,PACKSIZE, TO_DATE(DATES_EKTELESIS,'DD/MM/RRRR') DATES,SUM(POSOTITA) SUM_POSOTITA
FROM EKTELESI_AT_SINT_FOREON_MS A, MITROO_FARMAKOU B
WHERE CODE_FARMAKOU = FARMAK_CODE
GROUP BY CODE_FARMAKOU,EMP_NAME,PACKTYPE,PACKSIZE, DATES_EKTELESIS
UNION ALL
SELECT CODE_FARMAKOU,EMP_NAME,PACKTYPE,PACKSIZE, TO_DATE(DATES_EKTELESIS,'DD/MM/RRRR') DATES,SUM(POSOTITA) SUM_POSOTITA
FROM EKTELESI_GEN_SINT_KLIN A, MITROO_FARMAKOU B
WHERE CODE_FARMAKOU = FARMAK_CODE
GROUP BY CODE_FARMAKOU,EMP_NAME,PACKTYPE,PACKSIZE, DATES_EKTELESIS
UNION ALL
SELECT CODE_FARMAKOU,EMP_NAME,PACKTYPE,PACKSIZE, TO_DATE(DATES_EKTELESIS,'DD/MM/RRRR') DATES,SUM(POSOTITA) SUM_POSOTITA
FROM EKTELESI_GEN_SINT_EX_IATR A, MITROO_FARMAKOU B
WHERE CODE_FARMAKOU = FARMAK_CODE
GROUP BY CODE_FARMAKOU,EMP_NAME,PACKTYPE,PACKSIZE, DATES_EKTELESIS)
)and its data are:
CODE_FARMAKOU EMP_NAME PACKTYPE PACKSIZE SUM_POSOTITA
1000016655 KABIVEN BT 50ΦΥΣ.Χ1,7ML 21
1000016655 KABIVEN BT 50ΦΥΣ.Χ1,7ML 2
1000000760 DEPON BT 20(BLIST2X10) 2
1000014127 DEPON VIT. C BT 2TUBX10 2
1000003279 MOXACEF BT 40(BLIST10X4) 45
0000014071 DOPAMINE HYDROCHLORIDE VIAL 5 ML X 25 30
0000086289 DETUSSIN EXPECTORANT BOT 120 ML 3
1000014127 DEPON VIT. C BT 2TUBX10 6
1000003279 MOXACEF BT 40(BLIST10X4) 1
1000003279 MOXACEF BT 40(BLIST10X4) 1
0000002419 FACTREL INJECTION VIAL 2 ML 5
1000000760 DEPON BT 20(BLIST2X10) 1NOTE :Sorry, I tried to pose some sample data using the "with" statement but i couldn't...the error ORA-32035 : unreferenced query name defined in WITH clause was displayed..
Regards,
SimonThe optimizer has, well, optimized out the row_number because you never refer to its value in the outermost query.
sql>select deptno, cnt, row_number() over (order by cnt desc) rn
2 from (select deptno, count(*) cnt
3 from emp
4 group by deptno);
DEPTNO CNT RN
30 6 1
20 5 2
10 3 3
3 rows selected.
-- here because we include rn in the outermost query,
-- the results are still ordered based on the analytical function result
sql>select deptno, cnt, rn
2 from (select deptno, cnt, row_number() over (order by cnt desc) rn
3 from (select deptno, count(*) cnt
4 from emp
5 group by deptno));
DEPTNO CNT RN
30 6 1
20 5 2
10 3 3
3 rows selected.
-- but if we don't include rn in the outermost query,
-- the optimizer leaves out the window sort and the results are in a different order
sql>select deptno, cnt
2 from (select deptno, cnt, row_number() over (order by cnt desc) rn
3 from (select deptno, count(*) cnt
4 from emp
5 group by deptno));
DEPTNO CNT
10 3
20 5
30 6
3 rows selected.This just reinforces the point that if you want your results in a particular order, you need to provide an ORDER BY clause - don't rely on execution plans to do your sorting for you. -
Is that important column order in a query with row_number function
Hi folks,
I am using Oracle 11g R2 on HP-UX machine.
I have 2 types of query with row_number and I think they are same but output of each of them are different. I changed only column order in query2.
Query 1 :
(SELECT
"LOOKUP_INPUT_SUBQUERY"."CONTRACT_SK" "CONTRACT_SK",
"LOOKUP_INPUT_SUBQUERY"."SIMCARD_SK" "SIMCARD_SK"
FROM (
SELECT row_number ()
OVER (
PARTITION BY "R_CON_SUBS_SIMCARD_LK".
"CONTRACT_SK"
ORDER BY
"R_CON_SUBS_SIMCARD_LK"."START_DATE" DESC,
"R_CON_SUBS_SIMCARD_LK"."SEQ_NUM" DESC NULLS LAST) /* EXPRESSION_3.OUTGRP1.SIRA */
"SIRA",
"R_CON_SUBS_SIMCARD_LK"."CONTRACT_SK" "CONTRACT_SK",
"R_CON_SUBS_SIMCARD_LK"."SIMCARD_SK" "SIMCARD_SK"
FROM "SRC_OZRDS"."R_CON_SUBS_SIMCARD_LK" "R_CON_SUBS_SIMCARD_LK")
"LOOKUP_INPUT_SUBQUERY"
WHERE ("LOOKUP_INPUT_SUBQUERY"."SIRA" = 1))
Output of this like that :
CONTRACT_SK SIMCARD_SK
1 1
1 3
1 4
1 5
1 6
1 11
1 12
1 14
1 15
1 16
Query 2 :
(SELECT
"LOOKUP_INPUT_SUBQUERY"."CONTRACT_SK" "CONTRACT_SK",
"LOOKUP_INPUT_SUBQUERY"."SIMCARD_SK" "SIMCARD_SK"
FROM (
SELECT
"R_CON_SUBS_SIMCARD_LK"."CONTRACT_SK" "CONTRACT_SK",
"R_CON_SUBS_SIMCARD_LK"."SIMCARD_SK" "SIMCARD_SK",
row_number ()
OVER (
PARTITION BY "R_CON_SUBS_SIMCARD_LK".
"CONTRACT_SK"
ORDER BY
"R_CON_SUBS_SIMCARD_LK"."START_DATE" DESC,
"R_CON_SUBS_SIMCARD_LK"."SEQ_NUM" DESC NULLS LAST) /* EXPRESSION_3.OUTGRP1.SIRA */
"SIRA"
FROM "SRC_OZRDS"."R_CON_SUBS_SIMCARD_LK" "R_CON_SUBS_SIMCARD_LK")
"LOOKUP_INPUT_SUBQUERY"
WHERE ("LOOKUP_INPUT_SUBQUERY"."SIRA" = 1))
Output of this like that:
2 874812
7 70097256
8 18734091
9 158024
10 815397739
13 22657919
19 83177779
20 82579529
22 5829949
23 35348926
25 3865978
I expected the second output, because there are lots of contract sk but there is one contract_sk in first query result. i did not get the point. What is the problem ?user8649469 wrote:
I changed only column order in query2.So what else do you expect? If you order, for example, by last name, fist name don't you think rows will be returned in a different order (and therefore same row will have different row number) than ordering by first name, last name?
SY. -
Use of ROW_NUMBER() function in PL/SQL
I have a Table Emp with the following Structure
SQL> desc emp
Name Null? Type
EMPNO NUMBER(2)
ENAME VARCHAR2(50)
HIREDATE DATE
DEPTNO NUMBER(2)
If I write a following query on this table
SQL> SELECT deptno, hiredate, record_id
2 FROM (SELECT deptno, ename, hiredate, ROW_NUMBER()
3 OVER (ORDER BY hiredate) AS record_id
4 FROM emp)
5 WHERE record_id >= 2
6 AND record_id <=5;
The Result I get is
DEPTNO HIREDATE RECORD_ID
10 22-NOV-01 2
10 22-NOV-01 3
10 22-NOV-01 4
10 22-NOV-01 5
But if I put this query in a cursor in a PL/SQL block. The
pl/sql does not compiles and gives me the following address
SQL> DECLARE
2 CURSOR c_my IS
3 SELECT deptno, hiredate, record_id
4 FROM (SELECT deptno, ename, hiredate, ROW_NUMBER()
5 OVER (ORDER BY hiredate) AS record_id
6 FROM emp)
7 WHERE record_id >= 2
8 AND record_id <=5;
9 BEGIN
10 FOR c_rec IN c_my LOOP
11 dbms_output.put_line(c_rec.ename);
12 END LOOP;
13 END;
14 /
OVER (ORDER BY hiredate) AS record_id
ERROR at line 5:
ORA-06550: line 5, column 13:
PLS-00103: Encountered the symbol "(" when expecting one of the
following:
, from
Question: Can you please tell me how I can use the ROW_NUMBER()
function in PL/SQL. I need to use this for selecting the correct
range of records for Pagination on a website.
Thanks in advance
PrashantAs Andrew said, PL/SQL hasn't caught up with the newer bits of
SQL. I have heard that in 9i, they will be the same, but in 8i
there are still things that you can do in SQL that you cannot do
directly in PL/SQL, such as the new functions like ROW_NUMBER.
However, you can use NDS as a work around. The following does
the same as what you posted:
SET SERVEROUTPUT ON
DECLARE
TYPE c_my_type IS REF CURSOR;
c_my c_my_type;
TYPE c_rec_type IS RECORD
(deptno emp.deptno%TYPE,
ename emp.ename%TYPE,
hiredate emp.hiredate%TYPE,
record_id INTEGER);
c_rec c_rec_type;
v_sql VARCHAR2 (4000);
BEGIN
v_sql :=
'SELECT deptno, ename, hiredate, record_id
FROM (SELECT deptno, ename, hiredate,
ROW_NUMBER() OVER
(ORDER BY hiredate)
AS record_id
FROM emp)
WHERE record_id >= 2
AND record_id <= 5';
OPEN c_my FOR v_sql;
LOOP
FETCH c_my INTO c_rec;
EXIT WHEN c_my%NOTFOUND;
DBMS_OUTPUT.PUT_LINE (c_rec.ename);
END LOOP;
END;
However, you stated that you need it for selecting the correct
range of records for pagination on a website. For that, you
will want something more like this:
CREATE OR REPLACE PACKAGE package_name
AS
TYPE c_my_type IS REF cursor;
PROCEDURE procedure_name
(c_my IN OUT c_my_type,
p_record_id1 IN NUMBER DEFAULT 1,
p_record_id2 IN NUMBER DEFAULT 4);
END package_name;
CREATE OR REPLACE PACKAGE BODY package_name
AS
PROCEDURE procedure_name
(c_my IN OUT c_my_type,
p_record_id1 IN NUMBER DEFAULT 1,
p_record_id2 IN NUMBER DEFAULT 4)
IS
v_sql VARCHAR2 (4000);
BEGIN
v_sql :=
'SELECT deptno, ename, hiredate, record_id'
|| ' FROM (SELECT deptno, ename, hiredate,'
|| ' ROW_NUMBER() OVER'
|| ' (ORDER BY hiredate)'
|| ' AS record_id'
|| ' FROM emp)'
|| ' WHERE record_id >= :a'
|| ' AND record_id <= :b';
OPEN c_my FOR v_sql
USING p_record_id1, p_record_id2;
END procedure_name;
END package_name; -
Row_number() -SQL function throws error - SAP B1 2005 B - PL 36
hi experts,
when i try 2 run "SELECT * FROM (SELECT ROW_NUMBER() OVER (ORDER BY CardName) AS RowNo,CardCode,CardName FROM OCRD Where CARDTYPE='C') AS OCRD"
query in SAP B1 2005 B, PL-36 it shows an error -server throw an exception.
anybody can comment on it
thanks in advance
jithesh nambiarHi,
Problem might be with your query. Server exception error may occur while accessing large number data retrieving from the data base. It is execution type of error. What do you expect/want from your sql query?
try this query
SELECT ROW_NUMBER() OVER (ORDER BY CardName) AS RowNo,CardCode,CardName FROM OCRD Where CARDTYPE='C'
Senthil Maruthappan. -
How to update data according to row_number in sql
hi,
after deleting an item I need to update data according to row_number.
in oracle database I have a code like this... working perfectly..
update regitem set line_sn= LPAD(ROWNUM,2,'0') where doc_no='" & mydocno & "'
how it will work in sql server...
thanks..........There is an example at the below link which uses the Transact SQL ROW_NUMBER():
http://stackoverflow.com/questions/6729616/sql-server-how-to-update-a-column-for-a-desired-row-number
Paul ~~~~ Microsoft MVP (Visual Basic) -
In my project,i am allowing user to select database of his choice from given options & i need to write queries considering
all these optional databases. So as per my one of requirement,is there any way to achieve generic functionality similar to ROW_NUMBER() in MSSQL ?First run this and check the db version
SELECT @@VERSION
EXEC sp_dbcmptlevel 'db name'
If version is above 2012 and compatibility level is above 80 ROW_NUMBER will work.
Otherwise you need to use this
http://sqlserverplanet.com/sql-2000/simulate-row_number-in-sql-2000
Please Mark This As Answer if it solved your issue
Please Vote This As Helpful if it helps to solve your issue
Visakh
My Wiki User Page
My MSDN Page
My Personal Blog
My Facebook Page -
Update column with ROW_NUMBER() value
I have a column that I would like to update with a ROW_NUMBER() value partitioned by a specific column.
CREATE TABLE ##tmp (id INT NULL,
value1 VARCHAR(10),
value2 VARCHAR(10))
INSERT INTO ##tmp (value1,
value2)
VALUES ('A', 'asdfasdf'),
('A', 'asdf'),
('A', 'VC'),
('B', 'aasdf'),
('C', 'sdfgs'),
('C', 'xdfbhsdty'),
('C', '23sdgfg'),
('C', '234')
-- update the ID column with the values below
SELECT ROW_NUMBER() OVER (PARTITION BY Value1 ORDER BY Value2),
Value1,
Value2
FROM ##tmp
I have 14 million records. Can someone explain the best way to do this? Does the ORDER BY destroy performance for this many records?
Thanks!CREATE TABLE ##tmp (id INT NULL,
value1 VARCHAR(10),
value2 VARCHAR(10))
INSERT INTO ##tmp (value1,
value2)
VALUES ('A', 'asdfasdf'),
('A', 'asdf'),
('A', 'VC'),
('B', 'aasdf'),
('C', 'sdfgs'),
('C', 'xdfbhsdty'),
('C', '23sdgfg'),
('C', '234')
-- update the ID column with the values below
; with cte as (
SELECT ROW_NUMBER() OVER (PARTITION BY Value1 ORDER BY Value2) as RowNumber,
Value1,
Value2
FROM ##tmp
update cte
set Id = RowNumber;
Russel Loski, MCT, MCSE Data Platform/Business Intelligence. Twitter: @sqlmovers; blog: www.sqlmovers.com -
Use ROW_NUMBER as a join to in a query.
Hello
I have created the following code from assistance on a previous post which works great
;WITH C AS
SELECT
ROW_NUMBER() OVER(PARTITION BY Introducer
ORDER BY Introducer) AS rownum, Introducer, BDM, TargetDate
FROM Reporting.dbo.IntroducerDailyReferrals
WHERE TargetDate = '2014-09-01'
SELECT rownum, Introducer, BDM, Targetdate
FROM C
How do I Insert the above query as a part of a join instead of a table link?
ThanksSub query?
SELECT * FROM tb1 JOIN (
SELECT
ROW_NUMBER() OVER(PARTITION BY Introducer
ORDER BY Introducer) AS rownum, Introducer, BDM, TargetDate
FROM Reporting.dbo.IntroducerDailyReferrals
WHERE TargetDate = '2014-09-01'
) AS DER ON tbl.PK=DER.something
Best Regards,Uri Dimant SQL Server MVP,
http://sqlblog.com/blogs/uri_dimant/
MS SQL optimization: MS SQL Development and Optimization
MS SQL Consulting:
Large scale of database and data cleansing
Remote DBA Services:
Improves MS SQL Database Performance
SQL Server Integration Services:
Business Intelligence -
Row_number function not starting from 1
I have two row_number functions in my select statement both has same columns and condition for partition and order by,
except one column in order by which is being ordered by DESC.below is function :
row_number() over (partition by event_cd order by status ASC ,event_date DESC) row1
row_number() over (partition by event_cd order by status DESC ,event_date DESC) row2
Status field has value 0 for incompleted and 1 for completed,if its 0 there will be no date.
The original set of data is :
event_cd status event_date
tsk 0
tsk 0
tsk 1 25-aug-2006
tsk 1 28-aug-2006
tsk 1 31-aug-2006
tsk 1 01-sep-2006
Result I am getting for row1 is
status row1
0 1
0 2
1 3
1 4
1 5
1 6
Result for row2 is
status row1
1 5
1 6
1 1
1 2
0 3
0 4
My confusion is why row2 has not its row number starting from 1,it looks like its
depending on row1 results?If yes,then why?Looks fine to me:
SQL> create table mytable
2 as
3 select 'tsk' event_cd, 0 status, null event_date from dual union all
4 select 'tsk', 0, null from dual union all
5 select 'tsk', 1, date '2006-08-25' from dual union all
6 select 'tsk', 1, date '2006-08-28' from dual union all
7 select 'tsk', 1, date '2006-08-31' from dual union all
8 select 'tsk', 1, date '2006-09-01' from dual
9 /
Tabel is aangemaakt.
SQL> select t.*
2 , row_number() over (partition by event_cd order by status ASC ,event_date DESC) row1
3 , row_number() over (partition by event_cd order by status DESC ,event_date DESC) row2
4 from mytable t
5 /
EVE STATUS EVENT_DATE ROW1 ROW2
tsk 0 1 5
tsk 0 2 6
tsk 1 01-09-2006 00:00:00 3 1
tsk 1 31-08-2006 00:00:00 4 2
tsk 1 28-08-2006 00:00:00 5 3
tsk 1 25-08-2006 00:00:00 6 4
6 rijen zijn geselecteerd.Regards,
Rob. -
Using with and row_number in a query
I'm trying to do a sub-select on a top N type query, for multiple slices. It works for 1 or 2 slices at a time, but then returns nulls.
with subselect as
select issue, count(*) as cnt, row_number() over (order by count(*) desc) as rn
from TBL
group by issue
select * from
select avg(cnt) as avg1_10 from SUBSELECT
where rn between 1 and 10
select avg(cnt) as avg11_20 from SUBSELECT
where rn between 11 and 20
returns
AVG1_10 AVG11_20
140.5 100
This query works fine, and so does this.
with subselect as
select issue, count(*) as cnt, row_number() over (order by count(*) desc) as rn
from TBL
group by issue
select * from
select avg(cnt) from SUBSELECT
where rn between 21 and 30
select avg(cnt) from SUBSELECT
where rn between 31 and 40
AVG21_30 AVG31_40
70.6 59
but this query:
with subselect as
select issue, count(*) as cnt, row_number() over (order by count(*) desc) as rn
from TBL
group by issue
select * from
select avg(cnt) as avg1_10 from SUBSELECT
where rn between 1 and 10
select avg(cnt) as avg11_20 from SUBSELECT
where rn between 11 and 20
select avg(cnt) from SUBSELECT
where rn between 21 and 30
select avg(cnt) from SUBSELECT
where rn between 31 and 40
returns:
AVG1_10 AVG11_20 AVG21-30 AVG31-40
140.5 100
Does anyone have some insights?
Thank-you, CSWThe initial select returns hundreds of rows.
But I'm even more confused now. The following query
with subselect as
select * from (
select issue, count(*) as cnt, row_number() over (order by count(*) desc) as rn
from TBL
group by issue
)where rownum <= 100)
select * from
select avg(cnt) as avg1_10 from SUBSELECT
where rn between 1 and 10
select avg(cnt) as avg11_20 from SUBSELECT
where rn between 11 and 20
select avg(cnt) from SUBSELECT
where rn between 21 and 30
select avg(cnt) from SUBSELECT
where rn between 31 and 40
select avg(cnt) from SUBSELECT
where rn between 41 and 50
returns:
AVG1_10 AVG11_20 AVG21_30 AVG31_40 AVG41_50
140.5 100 70.6 59 50
Why would limiting the returning rows from the subselect affect the results?
Thanks. -
ROW_NUMBER function is nondeterministic
Can we categorize ROW_NUMBER function as nondeterministic. I think, unless we provide column names in the ORDER BY clause to uniquely identify a raw.
" windowed functions are non-deterministic either way you look at them ;)"
This is not true. Some Window Functions are deterministic others are not. The OP wanted to know if ROW_NUMBER was was non-deterministic; and yes, ROW_NUMBER is non-deterministic.
There are four functions in T-SQL that are referred to as "ranking functions": ROW_NUMBER, NTILE, RANK and DENSE_RANK. RANK and DENSE_RANK are deterministic, ROW_NUMBER and NTILE are not. To better understand this please note the queries below
(you can copy/paste into ssms):
DECLARE @names TABLE (name_txt varchar(100) not null, phone_nbr varchar(20) not null);
WITH rand_names AS
SELECT TOP 10 *
FROM (VALUES
('bob', '555-1212'),('bob', '555-1245'),('bob', '555-7896'),
('al', '555-8888'),('zach', '555-9999')
) t(n,p)
ORDER BY newid()
INSERT @names
SELECT * FROM rand_names;
-- query1
SELECT [ROW_NUMBER()] = ROW_NUMBER() over (order by name_txt),
[NTILE(2)] = NTILE(2) over (order by name_txt),
[RANK()] = RANK() over (order by name_txt),
[DENSE_RANK()] = DENSE_RANK() over (order by name_txt), *
FROM @names;
-- query2
SELECT [ROW_NUMBER()] = ROW_NUMBER() over (order by (select null)),
[NTILE(2)] = NTILE(2) over (order by (select null)),
[RANK()] = RANK() over (order by (select null)),
[DENSE_RANK()] = DENSE_RANK() over (order by (select null)), *
FROM @names; -
Problems with Row_Number function
I am having problems with the Row_Number function. I am using it to assign row numbers to records where a student has a grade of pass on a module and excluding failed modules (I want it to show a 0 as row number for failed modules). The problem is that when I try to use a condition, the report still assigns a row number to a failed module though it doesnt display it (it displays a 0 which I wanted it to show). The results appear as follows:
Row number
Module
Grade
1
ModuleA
Pass
2
ModuleB
Pass
0
ModuleC
Fail
4 (instead of 3)
ModuleD
Pass
How can I make it skip assigning a row number to all failed modules? Please assist.
Thanks.It is tricky to use rownum. Because if we generate sequence no by select max(column)+1 from table name ..we can match the where condition and assign the incremented value to the PASS condition.
On the other hand rownum generates a sequence while it is fetching the records one by one so obviously you get the above result only So i will give you an idea to fetch the records of 'PASS' condition first.
Take a look at the below query and alter it and use it on your report.
select a,b,c from (
select 0 a,module b, grade c from table where grade = 'FAIL'
union all
select rownum a,module b, grade c from table where grade = 'PASS' order by b)
order by a
So the above query generates row number only for the PASS condition.
Hope this helps...
Regards,
Soofi. -
Hello every one, can I rewrite the following query without using the 'ROW_NUMBER() OVER ' part.
The query is supposed to pull out the records whose CODE is not NULL and has most
recent date for UPDATE_DATE . The reason I wanted to do this is, When I embed this query
in between many other queries along with JOINs, My oracle server is unable to execute. So, I thought
its better to supplant 'ROW_NUMBER() OVER ' logic with something else and try it. .
SELECT a.* FROM
(SELECT b.*, ROW_NUMBER() OVER (PARTITION BY b.PIDM
ORDER BY b.UPDATE_DATE DESC) AS Rno
FROM
SELECT *
FROM SHYNCRO WHERE CODE IS NOT NULL
)b
)a
WHERE a.Rno = 1Hi,
You didn't write over 150 lines of code and then start testing it, did you?
Don't.
Take baby steps. Write as little as pssiblem test that. Debug and test again until you have something that does exactly what you want it to do.
When you have somehting that works perfectly, take one baby step. Add a tiny amount of code, maybe 1 or 2 lines more, and test again.
When you do get an error, or wrong results, you'll have a much better idea of where the problem is. also, you won't be building code on a flimsy foundation.
If you need help, post the last working version and the new version with the error. Explain what you're trying to do in the new version.
The error message indicates line 133. It looks like line 133 of your code is blank. Does your front end allow completely blank lines in the middle of a query? SQL*Plus doesn't by default; you have to say
SET SQLBLANKLINES ONto have a completely blank line in SQL*Plus. (However, lines containing nothing but at commnet are always allowed.)
You may have noticed that this site normally doesn't display multiple spaces in a row.
Whenever you post formatted text (such as indented code) on this site, type these 6 characters:
\(small letters only, inside curly brackets) before and after each section of formatted text, to preserve spacing.
The 4 people who posted small code fragments for you to read all did this. It would be so much easier for people to read your humongeous query if it were formatted. -
Dear All,
I want to create serial no at class level please check my query , i have already try , row_number , DENSE_RANK() , rank(), it shows 1
Please guie .
Regards
SELECT
distinct
cla.R_Description,
cla.N_Description,
period.period_name,
cla.BRANCH,
cla.CLASS_NAME,
cla.CLASS_NAME CLASS_NAME_SEQ,
DENSE_RANK() OVER (partition by cla.R_Description order by cla.CLASS_NAME) CLASS_NAME_desk,
RANK() OVER (partition by cla.CLASS_NAME order by cla.CLASS_NAME) CLASS_NAME_rank,
st.ACCOUNT_NUMBER,
st.PARTY_NAME,
decode(ASCII(substr(site.attribute13,length(site.attribute13),length(site.attribute13)-1)),13,
substr(site.attribute13,1,length(site.attribute13)-1),site.attribute13) FATHER,
pay.amount_due_original,
trx.trx_number challan_no,
ST.SECTION,
pay.amount_due_remaining,
TO_NUMBER(trx.attribute3) Concession,
ARREAR.DUE_AMOUNT
FROM
RA_CUSTOMER_TRX_ALL TRX,
student_city_school_all st,
class_v_table cla,
bill_period period,
ar_payment_schedules_all pay,
HZ_CUST_SITE_USES_ALL SITE_USE,
HZ_CUST_ACCT_SITES_ALL SITE,
--branch_network_region_link br,
( SELECT BILL_TO_CUSTOMER_ID,SUM(DUE_AMOUNT) DUE_AMOUNT FROM ARREARS_STUDENT WHERE PERIOD_START_DATE < nvl(:P_START_DATE,PERIOD_START_DATE)
--AND
-- ORG_ID=NVL( :P_ORG_ID , ORG_ID )
GROUP BY BILL_TO_CUSTOMER_ID
) ARREAR
WHERE TRX.ATTRIBUTE_CATEGORY='Periodic Billing'
AND trx.attribute2=cla.Fee_Structure_Id
AND trx.bill_to_customer_id=st.CUST_ACCOUNT_ID
AND TRX.BILL_TO_SITE_USE_ID=SITE_USE.SITE_USE_ID
AND SITE_USE.CUST_ACCT_SITE_ID=SITE.CUST_ACCT_SITE_ID
AND TRX.BILL_TO_CUSTOMER_ID=ARREAR.BILL_TO_CUSTOMER_ID(+)
AND trx.org_id=nvl(:p_org_id,trx.org_id)
AND trx.attribute1=period.period_id
-- AND period.period_id=:P_PERIOD_ID
AND trx.customer_trx_id=pay.customer_trx_id
-- AND cla.branch=nvl(:P_BRANCH,cla.branch)
-- AND CLA.CLASS_NAME=NVL(:P_CLASS,CLA.CLASS_NAME)
-- AND cla.N_Description LIKE '%KAPCO%'
-- and (cla.CLASS_NAME like '%kapco%' or cla.CLASS_NAME like '%kapco%' )
--&P_WHERE
ORDER BY CLA.CLASS_NAME, ST.SECTION,
ST.ACCOUNT_NUMBERPlease have the decency to format your code and put it in between delimiters.
Also provide create table statements and some test data along with some output that your expecting to see.
Finally you have had 18 posts and 15 are unresolved. Could you please take the time to mark those as answered
or we will assume that it is a waste of time helping you as our answers aren't helpful to you.
All the above is explained clearly in the FAQ
SQL and PL/SQL FAQ
Edited by: Paul Horth on 05-Mar-2012 23:55 -
Does row_number() returns different result set every time?
Hi,
I have a query somewhat like the example:
select /*+ parallel (ftstm,4) */
ftstm. term,
sum(ftstm.revenue) revenue,
sum(ftstm.searches) searches,
row_number() over (order by sum(ftstm.revenue)desc) as depth_rank
from
fact_terms
group by
ftstm.term
ORDER BY
revenue DESC
where
depth_rank <= 100000
I have run this query 2-3 times and surprisingly- when I do MINUS on the result sets, I got few records.
Is that possible or am I doing some mistake with this query? Please help...Nandini wrote:
Hi,
I have a query somewhat like the example:
select /*+ parallel (ftstm,4) */
ftstm. term,
sum(ftstm.revenue) revenue,
sum(ftstm.searches) searches,
row_number() over (order by sum(ftstm.revenue)desc) as depth_rank
from
fact_terms
group by
ftstm.term
ORDER BY
revenue DESC
where
depth_rank <= 100000
I have run this query 2-3 times and surprisingly- when I do MINUS on the result sets, I got few records.well what are you MINUSing from it?
You need to provide us with your database version, example data and expected output (or show us what you are getting)...
{message:id=9360002}
Maybe you are looking for
-
Sending variable value from php to flash to load an xml file
I would like to load an XML file from the location locally or on the server being unaware of the name of the file. I am using PHP for sending the filename to Flash. The below is the PHP code: <?php filesInDir('C:\Documents and Settings\457305\My Docu
-
If someone could help me out with what I'm missing I would be most grateful! Thanks!
-
JCo IDoc: SAP Business Connector required?
On my searching for the error IDOC_ERROR_METADATA_UNAVAILABLE i have found this thread: BC 4.7 -IDOC_ERROR_METADATA_UNAVAILABLE "As of SAP BC 4.7 the SAP Adapter (the piece dealing with the SAP system(s)) uses the new SAP IDoc Java API which uses oth
-
itunes can not read the contents of the iphones "iphone 5s" go to summary tab in iphone preferences... . "what is this means?" "what i do????"
-
Embedded vs. External video file?
Started playing around with a Flash tutorial on video and saw two options to use a video clip with an SWF file. I wanted some clarification: Embedding the file creates a huge SWF, while having the file external allows for a smaller SWF, right? But in