Last n query
Do anyone know how to write a sql query to get the last 5 record.
e.g. there are 10 records and record number are 11, 3, 6, 32, 55, 7, 21, 90
i want to get the record record number in
90, 55, 32, 21, 11
But i dont want to write 5 nested.....
Can anyone help me?
The solutions that Todd and David provided should work and I am surprised that they don't. If you are going to use Todd's solution, it does require that you use the order by clause in the sub-query, as David explained. Usually, it is only Oracle versions prior to 8i that do not allow the order by clause in the sub-query. However, as APC said, sometimes what Forms allows lags a bit behind what Oracle allows. So, if you can't use an order by clause in a sub-query or the ROW_NUMBER() function, then here is one of those slow, old, poorly performing queries that Todd mentioned. Try it and see it solves your problem:
DECLARE
CURSOR c1
IS
SELECT sal
FROM emp outer
WHERE 5 >=
(SELECT COUNT (sal) + 1
FROM emp inner
WHERE inner.sal > outer.sal)
ORDER BY sal DESC;
BEGIN
NULL;
END;
For a detailed explanation of the use of ROWNUM in sub-queries for top-n analysis and alternatives for older versions, including examples and references, click on the link below:
http://technet.oracle.com:89/ubb/Forum88/HTML/002476.html
null
Similar Messages
-
How to find the last execute query
Hi, Is there a way to find the last executed query of report?
Thanks
Aali
Edited by: aali on 22-Feb-2010 06:39Hi,
if you hold the necessary privileges, you might find the SQL in the shared pool:
select last_active_time, parse_calls, disk_reads, buffer_gets, cpu_time, end_of_fetch_count, executions, plsql_exec_time, sql_text
from V$SQLSTATS
where sql_text like '% some distinctive string %'
order by last_active_time desc;if you insert some meaningful string in form of comment inside the query itself, say /* report #1 on page 3 */
you may be able to track it down more easily.
select last_active_time, parse_calls, disk_reads, buffer_gets, cpu_time, end_of_fetch_count, executions, plsql_exec_time, sql_text
from V$SQLSTATS
where sql_text like '% report #1 on page 3 %'
order by last_active_time desc;Bye
Flavio
http://oraclequirks.blogspot.com -
Last ran query in the database
Hi ,
I need to know the last executed sql query in the data base.
I tried desc v_$sql , etc.. but it says 'table or view doesnot exist'..
So could you please let me know the way to find the last executed query .. :)Not sure if you can retrieve the 'last ran sql' through querying v$SQL, but it looks you'll need some grants/privileges in order to be able to query the DataDictionary views. SELECT_CATALOG_ROLE, for example...
SQL> select * from v_$sql where rownum = 1;
select * from v_$sql where rownum = 1
ERROR at line 1:
ORA-00942: table or view does not exist
SQL> select * from v$sql where rownum = 1;
SQL_TEXT
SQL_FULLTEXT
SERVICE
.....top secret code removed to prevent WW3...
1 row selected. -
How to find last executed query
Hello,
I want to find the last executed query in the Oracle database.
Can anybody help me, how can I find last fired query.
I am using following query for it, but it gives me the same query as in the result.
SELECT SQL_TEXT FROM V$SQL
WHERE ADDRESS = (SELECT SQL_ADDRESS
FROM V$SESSION
WHERE AUDSID = USERENV('SESSIONID'))
Please help me.Thanks.Hi,
select sa.sql_text,ss.username
from v$session ss, v$sqlarea sa
where sa.hash_value = ss.prev_hash_value
SQL> /
SQL_TEXT
USERNAME
DELETE FROM T
HR
GRANT SELECT ON T TO
HR
SELECT COUNT(*) FROM HR.T
SCOTT
SQL_TEXT
USERNAME
select sa.sql_text,ss.username from v$session ss, v$sqlarea sa where sa.hash_val
ue = ss.prev_hash_value
SYSregards
Taj
Message was edited by:
Mohammad Taj -
How to find last executed query on database and or schema.
hi,
how to find last executed query on database and or schema.
yours sincerelySessionwise if you wanna see means use v$session.. Because it will show execution time also so you can analyze it easily and get sql based on SID and serial number ..
Try :
select * from v$session;
Regards.. -
How to get text of Last SQL Query
Sorry if this has been covered, but the search page keeps erring on me.
I am trying to figure out how to get the text of the last SQL statement executed (succesfully or unsuccesfully) by my current login.
What it is, is i want in the exception handler of my procedure to be able to dump the text of the Query which was last run into the log file.
Any help with this will be greatly appreciated!
Thanks in advance,
Aaron.
[email protected]Well, ive been trying V$SQLTEXT in conjunction with V$SESSION and USERENV('SESSIONID'). This gets me my SQL statements, but the SQL_HASH_VALUE and PREV_HASH_VALUE, which are supposed to be the current and previous statements respectively always are the same and always point to the current statement..... is there some sort of flag you have to set to get PREV_HASH_VALUE (and PREV_SQL_ADDR) to actually point to the **PREVIOUS** statement??
-
Last execution query and last user
Hello,
I'd like to know last execution time of a query and last user - table RSZCOMPDIR provides the "Owner" and "Last changed by" only.
I don't want to install the technical content.
Have you any idea, which table contains this information (BW 3.1 used)?
Besides, Can I have same information about a query executed by WebTemplate (therefor via WEB)?
Regards,
ClaudiaHi,
You can find this information in RSDDSTAT_OLAP table.
Enter the query's technical name in OBJNAME field and a date in CALDAY one. It will be returned a list of all users (UNAME field) that executed the query in that day (sort the list by UTIME field to get to know who was the last user).
The logs in this table are generated regardless the way through which the queries are executed - WAD, Analyser etc.
Regards,
Tiago
Edited by: Tiago Santos on Feb 8, 2010 2:00 PM -
Copy the Last Record queried to the new record in a Multi - Record Block
Hi Team
When i enter a new record in a Multi - Record Block the last record should get copied to the new record.
Please help me.
ThanksHi
pls use...
DUPLICATE_RECORD;Amatu Allah -
In a situation I need the Update statement that form builder has generated on the ON-UPDATE trigger.
is there any system veriable like :system.last_query or get_block_property('blockname',last_query); or some any procedure that will return the update statement generated by Form builder
As I am using Forms [32 Bit] Version 9.0.2.9.0Hi,
you would have to try this out.
There is no direct way to achieve this, put you could force a form error (maybe by setting the DML Data Target Name to a non existent table) and examine dbms_error_text. I think that contains the update statement in error -
Hi,is it possible to get the number of records of the last SQL-Query inside a Stored Procedure.
I´m trying to get my Stored Procedure faster and the problem is, that there are many code snippets like the following:
--check if there are any records
Select count(*) INTO v_cnt FROM XXX WHERE XXXX=XXXX;
--if there are records
IF v_cnt > 0 THEN
--get the data of this records
SELECT a,b,c INTO v_a,v_b,v_c FROM XXX WHERE XXXX=XXXX;
END IF;
Now i want to solve this with one SQL-Statement.
BEGIN
SELECT a,b,c INTO v_a,v_b,v_c FROM XXX WHERE XXXX=XXXX;
--Catch the "No data found"-Exception
EXCEPTION WHEN OTHERS THEN
NULL;
END;
IF (??theglobalvariablethatisearch??) > 0 THEN
END IF;
Is there such a "global Variable" in Oracle SQL??
ThanksWhy not eliminate the IF altogether? If NO_DATA_FOUND isn't raised, the number of rows must be > 0
BEGIN
SELECT a,b,c
INTO v_a,v_b,v_c
FROM XXX
WHERE XXXX=XXXX;
<<contents of the THEN clause of your IF statement>>
--Catch the "No data found"-Exception
EXCEPTION
WHEN OTHERS
THEN
NULL;
END;Justin
Distributed Database Consulting, Inc.
http://www.ddbcinc.com/askDDBC -
Replace NULL values from PIVOT query
Hi,
I'm querying a table for sales, for some values, when now columns, the finish with a null value. How can I handle these values as 0 (zeros).
WITH PIVOT_DATA AS (
SELECT S.ZONE_CODE,Z.ZONE_NAME,S.YEAR,S.PERIOD,S.SALES
FROM STAT_TABLE_SALES VTA
JOIN ZONES Z ON S.COMP=Z.COMP AND S.ZONE_CODE=Z.ZONE_CODE
WHERE S.COMP = '001'
AND S.BRAND_CODE = '001'
SELECT *
FROM PIVOT_DATA
PIVOT(
SUM(SALES) FOR (YEAR,PERIOD) IN ((2009,1),
(2009,2),
(2009,3),
(2009,4),
(2009,5),
(2009,6),
(2009,7),
(2009,8),
(2009,9),
(2009,10),
(2009,11),
(2009,12),
(2010,1),
(2010,2),
(2010,3),
(2010,4),
(2010,5),
(2010,6),
(2010,7))
ORDER BY 14 DESC NULLS LAST;
This query returns the following:
COD_ZONA NOM_ZONA 2009_1 2009_2 2009_3
01 YEDUSIJH. 1382367.75 1559943.27 1441279.64
02 DO,ASKAK 711897.82 865854.05 1583232.3
03 ASDFG 130443.03 205409.84 178633.69
04 OSOIDSD 320118.32 439008.83 409251.18
05 ODFSDF 300908.21 276301.59 260188.53
06 CH 242749.65 325196.71 464938.9
07 SOA 610312.31 606312.93 754569.82
08 SAN 89426.8 81360.04 61649.27
09 YP 284487.79 328281.31 267210.85
10 TC 87043.28 158594.43 85195.8
11 BAGNN 76778.78 68180.76 118530.04
12 CRTS 122023.7 143442.21 134744.85
13 OCC 209992.79 196477.03 185222.14
14 IDLB
15 YA 23870.41 4137.33 16660.53
**These are not all the columns and rows, but this is a piece of what it returns.
How can I replace these NULLS with 0(zeros).Still not sure why nvl shouldn't suit your needs:
SQL> select ename, nvl (clerk_20, 0) clerk_20, nvl (sal_30, 0) sal_30
from emp pivot (sum (sal)
for (job, deptno)
in ( ('CLERK', 20) clerk_20, ('SALESMAN', 30) sal_30))
ENAME CLERK_20 SAL_30
WARD 0 1250
JONES 0 0
TURNER 0 1500
ADAMS 1100 0
ALLEN 0 1600
SMITH 800 0
CLARK 0 0
KING 0 0
BLAKE 0 0
JAMES 0 0
FORD 0 0
SCOTT 0 0
MARTIN 0 1250
MILLER 0 0
14 rows selected. -
Sql query parsing (retrieve tables and columns used in query)
Hi,
1. Is there any view in Oracle which can tell me, which tables and columns were used in the last ran query, no matter how complex the query is.
2. Secondly I can get table names, used in stored procedures and views, using USER_DEPENDENCIES VIEW, however this System View does not return column names, used in that procedure / view, any idea please .
Thanks and Regards,
Luqmanluqman123 wrote:
Hi,
1. Is there any view in Oracle which can tell me, which tables and columns were used in the last ran query, no matter how complex the query is.I don't believe so. You can look at V$SQL and retrieve the actual SQL text from there.
2. Secondly I can get table names, used in stored procedures and views, using USER_DEPENDENCIES VIEW, however this System View does not return column names, used in that procedure / view, any idea please .Oracle added fine grained dependency tracking in 11g, but didn't expose any views to query this information. However, it has been reverse engineered a bit: About Oracle: DBA_DEPENDENCY_COLUMNS -
Order of tables in the form clause influences query performance? (ora 9i)
Hi,
I have a SQL select with 5 tables in the 'from' clause. On Oracle 9i (and 8i too) if I user a certain table order (bigger tables first, smaller tables last) the query executes fast. If I change the order, then it executes in slow motion. I use cost based optimizing.
I thought it was just Oracle 8i where I had to take into account such things when writing a query. Does some one know the cause for this? I
Regards,
Tamas SzecsyAnd, even in 10GR2, if the optimizer_mode is changed to CHOOSE (the default is ALL_ROWS), the optimizer defaults to RULE based:
SQL> show parameter optimizer_mode
NAME TYPE VALUE
optimizer_mode string ALL_ROWS
SQL> alter session set optimizer_mode = choose ;
Session altered.
SQL> show parameter optimizer_mode
NAME TYPE VALUE
optimizer_mode string CHOOSE
SQL> analyze table emp delete statistics ;
Table analyzed.
SQL> analyze table dept delete statistics ;
Table analyzed.
SQL> set autotrace traceonly explain
SQL> select e.*, d.* from emp e, dept d where d.deptno = e.deptno (+) ;
Execution Plan
Plan hash value: 748780195
| Id | Operation | Name |
| 0 | SELECT STATEMENT | |
| 1 | NESTED LOOPS OUTER | |
| 2 | TABLE ACCESS FULL | DEPT |
| 3 | TABLE ACCESS BY INDEX ROWID| EMP |
|* 4 | INDEX RANGE SCAN | IDX_EMP_DEPTNO |
Predicate Information (identified by operation id):
4 - access("D"."DEPTNO"="E"."DEPTNO"(+))
Note
- rule based optimizer used (consider using cbo)
SQL> analyze table emp compute statistics for table for all indexes for all indexed columns ;
Table analyzed.
SQL> analyze table dept compute statistics for table for all indexes for all indexed columns ;
Table analyzed.
SQL> select e.*, d.* from emp e, dept d where d.deptno = e.deptno (+) ;
Execution Plan
Plan hash value: 748780195
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
| 0 | SELECT STATEMENT | | 14 | 826 | 4 (0)| 00:00:01 |
| 1 | NESTED LOOPS OUTER | | 14 | 826 | 4 (0)| 00:00:01 |
| 2 | TABLE ACCESS FULL | DEPT | 4 | 76 | 3 (0)| 00:00:01 |
| 3 | TABLE ACCESS BY INDEX ROWID| EMP | 4 | 160 | 1 (0)| 00:00:01 |
|* 4 | INDEX RANGE SCAN | IDX_EMP_DEPTNO | 5 | | 0 (0)| 00:00:01 |
Predicate Information (identified by operation id):
4 - access("D"."DEPTNO"="E"."DEPTNO"(+))
SQL> alter session set optimizer_mode = rule ;
Session altered.
SQL> select e.*, d.* from emp e, dept d where d.deptno = e.deptno (+) ;
Execution Plan
Plan hash value: 748780195
| Id | Operation | Name |
| 0 | SELECT STATEMENT | |
| 1 | NESTED LOOPS OUTER | |
| 2 | TABLE ACCESS FULL | DEPT |
| 3 | TABLE ACCESS BY INDEX ROWID| EMP |
|* 4 | INDEX RANGE SCAN | IDX_EMP_DEPTNO |
Predicate Information (identified by operation id):
4 - access("D"."DEPTNO"="E"."DEPTNO"(+))
Note
- rule based optimizer used (consider using cbo)
SQL> disconnect
Disconnected from Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options
SQL> -
Can anyone give me examples of replacement path with query?
hi all,
Can anyone give me examples of replacment path with query. if u have any docs pls send it across to my email id [email protected]
thanxs
hariHere is an example:
Lets assume you have a characteristic( Say Project) with about 30 attributes. now your requirement is to display all 30 attributes and about 10 keyfigures and another characteristic and it's attributes...
If Project has about 25K projects in a Year,you will find its very difficult to diasply all the inofmation with one Query.(you may get disconnected from server after executing the query)..
So firstly,create a Query1 only with Project and keyfigures.
then create another Query2 only with Project and its attributes...here create a variable on Projeect with replacementpath and select the Query1.So you will have the Projects only from Query1.
Create Query3 with Project and other chars.create one more variable on Project with replacementpath select Query1 result. So this Query also gets Projects from Query1...
Now Query2 and Query3 get Projects from Query1 always...so you will have same Projects in your all 3 Queries..
Now crate a workbook and insert 3 Queries.make sure you insert Query1 lastly.Since last inserted query will be executed first...
You may use VBA or excel functionality if you want to see whole data in one sheet.
Hope this helps. -
Can we use Lookup function twice in one query ??
Cause in the Log file, I only see the last lookup query.
Or Any Suggesion for this case ?
Here's the sample :
*SELECT(%PRMFGList%,"[ID]","PROCESS","SFG_Pos='P'")
*XDIM_MEMBERSET PROCESS = %PRMFGList%
*LOOKUP FINANCE
*DIM SalesQty:OTHERS = PROCESS.SFG
*DIM SalesQty:TPDTLS = TPDTLS.ID
*DIM SalesQty:TPYEARS = TPYEARS.ID
*DIM SalesQty:CATEGORY = CATEGORY.ID
*DIM SalesQty:TIME = TIME.ID
*ENDLOOKUP
*LOOKUP FINANCE
*DIM LotSize:OTHERS = PROCESS.SFG
*DIM LotSize:TPDTLS = TPDTLS.ID
*DIM LotSize:TPYEARS = TPYEARS.ID
*DIM LotSize:CATEGORY = CATEGORY.ID
*DIM LotSize:TIME = TIME.ID
*ENDLOOKUP
*WHEN Account.ID
*IS "Qty"
*REC(FACTOR=-(LOOKUP(SalesQty)/LOOKUP(LotSize)),ACCOUNT=QtyRMUsage)
*ENDWHEN
*COMMITHi Ari,
I've faced the same problem, and never found a solution that allowed for 2 lookups. I believe that only one is supported (although I never saw this confirmed in any documentation). With that limitation in mind, you need to choose your LOOKUP carefully, and then use an expanded source data region to load in whatever else you need in your calc. For whatever you can't LOOKUP, you'll need to GET.
In that case, consider using xdim_noscan to only process the records you want. This helps get around the single LOOKUP limitation.
In your example here, however, it looks like you can get both of these values in a single lookup. It's not clear to me what differentiates the SalesQty and LotSize (they seem to be the same thing), but I'll assume these are two members in your Account dimension.
Also, note that if you're not re-directing a particular dimension to a different member, you can just omit it from the LOOKUP. This would apply to your TPDTLS, TPYEARS, CATEGORY, and TIME, if I read your code correctly.
In that case, you could do this:
*SELECT(%PRMFGList%,"ID","PROCESS","SFG_Pos='P'")
*XDIM_MEMBERSET PROCESS = %PRMFGList%
// this may make the logic run faster
*XDIM_MEMBER Account = Qty
*LOOKUP FINANCE
*DIM SalesQty:Account = SalesQty
*DIM SalesQty:OTHERS = PROCESS.SFG
*DIM LotSize:Account = LotSize
*DIM LotSize:OTHERS = PROCESS.SFG
*ENDLOOKUP
*WHEN Account.ID
*IS "Qty"
// or possibly you can just use *When * *is *
*REC(FACTOR=-(LOOKUP(SalesQty)/LOOKUP(LotSize)),ACCOUNT=QtyRMUsage)
*ENDWHEN
*COMMIT
Maybe you are looking for
-
Is there a way to order multiple different prints in any quantity from Iphoto? it appears currently like I would have to ship each separately, even if only one print... is there a way around this so I can can get several DIFFERENT prints of any quant
-
Hi, I need to re-format my hard drive, as it has a virus and is corrupted- so I will lose all my data including my itunes software. I guess I will need to reinstall itunes again - but if I do this will, I lose all the music that I have bought or impo
-
types: begin of type_s_data, record(65000) type c, end of type_s_data. data: w_xml_out type string. data: t_xml_tab type table of type_s_data with header line, Source code t_code type table of type_s_data with header line, Af
-
XML Insert into database by direct http request from browser
I want to ask if it is possible to pass a xml filename which valuse should be stored in the database directly by a http request of i.e. IE 5.5. I mean perhaps such a construction: http://pcziegl/Test_Insert.xsql?posted-xml=http://pcziegl/Test_Insert.
-
I go to the US App store, click on the upgrade tab for Lion. It then shows me the price in UK Sterling. I complete payment detals, click continue and it tells me my credit card is not valid in the UK. Anyone know why it directs me to the UK site to p