473,386 Members | 1,745 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

query issue

Hi,
I'm in deep struggle with query from a huge table:

what I've got is a lot of records like:

_______________
numb fk1
_______________
1231456 61
1231456 62
1231456 63
1231456 61

from my view I should only get the last record:
__________
123456 61

last has to be intended as inserting order, there's not sorting defined on data
the table has also a progressive id generated by a sequence..

I've tried the the following , but doesn't work:

select distinct numb,fk1
from <table>

can anybody help, please?
best regards
jc
Jul 19 '05 #1
9 4610
"curwen" wrote...
Hi,
I'm in deep struggle with query from a huge table:

what I've got is a lot of records like:

_______________
numb fk1
_______________
1231456 61
1231456 62
1231456 63
1231456 61

from my view I should only get the last record:
__________
123456 61

last has to be intended as inserting order, there's not sorting defined on data the table has also a progressive id generated by a sequence..

I've tried the the following , but doesn't work:

select distinct numb,fk1
from <table>

can anybody help, please?
best regards
jc


Hi jc,

why don't you use the 'progressive id' you mentioned?
Hint for free: MAX().

hth,
Guido
Jul 19 '05 #2
"Guido Konsolke" <GK@oblivion.com> wrote in message
news:10***************@news.thyssen.com...
| "curwen" wrote...
| > Hi,
| > I'm in deep struggle with query from a huge table:
| >
| > what I've got is a lot of records like:
| >
| > _______________
| > numb fk1
| > _______________
| > 1231456 61
| > 1231456 62
| > 1231456 63
| > 1231456 61
| >
| > from my view I should only get the last record:
| > __________
| > 123456 61
| >
| > last has to be intended as inserting order, there's not sorting
| defined on data
| > the table has also a progressive id generated by a sequence..
| >
| > I've tried the the following , but doesn't work:
| >
| > select distinct numb,fk1
| > from <table>
| >
| > can anybody help, please?
| > best regards
| > jc
|
| Hi jc,
|
| why don't you use the 'progressive id' you mentioned?
| Hint for free: MAX().
|
| hth,
| Guido
|
|

additional hint: use the max(id) in a subquery

-- mcs
Jul 19 '05 #3
> Hi jc,

why don't you use the 'progressive id' you mentioned?
Hint for free: MAX().

hth,
Guido


because if I do something like:

select max(n_id),n_number from numbers group by n_number

then I get :
_____________
max(id) numb
12 123456

I need the fk1 field, too
and

select max(n_id),n_number,fk1 from numbers group by n_number

is not going to work..

I accomplished the mission using:

select * from numbers where n_id in
(select max(n_id) from numbers group by n_number);

but i'm afraid it could be a bottleneck
is there any way to avoid nested queries?

PS i'm sorry for xpost :)
Jul 19 '05 #4
"Guido Konsolke" <GK@oblivion.com> wrote in message news:<10***************@news.thyssen.com>...
"curwen" wrote...
Hi,
I'm in deep struggle with query from a huge table:

what I've got is a lot of records like:

_______________
numb fk1
_______________
1231456 61
1231456 62
1231456 63
1231456 61

from my view I should only get the last record:
__________
123456 61

last has to be intended as inserting order, there's not sorting

defined on data
the table has also a progressive id generated by a sequence..

I've tried the the following , but doesn't work:

select distinct numb,fk1
from <table>

can anybody help, please?
best regards
jc


Hi jc,

why don't you use the 'progressive id' you mentioned?
Hint for free: MAX().

hth,
Guido


JC, there is no guarantee that the order that the rows display in is
the same as the insert order since Oracle is going to look for and
insert into blocks with free space in them. Guido, pointed you at the
solution: you have to use the max progressive id for a numb fk1 value
combination to find the last inserted value pair.

HTH -- Mark D Powell --
Jul 19 '05 #5
> JC, there is no guarantee that the order that the rows display in is
the same as the insert order since Oracle is going to look for and
insert into blocks with free space in them. Guido, pointed you at the
solution: you have to use the max progressive id for a numb fk1 value
combination to find the last inserted value pair.

HTH -- Mark D Powell --


ok, do you mean something like this:

select * from numbers where n_id in
(select max(n_id) from numbers group by n_number);
in your opinion is there any way to avoid the nested query?
jc
Jul 19 '05 #6
jo**********@despammed.com (curwen) wrote in message news:<cc**************************@posting.google. com>...
JC, there is no guarantee that the order that the rows display in is
the same as the insert order since Oracle is going to look for and
insert into blocks with free space in them. Guido, pointed you at the
solution: you have to use the max progressive id for a numb fk1 value
combination to find the last inserted value pair.

HTH -- Mark D Powell --


ok, do you mean something like this:

select * from numbers where n_id in
(select max(n_id) from numbers group by n_number);
in your opinion is there any way to avoid the nested query?
jc


Observe:
SQL> select * from marktest2;

TCOL1 TCOL2
---------- ----------
1231456 1
1231456 2
1231456 3
1231456 4
3211456 1
3211456 3
3211456 2 <= notice value 2 stored physically after later
insert

7 rows selected.

1 select * from marktest2 a
2 where tcol2 = ( select max(tcol2) from marktest2 b
3* where b.tcol1 = a.tcol1 )
SQL> /

TCOL1 TCOL2
---------- ----------
1231456 4
3211456 3

The above will work well if you enter the outer query using tcol1, but
if you want a solution without a sub-query then convert the subquery
into a join:

1 select a.*
2 from marktest2 a
3 ,(select b.tcol1, max(b.tcol2) as tcol2
4 from marktest2 b
5 group by b.tcol1
6 ) c
7 where a.tcol1 = c.tcol1
8* and a.tcol2 = c.tcol2
SQL> /

TCOL1 TCOL2
---------- ----------
1231456 4
3211456 3
However, if you want all the max id's for a value then just run the
group by query and no outer query or join is necessary at all.

HTH -- Mark D Powell --
Jul 19 '05 #7
>
select * from numbers where n_id in
(select max(n_id) from numbers group by n_number);
in your opinion is there any way to avoid the nested query?


There's no way, and frankly, no need to avoid the nested query. What
you need to avoid is the double scan of the numbers table (or its
index on N_ID). And I thought you said you need a single record, not
the last record for every n_number?
Assuming that N_ID is unique, and the sequence generates unique
numbers in ascending order (nocycle), this will select the very last
record in the table using a NOSORT index access with a stopkey, and
then a single table access by index ROWID (in other words: very fast):

select * from (
select * from numbers order by n_id desc)
where rownum=1;

Note that 'the very last' only refers to the order in which sequence
numbers are generated, not necessarily the actual insertion order.
There are questions like 'what counts as insertion time -- the time
the insert statement is executed, or the time when the sequence number
is generated?' that need to be answered -- in a legal debate, for
example :-)

For all other intents and purposes, the sequence logic will do nicely.
HTH,
Flado
Jul 19 '05 #8
> select * from numbers where n_id in
(select max(n_id) from numbers group by n_number);
in your opinion is there any way to avoid the nested query?
jc


In Oracle 9 there is a way :
select
n_number,
max(fk1) keep (dense_rank last order by n_id) last_fk1
from numbers
group by n_number;
What this means is, that it will only do a max(fk1) on those records
with the last n_id value within each group.
As n_id is unique, the max(fk1) will only operate on the one record
within each n_number group that has the largest n_id.
In Oracle 8 you won't avoid the nested query, but you can avoid
accessing numbers table twice :
select distinct n_number, last_fk1
from
(
select
n_number,
last_value(fk1) over (partition by n_number order by n_id rows
between unbounded preceding and unbounded following) last_fk1
from numbers
);
The analytic function will give you the fk1 value for the last n_id
within each n_number partition as you go along, but as that inner
select will return a row for each row in numbers, you select distinct
to only get the relevant information.
Hope these may help.
KiBeHa
Jul 19 '05 #9
Hi jc,
>
why don't you use the 'progressive id' you mentioned?
Hint for free: MAX().

hth,
Guido
because if I do something like:

select max(n_id),n_number from numbers group by n_number

then I get :
_____________
max(id) numb
12 123456

I need the fk1 field, too
and

select max(n_id),n_number,fk1 from numbers group by n_number

is not going to work..

I accomplished the mission using:

select * from numbers where n_id in
(select max(n_id) from numbers group by n_number);

but i'm afraid it could be a bottleneck
is there any way to avoid nested queries?

PS i'm sorry for xpost :)
Jun 27 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: DBNovice | last post by:
I have a database that keeps records on the issue and failure of an item. Currently, the database is poorly desisned; therefore I'm performing queries to break the data into normalized tables and...
3
by: Brian Oster | last post by:
After applying security patch MS03-031 (Sql server ver 8.00.818) a query that used to execute in under 2 seconds, now takes over 8 Minutes to complete. Any ideas on what the heck might be going...
3
by: Paul Mateer | last post by:
Hi, I have been running some queries against a table in a my database and have noted an odd (at least it seems odd to me) performance issue. The table has approximately 5 million rows and...
4
by: Starbuck | last post by:
OK, first let me say that I am no DB person. But I have a user here who keeps getting this error whenever she does, whatever it is she does, with databases... A google search takes me to...
3
by: Megan | last post by:
hi everybody- it seems like my update query should work. the sql view seems logical. but that might be up to discussion...lol...i'm a newbie! UPDATE , Issue SET .IssueID = . WHERE ((.=.));
2
by: Brian Tabios | last post by:
Hello Everyone, I have a very complex performance issue with our production database. Here's the scenario. We have a production webserver server and a development web server. Both are running...
5
by: elitecodex | last post by:
Hey everyone. I have this query select * from `TableName` where `SomeIDField` 0 I can open a mysql command prompt and execute this command with no issues. However, Im trying to issue the...
2
by: googlegroups.dsbl | last post by:
I'm really confused here, and am wondering if someone knows what could be the issue with my TableAdapter query. A few months ago, I created a really neat program that has th ability to search by...
1
by: Andy_Khosravi | last post by:
Background: Access 2003 (converted from A97 DB recently), database is split into FE/BE with the FE residing on client machine. I've got a question about how I can possibly optimize a query I've...
18
by: JGrizz | last post by:
Greetings, I first off want to state that I am new to the forum, so if this question ends up in the wrong area, I apologize. This pertains to Access 2003/VBA/SQL issues... I have been doing some...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.