Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

sub select question

sub select question

sub select question

I want to use one query to generate this type of data from one table.

Phone Number          #calls           Avg_Duration(sec)
xxx-xxx-xxxx          1,234               12.34
yyy-yyy-yyyy         12,032                9.03
zzz-zzz-zzzz            435               13.45


SELECT Distinct
  (SELECT count(distinct ID) FROM call WHERE PhoneNumber = c.PhoneNumber) #calls,
  (SELECT AVG( duration ) FROM call WHERE PhoneNumber = c.PhoneNumber) AVG_Duration(sec)
  Call c
  1 ASC

Am I trying to do something completely wrong with my Sub Select statements which call for the table and element in the external Select Statment?

RE: sub select question

The SQL syntax is wrong; please post back the original table design, along with a few sample records.

"Don't be irreplaceable. If you can't be replaced, you can't be promoted."

RE: sub select question

You also might consider posting in the ANSI-SQL forum forum220: ANSI_SQL or in the Microsoft, DB2, Oracle, or appropriate database forum for your database SQL.  

The trouble with doing something right the first time is that nobody appreciates how difficult it was - Steven Wright

RE: sub select question

Call Table
callID:int (key)

CallID     duration          PhoneNumber
1          1.23              xxx-xxx-xxxx
2          3.23              yyy-yyy-yyyy
3          2.10              zzz-zzz-zzzz
4          2.23              xxx-xxx-xxxx
5          4.32              yyy-yyy-yyyy
6          8.02              xxx-xxx-xxxx
7          1.20              xxx-xxx-xxxx

Looking for output like:

NumberCalled      #calls     Average_Duration
============      ======     ================
xxx-xxx-xxxx        4        3.17
yyy-yyy-yyyy        2        3.78
zzz-zzz-zzzz        1        2.10

RE: sub select question

Something like:


SELECT PhoneNumber,
 (SELECT Count (*) FROM calls c1 where c1.PhoneNumber = calls.PhoneNumber) As NumCalls,
 SELECT Avg (Duration) FROM calls c2 where c2.PhoneNumber = calls.PhoneNumber As AverageDuration
FROM Calls
GROUP BY PhoneNumber
ORDER BY PhoneNumber

As Genomon and Johnherman say, you don't specify which database engine you are using, so we can't provide an exact response.


RE: sub select question

Is there something I don't understand here, but isn't this just a basic GROUP BY query???

SELECT phoneNumber,
       COUNT(*) AS #calls,
       AVG(duration) AS AVG_Duration

BTW, CALL is a reserved word in standard SQL. To be avoid future problems double quote it ("CALL"), or rename the table.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close