473,399 Members | 3,401 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,399 software developers and data experts.

How to make a "group by ordered" ?

I have a table State_history :
Id State DateModif object_id
1 A 10/10/01 1
2 B 10/11/01 1
3 B 11/11/01 1
4 B 11/12/01 1
5 C 01/01/02 1
6 C 01/02/02 1
7 A 02/03/02 1
The challenge is to select only differents states of an object

I try this :
Select max(DateModif),id,state,object_id
from State_history
group by id,state,object_id;

I get :
Id State DateModif object_id
4 B 11/12/01 1
6 C 01/02/02 1
7 A 02/03/02 1

I wanted :
Id State DateModif object_id
1 A 10/10/01 1
4 B 11/12/01 1
6 C 01/02/02 1
7 A 02/03/02 1

The problem is that it doesnt select the first row, which is a different
state from the line 7, because the object take different state between line
1 and 7

Any idea to make this query ?
Regards,

Lilian.


Jul 20 '05 #1
3 1480
If you want to try :

CREATE TABLE TEST
(
ID NUMBER primary key,
ETAT VARCHAR2(1),
DATE_CREAT DATE
);

INSERT INTO TEST ( ID, ETAT, DATE_CREAT ) VALUES (
1, 'A', TO_Date( '10/10/2001 12:00:00 AM', 'MM/DD/YYYY HH:MI:SS AM'));
INSERT INTO TEST ( ID, ETAT, DATE_CREAT ) VALUES (
2, 'B', TO_Date( '10/11/2001 12:00:00 AM', 'MM/DD/YYYY HH:MI:SS AM'));
INSERT INTO TEST ( ID, ETAT, DATE_CREAT ) VALUES (
3, 'B', TO_Date( '10/12/2001 12:00:00 AM', 'MM/DD/YYYY HH:MI:SS AM'));
INSERT INTO TEST ( ID, ETAT, DATE_CREAT ) VALUES (
4, 'C', TO_Date( '11/12/2001 12:00:00 AM', 'MM/DD/YYYY HH:MI:SS AM'));
INSERT INTO TEST ( ID, ETAT, DATE_CREAT ) VALUES (
5, 'C', TO_Date( '12/10/2001 12:00:00 AM', 'MM/DD/YYYY HH:MI:SS AM'));
INSERT INTO TEST ( ID, ETAT, DATE_CREAT ) VALUES (
6, 'C', TO_Date( '12/13/2001 12:00:00 AM', 'MM/DD/YYYY HH:MI:SS AM'));
INSERT INTO TEST ( ID, ETAT, DATE_CREAT ) VALUES (
7, 'A', TO_Date( '12/14/2001 12:00:00 AM', 'MM/DD/YYYY HH:MI:SS AM'));
commit;
the only query i found :

select
*
from
test etat_i
where
etat_i.etat not in (
select distinct
etat_f.etat
from test etat_f
where
etat_f.id>etat_i.id
and etat_f.id < (
select min(etat_s.ID)
from
test etat_s
where
etat_s.etat != etat_i.etat
and etat_s.ID > etat_i.ID
)
)
;

If gurus got a better query....

Jul 20 '05 #2
If you want to try :

CREATE TABLE TEST
(
ID NUMBER primary key,
ETAT VARCHAR2(1),
DATE_CREAT DATE
);

INSERT INTO TEST ( ID, ETAT, DATE_CREAT ) VALUES (
1, 'A', TO_Date( '10/10/2001 12:00:00 AM', 'MM/DD/YYYY HH:MI:SS AM'));
INSERT INTO TEST ( ID, ETAT, DATE_CREAT ) VALUES (
2, 'B', TO_Date( '10/11/2001 12:00:00 AM', 'MM/DD/YYYY HH:MI:SS AM'));
INSERT INTO TEST ( ID, ETAT, DATE_CREAT ) VALUES (
3, 'B', TO_Date( '10/12/2001 12:00:00 AM', 'MM/DD/YYYY HH:MI:SS AM'));
INSERT INTO TEST ( ID, ETAT, DATE_CREAT ) VALUES (
4, 'C', TO_Date( '11/12/2001 12:00:00 AM', 'MM/DD/YYYY HH:MI:SS AM'));
INSERT INTO TEST ( ID, ETAT, DATE_CREAT ) VALUES (
5, 'C', TO_Date( '12/10/2001 12:00:00 AM', 'MM/DD/YYYY HH:MI:SS AM'));
INSERT INTO TEST ( ID, ETAT, DATE_CREAT ) VALUES (
6, 'C', TO_Date( '12/13/2001 12:00:00 AM', 'MM/DD/YYYY HH:MI:SS AM'));
INSERT INTO TEST ( ID, ETAT, DATE_CREAT ) VALUES (
7, 'A', TO_Date( '12/14/2001 12:00:00 AM', 'MM/DD/YYYY HH:MI:SS AM'));
commit;
the only query i found :

select
*
from
test etat_i
where
etat_i.etat not in (
select distinct
etat_f.etat
from test etat_f
where
etat_f.id>etat_i.id
and etat_f.id < (
select min(etat_s.ID)
from
test etat_s
where
etat_s.etat != etat_i.etat
and etat_s.ID > etat_i.ID
)
)
;

If gurus got a better query....

Jul 20 '05 #3
If you want to try :

CREATE TABLE TEST
(
ID NUMBER primary key,
ETAT VARCHAR2(1),
DATE_CREAT DATE
);

INSERT INTO TEST ( ID, ETAT, DATE_CREAT ) VALUES (
1, 'A', TO_Date( '10/10/2001 12:00:00 AM', 'MM/DD/YYYY HH:MI:SS AM'));
INSERT INTO TEST ( ID, ETAT, DATE_CREAT ) VALUES (
2, 'B', TO_Date( '10/11/2001 12:00:00 AM', 'MM/DD/YYYY HH:MI:SS AM'));
INSERT INTO TEST ( ID, ETAT, DATE_CREAT ) VALUES (
3, 'B', TO_Date( '10/12/2001 12:00:00 AM', 'MM/DD/YYYY HH:MI:SS AM'));
INSERT INTO TEST ( ID, ETAT, DATE_CREAT ) VALUES (
4, 'C', TO_Date( '11/12/2001 12:00:00 AM', 'MM/DD/YYYY HH:MI:SS AM'));
INSERT INTO TEST ( ID, ETAT, DATE_CREAT ) VALUES (
5, 'C', TO_Date( '12/10/2001 12:00:00 AM', 'MM/DD/YYYY HH:MI:SS AM'));
INSERT INTO TEST ( ID, ETAT, DATE_CREAT ) VALUES (
6, 'C', TO_Date( '12/13/2001 12:00:00 AM', 'MM/DD/YYYY HH:MI:SS AM'));
INSERT INTO TEST ( ID, ETAT, DATE_CREAT ) VALUES (
7, 'A', TO_Date( '12/14/2001 12:00:00 AM', 'MM/DD/YYYY HH:MI:SS AM'));
commit;
the only query i found :

select
*
from
test etat_i
where
etat_i.etat not in (
select distinct
etat_f.etat
from test etat_f
where
etat_f.id>etat_i.id
and etat_f.id < (
select min(etat_s.ID)
from
test etat_s
where
etat_s.etat != etat_i.etat
and etat_s.ID > etat_i.ID
)
)
;

If gurus got a better query....

Jul 20 '05 #4

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

Similar topics

1
by: Tamer Higazi | last post by:
Hi! I have problems creating joins between tables and to limit rows for a specified clolumn. I have 2 tables! employees departments I face the problems with the GROUP BY clause and i don't...
0
by: Lilian BRUN | last post by:
I have a table State_history : Id State DateModif object_id 1 A 10/10/01 1 2 B 10/11/01 1 3 B 11/11/01 1 4 B 11/12/01 1 5 C ...
1
by: Son KwonNam | last post by:
When I try <xsl:value-of select="count('/GROUPS/GROUP')"/> on JSTL <x:transform> tag, I got the following error message. org.apache.jasper.JasperException: Can not convert #STRING to a NodeList!...
0
by: Rob | last post by:
I need to allow a user with the default "Full Data User" permissions on the front-end database to relink to a table in a backend to which he has the same permissions. (I'd like to do read, update,...
2
by: mark | last post by:
How can I use "Group By" or a formula to group my query results in 1-year periods from a given date, e.g. 3 groups: 1 Sept 2001 - 1 Sept 2002 1 Sept 2002 - 1 Sept 2003 1 Sept 2003 - 1 Sept 2004 ...
0
by: TaeHo Yoo | last post by:
After running my code, group tree(the left section of the report) changes but not the content in the report. My code is ---------------------------------------------------------- Private Sub...
0
by: Beren | last post by:
Hello This might not be the right group for this topic, but I figure that alot of you will be able to help me out. I have IIS 5.1 running on an xp pro machine on my local network. My goal is...
4
by: Phill W. | last post by:
Is there anything amiss with the newsgroup microsoft.public.dotnet.vb.general ?? From where I'm sitting, it's /completely/ dried up - only four posts in as many days. Did I miss the mass...
5
by: Dave Smithz | last post by:
Hi there, Been working on an evolving DB program for a while now. Suddenly I have come across a situation where I need to update a table based on a group by query. For example, I have a table...
1
by: Thomas Qi | last post by:
There is a basic sql below: SELECT CTEnr AS ID, TimeStamp, DatagramSize, Source AS LocalIP, Destination AS RemoteIP, Protocol, Messages, SourcePort AS LocalPort, DestPort AS RemotePort FROM...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.