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

Count function in a query

15
Thanks for all the help so far, my database is starting to come along well now.

I am having trouble with a query at the minute. Basically it goes like this:

I have a main table to my database which holds client info, including city name.

Through a many to many relationship this is conected to a contact table which records contacts with the clients. This table has a field titled outcome in which either 'no reply', 'call later', or returned call' is recorded

What I am trying to do is run a query that will sort and count call types by location (city)

i.e.

[PHP]Count Call type City

3 call later London
6 no reply London
3 call later Birmingham[/PHP]

I hope this reads ok for you, any help is much appreaciated!
Jan 11 '07 #1
14 2385
cyberdwarf
218 Expert 100+
Hi

Try something like this
Expand|Select|Wrap|Line Numbers
  1. Select Count(*), CallType, City From tblClient
  2. Group By City, CallType
  3. Order By City, CallType
HTH
Steve
Jan 11 '07 #2
lostdog
15
Hi

Try something like this
Expand|Select|Wrap|Line Numbers
  1. Select Count(*), CallType, City From tblClient
  2. Group By City, CallType
  3. Order By City, CallType
HTH
Steve
Cheers for that,

I have modified it slighty

Expand|Select|Wrap|Line Numbers
  1. SELECT Count(*) AS Expr1, Calls.Subject, tblclient.city
  2. FROM contacts INNER JOIN Calls ON tblclient.ContactID = Calls.ContactID
  3. GROUP BY Calls.Subject, tblclient.city
  4. ORDER BY tblclient.city, Calls.Subject;
Then something strange happens...

I tested it on a 5 record db

thing is it works perfectly for all the records except 2 and 5

for some reason the query returns results like so

Count Subject City

2 no reply london
3 no reply london

etc, as I have said all the other records sort and count perfectly.

the subject field is a text input, I have checked the formatting and it corresponds with that of other records.

The query seems to treat these 2 records as separate counts, anyone got any ideas?

Thanks
Jan 11 '07 #3
NeoPa
32,556 Expert Mod 16PB
Look at the data very carefully.
Are there any extraneous spaces or other white-space chars?
Jan 12 '07 #4
MMcCarthy
14,534 Expert Mod 8TB
Try this ...

Expand|Select|Wrap|Line Numbers
  1.  SELECT Count(*) AS Expr1, Calls.Subject, tblclient.city
  2. FROM tblclient INNER JOIN Calls ON tblclient.ContactID = Calls.ContactID
  3. GROUP BY Calls.Subject, tblclient.city
  4. ORDER BY tblclient.city, Calls.Subject;
You never replaced the contacts table name with tblclient
Jan 12 '07 #5
NeoPa
32,556 Expert Mod 16PB
Good spot Mary.
I've added the code tags to the post (#3) so that it's more clearly visible what was used.
Jan 12 '07 #6
lostdog
15
Thanks for the help, bit of an oversight there!!

I thought about Neopa's post an decided it would be better to use a dropdown list to select an outcome to avoid the chance of white space errors.

One thing I am trying to sort out is the query display, the dropdown list is as usual supported by an autonumbered table. How do I go about getting the linked text to appear in the query result as opposed to just the autonumber number?

Thanks
Jan 15 '07 #7
NeoPa
32,556 Expert Mod 16PB
Try the Column property of the ComboBox.
Press F1 on the word Column from the VBA window for a fuller explanation of the Column item.
Jan 16 '07 #8
lostdog
15
Cheers for that,

I have modified it slighty

Expand|Select|Wrap|Line Numbers
  1. SELECT Count(*) AS Expr1, Calls.Subject, tblclient.city
  2. FROM tblclient INNER JOIN Calls ON tblclient.ContactID = Calls.ContactID
  3. GROUP BY Calls.Subject, tblclient.city
  4. ORDER BY tblclient.city, Calls.Subject;
Then something strange happens...

I tested it on a 5 record db

thing is it works perfectly for all the records except 2 and 5

for some reason the query returns results like so

Count Subject City

2 no reply london
3 no reply london

etc, as I have said all the other records sort and count perfectly.

the subject field is a text input, I have checked the formatting and it corresponds with that of other records.

The query seems to treat these 2 records as separate counts, anyone got any ideas?

Thanks
I got the last bit sorted thank you Neopa (again)

I made the corrections that mmccarthy pointed out.

I found out why the counts were running the way I showed, its because of the different call dates, i.e. it counts all the calls from one day and sums them by city and subject. Is there any way to ammend the sql so it sums the totals for the user inputed dates?

Thanks

Stephen
Jan 16 '07 #9
NeoPa
32,556 Expert Mod 16PB
Do you have your latest (as correct as possible) SQL?
Jan 16 '07 #10
lostdog
15
Do you have your latest (as correct as possible) SQL?
I'm little further on from my last post

Expand|Select|Wrap|Line Numbers
  1. SELECT Count(*) AS Expr1, Calls.Subject, contacts.ContactTypeID, Calls.CallDate
  2. FROM contacts INNER JOIN Calls ON contacts.ContactID = Calls.ContactID
  3. GROUP BY Calls.Subject, contacts.ContactTypeID, Calls.CallDate
  4. HAVING (((Calls.CallDate) Between [Enter Start of Date Range] And [Enter End of Date Range]))
  5. ORDER BY contacts.ContactTypeID, Calls.Subject;


It seems when I add the date range option in the above code it changes the type of result
Jan 16 '07 #11
MMcCarthy
14,534 Expert Mod 8TB
Is this what you're looking for?

Expand|Select|Wrap|Line Numbers
  1. SELECT Count(*) AS Expr1, Calls.Subject, contacts.ContactTypeID
  2. FROM contacts INNER JOIN Calls 
  3. ON contacts.ContactID = Calls.ContactID
  4. WHERE [last contact date] Between [Enter Start of Date Range] And [Enter End of Date Range]
  5. GROUP BY Calls.Subject, contacts.ContactTypeID
  6. ORDER BY contacts.ContactTypeID, Calls.Subject;
  7.  
Mary
Jan 16 '07 #12
NeoPa
32,556 Expert Mod 16PB
I'm little further on from my last post

Expand|Select|Wrap|Line Numbers
  1. SELECT Count(*) AS Expr1, Calls.Subject, contacts.ContactTypeID, Calls.CallDate
  2. FROM contacts INNER JOIN Calls ON contacts.ContactID = Calls.ContactID
  3. GROUP BY Calls.Subject, contacts.ContactTypeID, Calls.CallDate
  4. HAVING (((Calls.CallDate) Between [Enter Start of Date Range] And [Enter End of Date Range]))
  5. ORDER BY contacts.ContactTypeID, Calls.Subject;


It seems when I add the date range option in the above code it changes the type of result
Expand|Select|Wrap|Line Numbers
  1. SELECT Count(*) AS Expr1, Calls.Subject, contacts.ContactTypeID, Calls.CallDate
  2. FROM contacts INNER JOIN Calls ON contacts.ContactID = Calls.ContactID
  3. WHERE (Calls.CallDate Between [Enter Start of Date Range] And [Enter End of Date Range])
  4. GROUP BY contacts.ContactTypeID, Calls.Subject, Calls.CallDate
  5. ORDER BY contacts.ContactTypeID, Calls.Subject;
This is a simple tidy up of your SQL.
Looking at your latest request I'm a little stuck :
You show examples using a field (City) but this isn't in your SQL at all.
Do you want to GROUP BY date alone (have only one result line ever for each date)?
If so, you need to decide what you want to do with the other fields.
Expand|Select|Wrap|Line Numbers
  1. ------------Input--------    ----------------Output----------------
  2. Date        City    Value    Date (Group)  City (?)     Value (Sum)
  3. 1 Jan 2007  London   32
  4. 1 Jan 2007  Bristol  12
  5. 1 Jan 2007  London   19      1 Jan 2007    Undefined(?)  63
If they are to be included at all, you need to treat them as some sort of Aggregate. Otherwise you may get extra output lines for each different item found in the field.
In this example I use City but it can be any non-aggregated field in a GROUP BY query.

If the same data had a query with a GROUP BY including the other field (City in this case) you get :
Expand|Select|Wrap|Line Numbers
  1. ------------Input--------    ----------------Output----------------
  2. Date        City    Value    Date (Group)  City (Group) Value (Sum)
  3. 1 Jan 2007  London   32
  4. 1 Jan 2007  London   19      1 Jan 2007    London        51
  5. 1 Jan 2007  Bristol  12      1 Jan 2007    Bristol       12
Jan 16 '07 #13
lostdog
15
Just a thank you for all your help, time and patience, my db is now nearly complete.
Jan 20 '07 #14
NeoPa
32,556 Expert Mod 16PB
Good to hear it lostdog. I'm glad to have helped.
Jan 21 '07 #15

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

Similar topics

3
by: Matthias Haffke | last post by:
Ok, this is a tricky question for the pro's: My access sheet: line, id a, id b, val% ---------------- 1, a, ac, 0.04 2, a, ac, 0.28 3, a, ac, 0.015 4, a, ac, 0.205
9
by: Terry E Dow | last post by:
Howdy, I am having trouble with the objectCategory=group member.Count attribute. I get one of three counts, a number between 1-999, no member (does not contain member property), or 0. Using...
3
by: Hyphessobricon | last post by:
Hallo, Indeed, a count of a query with a group by function gives more records than there are and so for-next structures don't function. How is this to be mended. Anyone? Everyone in fact....
6
by: dBNovice | last post by:
Hey group, I am trying to do a count of the number of papers in a table. The table has a PaperID that differentiates each paper , e.g. 004.1. Some papers are reused. The reused paper is given...
3
by: auron | last post by:
Hi there, I have a really stupid and banal problem with showing the results of a MySQL query in PHP, preciselly with MySQL count() function that gives to a variable in PHP the result. NOTE:...
1
by: heckstein | last post by:
I am working in Access 2002 and trying to create a report from our company's learming management system. I am not a DBA and most of my SQL knowledge has been self taught through trial and error. I...
22
by: MP | last post by:
vb6,ado,mdb,win2k i pass the sql string to the .Execute method on the open connection to Table_Name(const) db table fwiw (the connection opened via class wrapper:) msConnString = "Data Source="...
9
by: Akhenaten | last post by:
The following snippet (for whatever reason) returns no value for the count. Suggestions? $arr = array ("A", "B", "C", "D", "E"); foreach ($arr as $client) { $count = mysql_query('SELECT...
12
by: petter | last post by:
Hi! I have two questions: one question that regards the COUNT-function, and one about how to display a month even if I don’t have any data for that month. I have an Access database where I want...
14
by: zufie | last post by:
I have to create a QA report regarding callers calling into a phone hotline. The report consists of many checkboxes such as: Did the IBCCP agency contact you? Yes/NO How many days passed...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: 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
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...

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.