Home » RDBMS Server » Server Administration » SQL query syntax
SQL query syntax [message #373860] Wed, 16 May 2001 03:26 Go to next message
Sarah
Messages: 15
Registered: June 1999
Junior Member
Hi,

I wonder if anyone can help me on this query.

I am trying to find the higher number in a chain for each of the chains existing in a table.

The relevant table rows are:

Chain No. Chain sequence
============================
1 ............... 1
1 ............... 2
1 ............... 3
2 ............... 1
2 ............... 2

the result I expect is:
chain no. chain sequence
1 ............... 3
2 ............... 2

I tried the following query:

select distinct chain_n, (select max(chain_id) from table_name ) from table_name;

but did not get the results I need. Instead I got this:
chain no. chain sequence
1 ............... 3
2 ............... 3

Can anyone help me with this query syntax?

Thanks, Sarah
Re: SQL query syntax [message #373861 is a reply to message #373860] Wed, 16 May 2001 03:34 Go to previous messageGo to next message
GIRIDHAR KODAKALLA
Messages: 92
Registered: May 2001
Member
Hai,
I am sending the details of my query which fetches the details in the manner you require.

SQL> create table chain (chain_no number,chain_sequence number);

Table created.

SQL> insert into chain values (1,1);

1 row created.

SQL> insert into chain values (1,2);

1 row created.

SQL> insert into chain values (1,3);

1 row created.

SQL> insert into chain values (2,1);

1 row created.

SQL> insert into chain values (2,2);

1 row created.

SQL> commit;

Commit complete.

SQL> select * from chain;

CHAIN_NO CHAIN_SEQUENCE
--------- --------------
1 1
1 2
1 3
2 1
2 2

SQL> select chain_no,count(chain_sequence) from chain group by chain_no order by
count(chain_sequence) desc

CHAIN_NO COUNT(CHAIN_SEQUENCE)
--------- ---------------------
1 3
2 2

Hope This Helps.

Cheers,
Giridhar
Re: SQL query syntax [message #373862 is a reply to message #373860] Wed, 16 May 2001 03:36 Go to previous messageGo to next message
GIRIDHAR KODAKALLA
Messages: 92
Registered: May 2001
Member
Hai,
I am sending the details of my query which fetches the details in the manner you require.

SQL> create table chain (chain_no number,chain_sequence number);

Table created.

SQL> insert into chain values (1,1);

1 row created.

SQL> insert into chain values (1,2);

1 row created.

SQL> insert into chain values (1,3);

1 row created.

SQL> insert into chain values (2,1);

1 row created.

SQL> insert into chain values (2,2);

1 row created.

SQL> commit;

Commit complete.

SQL> select * from chain;

CHAIN_NO CHAIN_SEQUENCE
--------- --------------
1 1
1 2
1 3
2 1
2 2

SQL> select chain_no,count(chain_sequence) from chain group by chain_no order by
count(chain_sequence) desc

CHAIN_NO COUNT(CHAIN_SEQUENCE)
--------- ---------------------
1 3
2 2

Hope This Helps.

Cheers,
Giridhar
Re: SQL query syntax [message #373863 is a reply to message #373860] Wed, 16 May 2001 03:38 Go to previous messageGo to next message
GIRIDHAR KODAKALLA
Messages: 92
Registered: May 2001
Member
Hai,
I am sending the details of my query which fetches the details in the manner you require.

SQL> create table chain (chain_no number,chain_sequence number);

Table created.

SQL> insert into chain values (1,1);

1 row created.

SQL> insert into chain values (1,2);

1 row created.

SQL> insert into chain values (1,3);

1 row created.

SQL> insert into chain values (2,1);

1 row created.

SQL> insert into chain values (2,2);

1 row created.

SQL> commit;

Commit complete.

SQL> select * from chain;

CHAIN_NO CHAIN_SEQUENCE
--------- --------------
1 1
1 2
1 3
2 1
2 2

SQL> select chain_no,count(chain_sequence) from chain group by chain_no order by
count(chain_sequence) desc

CHAIN_NO COUNT(CHAIN_SEQUENCE)
--------- ---------------------
1 3
2 2

Hope This Helps.

Cheers,
Giridhar
Re: SQL query syntax [message #373864 is a reply to message #373860] Wed, 16 May 2001 03:41 Go to previous messageGo to next message
GIRIDHAR KODAKALLA
Messages: 92
Registered: May 2001
Member
Hai,
I am sending the details of my query which fetches the details in the manner you require.

SQL> create table chain (chain_no number,chain_sequence number);

Table created.

SQL> insert into chain values (1,1);
SQL> insert into chain values (1,2);
SQL> insert into chain values (1,3);
SQL> insert into chain values (2,1);
SQL> insert into chain values (2,2);
SQL> commit;
SQL> select * from chain;

CHAIN_NO CHAIN_SEQUENCE
--------- --------------
1 1
1 2
1 3
2 1
2 2
SQL> select chain_no,count(chain_sequence) from chain group by chain_no order by
count(chain_sequence) desc

CHAIN_NO COUNT(CHAIN_SEQUENCE)
--------- ---------------------
1 3
2 2

Hope This Helps.

Cheers,
Giridhar
Re: SQL query syntax [message #373865 is a reply to message #373860] Wed, 16 May 2001 04:40 Go to previous messageGo to next message
GIRIDHAR KODAKALLA
Messages: 92
Registered: May 2001
Member
Hai,
I am sending the details of my query which fetches the details in the manner you require.

SQL> create table chain (chain_no number,chain_sequence number);

Table created.

SQL> insert into chain values (1,1);
SQL> insert into chain values (1,2);
SQL> insert into chain values (1,3);
SQL> insert into chain values (2,1);
SQL> insert into chain values (2,2);
SQL> commit;
SQL> select * from chain;

CHAIN_NO CHAIN_SEQUENCE
--------- --------------
1 1
1 2
1 3
2 1
2 2
SQL> select chain_no,count(chain_sequence) from chain group by chain_no order by
count(chain_sequence) desc

CHAIN_NO COUNT(CHAIN_SEQUENCE)
--------- ---------------------
1 3
2 2

Hope This Helps.

Cheers,
Giridhar
Re: SQL query syntax [message #373897 is a reply to message #373860] Wed, 16 May 2001 13:03 Go to previous messageGo to next message
me
Messages: 66
Registered: August 2000
Member
select chain no, max(chain sequence)
from table_name
group by chain_no
Re: SQL query syntax [message #373904 is a reply to message #373860] Wed, 16 May 2001 23:44 Go to previous message
Sarah
Messages: 15
Registered: June 1999
Junior Member
Hi everyone,
It worked.

Thank you for the help :-)
Sarah
Previous Topic: Supressing blank spaces
Next Topic: how to fix ora-936 and ora-908
Goto Forum:
  


Current Time: Mon Dec 23 09:57:08 CST 2024