473,394 Members | 1,734 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,394 software developers and data experts.

Several Counts and grouping the results...

3
Hi,

I'm trying to get some information from several tables.

Basically I have 3 tables :

Call_Activity Table with 3 fields : Activity_ID, User_ID, Subject
User Table with 2 fields; User_ID, Name
PhoneCall Table with 2 fileds : Activity_ID, Complete_Flag

(there is more fields than that, but that's the ones I'm interrested in this particular case). I can't change the design of the database because I'm using an existing application and it's preconfigured database.

The idea that people are entering their phone activity in the base. When someone places a call it creates a new item in the Call_Activity table, linked to the user making that call (the user is listed in the User table). If the call is completed, the PhoneCall table set the Complete_Flag to true.

I want to get make a unique SQL query to display the following :

Expand|Select|Wrap|Line Numbers
  1. Names                 # of calls           # of completed calls
  2. John                     3                       2
  3. Paul                     12                     10
That would means that John made 3 calls with 2 of which completed. That would means that the table would look like (just displaying the info about John's call) :

User Table
Expand|Select|Wrap|Line Numbers
  1. User_ID   Name
  2. 1             John
Activity Table
Expand|Select|Wrap|Line Numbers
  1. Activity_ID    User_ID   Subject
  2. 1               1           Call1
  3. 2               1           Call2
  4. 3               1           Call3
PhoneCall Table
Expand|Select|Wrap|Line Numbers
  1. Activity_ID    Complete_Flag
  2. 1                  1
  3. 2                  0
  4. 1                  1
Anyone has an idea how to do this in just on SQL query?
I've been trying different things with no luck.
Thanks!!
Dec 6 '07 #1
4 1035
amitpatel66
2,367 Expert 2GB
Hi,

I'm trying to get some information from several tables.

Basically I have 3 tables :

Call_Activity Table with 3 fields : Activity_ID, User_ID, Subject
User Table with 2 fields; User_ID, Name
PhoneCall Table with 2 fileds : Activity_ID, Complete_Flag

(there is more fields than that, but that's the ones I'm interrested in this particular case). I can't change the design of the database because I'm using an existing application and it's preconfigured database.

The idea that people are entering their phone activity in the base. When someone places a call it creates a new item in the Call_Activity table, linked to the user making that call (the user is listed in the User table). If the call is completed, the PhoneCall table set the Complete_Flag to true.

I want to get make a unique SQL query to display the following :

Expand|Select|Wrap|Line Numbers
  1. Names                 # of calls           # of completed calls
  2. John                     3                       2
  3. Paul                     12                     10
That would means that John made 3 calls with 2 of which completed. That would means that the table would look like (just displaying the info about John's call) :

User Table
Expand|Select|Wrap|Line Numbers
  1. User_ID   Name
  2. 1             John
Activity Table
Expand|Select|Wrap|Line Numbers
  1. Activity_ID    User_ID   Subject
  2. 1               1           Call1
  3. 2               1           Call2
  4. 3               1           Call3
PhoneCall Table
Expand|Select|Wrap|Line Numbers
  1. Activity_ID    Complete_Flag
  2. 1                  1
  3. 2                  0
  4. 1                  1
Anyone has an idea how to do this in just on SQL query?
I've been trying different things with no luck.
Thanks!!
try this query:

Expand|Select|Wrap|Line Numbers
  1.  
  2. SELECT u.name, (SELECT count(*) FROM activity WHERE user_id = u.user_id) No_of_Calls, (SELECT COUNT(*) FROM phonecall WHERE activity_id IN ( SELECT activity_id FROM activity WHERE user_id = u.user_id) AND complete_flag = 1) Calls_Completed FROM
  3. user u
  4.  
Dec 7 '07 #2
VijaySofist
107 100+
Hi!

Please dont use the table name user because it is a System table that will be defaultly available in the SQL Server. If we use it it will display error. Anyway way we can solve that error by giving double quotes to the table name user (ex: "user").

Anyway copy the following query and paste it in your query analyser. It will work.

SELECT u.name, (SELECT count(*) FROM call_activity WHERE user_id = u.user_id) No_of_Calls, (SELECT COUNT(*) FROM phonecall WHERE activity_id IN ( SELECT activity_id FROM call_activity WHERE user_id = u.user_id) AND complete_flag = 1) Calls_Completed FROM
"user" u

All the Best

With Regards
Vijay. R
Dec 7 '07 #3
daveng
3
Thanks guys.
I'm going to try all this.

Actually, I'm not using a table named user, it's just the name I used for the description of my setting.

Many thanks again!
Dec 7 '07 #4
daveng
3
It worked, I just had to add a GROUP by name to have only one line per user.

Thanks for your help, it saved me a lot of time!
Dec 7 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Debbie Davis | last post by:
Hi there, SQL 2000 I have the following query: SELECT sponsor, COUNT(sponsor) * 2 AS total FROM Referrals GROUP BY sponsor Works great, returns the sponsor and the total * 2 of their...
4
by: kristofera | last post by:
I am trying to do a distinct grouping of some nodes sorted by a numeric value but for some reason the distinct (preceding-sibling filter) is applied to the result as if not sorted. If I don't use...
4
by: Jim | last post by:
I'm having trouble doing multiple counts with groups, ordering, and joins. Can someone help me out? Below is two table samples and what I'm trying to get my output to be: TABLEA ID ...
3
by: ahaque38 | last post by:
Hello. Using A2K SP3, I am having the following problem with a report using "Sorting and Grouping". I have recently added a grouping in the reports for "Category2<>'CONTRACTS'". I have...
8
by: Mike MacSween | last post by:
tblCourses one to many to tblEvents. A course may have an intro workshop (a type of event), a mid course workshop, a final exam. Or any combination. Or something different in the future. At...
2
by: pike | last post by:
DB2 UDB v8.2 (FP11) on AIX 5.2. I'm not an SQL guru, so please be gentle with me. I have a table that contains a BLOB(312MB) column, and I'd like to determine, in an efficient way, the...
3
by: Bill Hutchison | last post by:
I have a query that returns different results (3508 rows for snapshot, 6288 for dynaset) and that is the only thing I change to get the different results. When I try to make a table from the...
9
by: JakeTheSnake | last post by:
Hello, I'm new here, but am really impressed with the positive attitude! I was wondering if someone could give me some help or point me in the right direction. I have a query that returns the...
3
by: kevenj | last post by:
Hello, I'm a bit of a newcomer to PHP and am having an issue I can't overcome. I've read some great posts on the issue of grouping results on this forum 1 and can happily display results under...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: 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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...

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.