Home » SQL & PL/SQL » SQL & PL/SQL » query audit table (merged 4)
query audit table (merged 4) [message #425167] Wed, 07 October 2009 22:08 Go to next message
jxh461
Messages: 185
Registered: March 2005
Senior Member
I am trying to query an audit table Table_name:

Column | Type
--------- -----
id number
changetime date
statusOld char
statusNew char

There are multiple entries for same id.
I am trying to pull the latest entry for each id.
Then I want to check if the statusOld is x.


Note:I don't want the latest entry where statusold is x,
I want latest entry if statusold is x. In other words, I
want the top record per id only if statusOld = x

Basically there will be group by id, order by changetime descending, and rownum = 1

This query is not doing what I want. I am not sure how to restrict the top most record while verifying if statusOld = x
for that record.

Select id from Table_name
group by id
having rownum = 1
order by changetime desc
query audit table (merged 3) [message #425169 is a reply to message #425167] Wed, 07 October 2009 22:10 Go to previous messageGo to next message
jxh461
Messages: 185
Registered: March 2005
Senior Member
I am trying to query an audit table Table_name:

Column | Type
--------- -----
id number
changetime date
statusOld char
statusNew char

There are multiple entries for same id.
I am trying to pull the latest entry for each id.
Then I want to check if the statusOld is x.


Note:I don't want the latest entry where statusold is x,
I want latest entry if statusold is x. In other words, I
want the top record per id only if statusOld = x

Basically there will be group by id, order by changetime descending, and rownum = 1

This query is not doing what I want. I am not sure how to restrict the top most record while verifying if statusOld = x
for that record.

Select id from Table_name
group by id
having rownum = 1
order by changetime desc
query audit table [message #425170 is a reply to message #425169] Wed, 07 October 2009 22:11 Go to previous messageGo to next message
jxh461
Messages: 185
Registered: March 2005
Senior Member
I am trying to query an audit table Table_name:

Column | Type
--------- | -----
id number
changetime date
statusOld char
statusNew char

There are multiple entries for same id.
I am trying to pull the latest entry for each id.
Then I want to check if the statusOld is x.


Note:I don't want the latest entry where statusold is x,
I want latest entry if statusold is x. In other words, I
want the top record per id only if statusOld = x

Basically there will be group by id, order by changetime descending, and rownum = 1

This query is not doing what I want. I am not sure how to restrict the top most record while verifying if statusOld = x
for that record.

Select id from Table_name
group by id
having rownum = 1
order by changetime desc
query audit table [message #425171 is a reply to message #425169] Wed, 07 October 2009 22:12 Go to previous messageGo to next message
jxh461
Messages: 185
Registered: March 2005
Senior Member
I am trying to query an audit table Table_name:

Column | Type
--------- | -----
id number
changetime date
statusOld char
statusNew char

There are multiple entries for same id.
I am trying to pull the latest entry for each id.
Then I want to check if the statusOld is x.


Note:I don't want the latest entry where statusold is x,
I want latest entry if statusold is x. In other words, I
want the top record per id only if statusOld = x

Basically there will be group by id, order by changetime descending, and rownum = 1

This query is not doing what I want. I am not sure how to restrict the top most record while verifying if statusOld = x
for that record.

Select id from Table_name
group by id
having rownum = 1
order by changetime desc
Re: query audit table [message #425173 is a reply to message #425167] Wed, 07 October 2009 22:18 Go to previous messageGo to next message
BlackSwan
Messages: 25046
Registered: January 2009
Location: SoCal
Senior Member
It would be helpful if you followed Posting Guidelines - http://www.orafaq.com/forum/t/88153/0/
It would be helpful if you provided DDL for tables involved.
It would be helpful if you provided DML for test data.
It would be helpful if you provided expected/desired results
& a detailed explanation how & why the test data gets transformed or organized.
Re: query audit table [message #425175 is a reply to message #425173] Wed, 07 October 2009 22:34 Go to previous messageGo to next message
jxh461
Messages: 185
Registered: March 2005
Senior Member
desired results
id
---
001 /* latest date if statusOld = x */
002 /* latest date if statusOld = x */
003 /* latest date if statusOld = x */


undesired results

id
---
004 /* latest date but statusOld != x */
005 /* latest date but statusOld != x */
006 /* latest date but statusOld != x */

[Updated on: Wed, 07 October 2009 22:36]

Report message to a moderator

Re: query audit table (merged 4) [message #425182 is a reply to message #425167] Wed, 07 October 2009 23:29 Go to previous messageGo to next message
Michel Cadot
Messages: 64139
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
Instead of spending time repeating the same question, you should take time to read and follow OraFAQ Forum Guide, especially "How to format your post?" section.
Make sure that lines of code do not exceed 80 characters when you format.
Use code tags and align the columns in result.
Use the "Preview Message" button to verify.
Also always post your Oracle version with 4 decimals.

And post a working Test case: create table and insert statements along with the result you want with these data.

And also take time to search before posting, top N queries have been answered so many times that it is not possible to not find a solution.

Regards
Michel

[Updated on: Wed, 07 October 2009 23:31]

Report message to a moderator

Re: query audit table [message #425185 is a reply to message #425175] Wed, 07 October 2009 23:32 Go to previous messageGo to next message
BlackSwan
Messages: 25046
Registered: January 2009
Location: SoCal
Senior Member
What should be results when ID=008?
Re: query audit table [message #425381 is a reply to message #425185] Thu, 08 October 2009 20:34 Go to previous message
jxh461
Messages: 185
Registered: March 2005
Senior Member
I just wanted to share that I was able to do this but I had to create a user table and query further from that. I will be back to give a better account of my scenario.

Meanwhile thanks for all who gave it some thought.

Especially you Black Swain. Wow !!!!!
Previous Topic: Return result of query into a 6 columns table
Next Topic: Order by issue
Goto Forum:
  


Current Time: Thu Dec 08 22:12:04 CST 2016

Total time taken to generate the page: 0.09534 seconds