If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > need help to built one sql query

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-24-03, 02:46
neelamchalam neelamchalam is offline
Registered User
 
Join Date: Jul 2003
Location: india
Posts: 15
Question need help to built one sql query

Hi, Could anybody please give me Solution for the following Problem.

Table Name: Test_Source

NAME AGE QUALIFIES
---------- ----------- -----------
Test0 26 9
Test1 21 9
Test2 15 0
Test3 15 0
Test4 21 9
Test5 45 4
Test6 20 9
Test7 20 9

The above table Test_Source is the source table. I need to write a query, which will generate data like following from the Test_Source.


Table Name: Test_Result

NoofOccurs Count
------------ ------------------
1 2
2 3
3 0
4 0

In Test_Result table
1. The first row meaning is that 26,45 are only two ages, which have only one occurrence.
2. The second row meaning is that 21,15,20 are only three ages, which have each two occurrence.
3. The third row meaning is that there is no age which have three occurrences.

Thanks in Advance,

Chalam N
Reply With Quote
  #2 (permalink)  
Old 09-24-03, 03:19
gannet gannet is offline
Registered User
 
Join Date: Oct 2002
Location: Plymouth UK
Posts: 116
Re: need help to built one sql query

The following should work and get you started:

Code:
SELECT a.num, COUNT(a.num)
FROM
  (SELECT COUNT(*)
   FROM test_source
   GROUP BY age) a
GROUP BY a.num
Reply With Quote
  #3 (permalink)  
Old 09-24-03, 07:00
neelamchalam neelamchalam is offline
Registered User
 
Join Date: Jul 2003
Location: india
Posts: 15
Re: need help to built one sql query

Quote:
Originally posted by gannet
The following should work and get you started:

Code:
SELECT a.num, COUNT(a.num)
FROM
  (SELECT COUNT(*)
   FROM test_source
   GROUP BY age) a
GROUP BY a.num
Hi Gannet ,Thank you for your kind help, but still the above query is not giving the result what i need. here i am getting result with combination of the following three queries . could you please suggest me one optimized way to get my results.

1.create table temp1 (age int, occurs int);
2.insert into temp1(age, occurs) (select age, count(*) from test_source group by age);
3. select occurs, count(*) from temp1 group by occurs order by occurs asc;

Thanking You,
Chalam N
Reply With Quote
  #4 (permalink)  
Old 09-24-03, 07:56
neelamchalam neelamchalam is offline
Registered User
 
Join Date: Jul 2003
Location: india
Posts: 15
Re: need help to built one sql query

Quote:
Originally posted by gannet
The following should work and get you started:

Code:
SELECT a.num, COUNT(a.num)
FROM
  (SELECT COUNT(*)
   FROM test_source
   GROUP BY age) a
GROUP BY a.num
Hi gannet,

sorry for sending reply that your given query is not giving results,

your given query is executing with good performance with a little modification in that one as follows.

SELECT a.num, COUNT(a.num)
FROM
(SELECT COUNT(*) as num
FROM test_source
GROUP BY age) a
GROUP BY a.num

Thank you very much,

Chalam N
Reply With Quote
  #5 (permalink)  
Old 09-24-03, 08:35
neelamchalam neelamchalam is offline
Registered User
 
Join Date: Jul 2003
Location: india
Posts: 15
Re: need help to built one sql query

Quote:
Originally posted by gannet
The following should work and get you started:

Code:
SELECT a.num, COUNT(a.num)
FROM
  (SELECT COUNT(*)
   FROM test_source
   GROUP BY age) a
GROUP BY a.num
Hi gannet,

sorry for sending reply that your given query is not giving results,

your given query is executing with good performance with a little modification in that one as follows.

SELECT a.num, COUNT(a.num)
FROM
(SELECT COUNT(*) as num
FROM test_source
GROUP BY age) a
GROUP BY a.num

Thank you very much,

Chalam N
Reply With Quote
  #6 (permalink)  
Old 09-24-03, 09:38
gannet gannet is offline
Registered User
 
Join Date: Oct 2002
Location: Plymouth UK
Posts: 116
Ooops many apologies for the omission, brain to hand co-ordination failure on my part. Glad to have helped.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On