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

Aggregate Query

I have certain tables as follows:

UserID UserName CallDT ResultCode

I am trying to run a query that will show me the total amount of calls for "UserID"

This is what I have done.

Expand|Select|Wrap|Line Numbers
  1. SELECT     UserID, Sum(CAST(UserID as Int))AS 'Total',  UserName, CallDT, ResultCode
  2. FROM         CallHist
  3. WHERE     (CallDT BETWEEN '2010-08-17' AND '2010-08-18') AND (NOT (UserID = N'PDIALER'))
  4. GROUP BY UserID, UserName, CallDT, ResultCode
The problem is that it doesn't show me the total for an agent. This is what it gives me.

UserID | Total | UserName | CallDT | ResultCode
2005 |2005 |Craig Costello|2010-08-17 | 80
2005 |2005 |Craig Costello|2010-08-17 | 45
2046 |2046 |Joe Servantez| 2010-08-17 | 80
2046 |2046 |Joe Servantez| 2010-08-17 | 71
2046 |2046 |Joe Servantez| 2010-08-17 | 71
2086 |2086 |Vic Day | 2010-08-17 | 85
2086 |2086 |Vic Day | 2010-08-17 | 7
2086 |2086 |Vic Day | 2010-08-17 | 71

What I want to accomplish is the following:

UserID | Total | Username | CallDT | RC80 |RC45|
2005 |---2---| Craig C. | 2010-08-17|---1---|--1-|

So basically UserID who is Username made a total of 2 calls on 2010-08-17 and out of those 2 calls 1 was from ResultCode 80 and the other from ResultCode 45

Could someone please help me with this. I am losing my mind.

Thank you,
Aug 23 '10 #1
8 1155
Oralloy
988 Expert 512MB
Well, there's two ways - use SUM(1) or COUNT(*)

Either should work.

Good luck.

Cheers!
Aug 23 '10 #2
colintis
255 100+
SQL cannot get your last requirements as it will not showing multiple columns from the same field. Your best approach for this will be:

UserID | Total | UserName | CallDT | ResultCode
2005 |1 |Craig Costello|2010-08-17 | 80
2005 |1 |Craig Costello|2010-08-17 | 45
2046 |1 |Joe Servantez| 2010-08-17 | 80
2046 |2 |Joe Servantez| 2010-08-17 | 71
2086 |1 |Vic Day | 2010-08-17 | 85
2086 |1 |Vic Day | 2010-08-17 | 7
2086 |1 |Vic Day | 2010-08-17 | 71

With this:
Expand|Select|Wrap|Line Numbers
  1. SELECT     UserID, Count(DISTINCT UserID)AS 'Total',  UserName, CallDT, ResultCode
  2. FROM         CallHist
  3. WHERE     (CallDT BETWEEN '2010-08-17' AND '2010-08-18') AND (NOT (UserID = N'PDIALER'))
  4. GROUP BY UserID, UserName, CallDT, ResultCode
Aug 24 '10 #3
colintis,
I tried what you said and it gives the same result as the one that I did.

Do you have any other suggestions?
Aug 24 '10 #4
Oralloy
988 Expert 512MB
Ok, now I understand. You need to use a TRANSFORM query something like this:
Expand|Select|Wrap|Line Numbers
  1. TRANSFORM COUNT(*) AS 'Total'
  2. SELECT  COUNT(*),  UserName, CallDT, ResultCode 
  3.   FROM  CallHist
  4.   WHERE  (CallDT BETWEEN '2010-08-17' AND '2010-08-18')
  5.          AND (UserID <> N'PDIALER')
  6.   GROUP BY UserID, UserName, CallDT, ResultCode
  7. PIVOT UserName
  8.  
Outside of that, it's been a while since I've done such a thing. They're usually called 'Pivot Tables', or 'Pivot Queries', if you need to google them.

Try this link for some useful pages:
http://www.google.com/#hl=en&q=sql+t...RANSFORM+QUERY
Aug 24 '10 #5
colintis
255 100+
A direct reference which I think it explains more clearly on
TRANSFORM QUERY
Aug 24 '10 #6
Well it seems like I am getting there. This is what I have now:

Expand|Select|Wrap|Line Numbers
  1. TRANSFORM Count(callhist.userid) AS total
  2. SELECT callhist.userid, callhist.username, callhist.resultcode
  3. FROM callhist
  4. WHERE (((callhist.calldt)>=[Enter Start date] And (callhist.calldt)<=[Enter End Date]))
  5. GROUP BY callhist.userid, callhist.username, callhist.resultcode
  6. PIVOT callhist.resultcode;
  7.  
I want to say thank you for the helpful links this is going to be useful but the problem I am having now is that in the "WHERE" statement I am getting this error:

"Microsoft Access database engine does not recognize '[Enter Start date]' as a valid field name or expression"

I use this same statement in other queries but the only difference is I have it identified in the "ORDER BY" statement at the end.

Can you tell me why I am getting this error?
Aug 24 '10 #7
colintis
255 100+
Give a try with referencing the form address such as
Expand|Select|Wrap|Line Numbers
  1. [forms]![<your form>]![Enter Start Date]
a reference of a similar case
Aug 24 '10 #8
Oralloy
988 Expert 512MB
@Danyluk,

I think you need to have a parameter bound to your query. Unfortunately I'm not the Access guy to answer that question for you directly.

Are you familiar with using DAO to execute SQL against your DB?

Also, I think that the COUNT value will always be 1 in your query there, but I'm not sure.

Luck!
Aug 24 '10 #9

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

Similar topics

2
by: Toby Dann | last post by:
I have an aggregate query as recordsource for a form to show a list of invoices, including the totals (calculated by the aggregate query - I'm trying to duplicate as little info as possible here)....
1
by: sausage31 | last post by:
I have a table as follows.... Device LotID Result1 Result2 Result3 aaa 1 5 10 15 bbb 1 2 4 6 aaa 2 ...
10
by: neb | last post by:
Dear member of the forum, Ms access has built-in aggregate function like: -Sum, Max, First, Avg, ... Is it possible to build user-defined aggregate? (if you have any clue, do not hesitate to...
1
by: Scott Gerhardt | last post by:
Hello, I am new to the list, my apology if this question is beyond the scope or charter of this list. My questions is: What is the best method to perform an aggregate query to calculate sum()...
2
by: lrgclasper | last post by:
Greetings, I have the following select statement from a single table ( Haul number, weight, species code). I would like the output to provide me with hauls with only two species. When I use...
3
by: brent78 | last post by:
I'm trying to create a query that finds the max of a group of records and then displays all details about that record. Suppose this is my data: Type Size Color Quantity A L Green 5 A M Blue 7...
3
by: John | last post by:
Is there a way to create a grouping query and be allowed to edit the data. I want to create a query that has a grouping and totals but also allow the user to edit. Can it be done?
1
by: lorirobn | last post by:
Hi, I have a report that displays summary information, summing prices for all records for a RoomID meeting certain criteria, and printing the roomID and sum on a detail line. Now I want to add...
4
by: sbowden81 | last post by:
Hi all, I'm trying to create what I thought was a simple inventory query, but I'm having a problem. I have a shipment table that looks like this: SHIP_ID SHIP_QTY 1 24 2 ...
1
by: clickingwires | last post by:
How do you consecutively number rows in an aggregate query?
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...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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.