473,662 Members | 2,593 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Getting count with multiple fields

Hi all,

I'm running into a road block, and I know I've done this before. I'm
getting fields from two tables, and I need to do a count of similar
items with it showing some extra info.

Here's my fields:
Log.LogId - Int
Log.LogDispatch erID - Int
Officer.Officer ID - Int
Officer.Officer FirstName - Varchar
Officer.Officer LastName - Varchar

I can get the info I need without a count with this:

select a.LogID,
a.LogDispatcher ID,
b.OfficerFirstn ame + ' ' + b.OfficerLastna me as OfficerName
from [Log] a, Officer b
where a.LogAssigned1 = b.OfficerID

But when I try to add a count and group-by it errors out:

select Count(a.LogID) as LogCount,
a.LogDispatcher ID,
b.OfficerFirstn ame + ' ' + b.OfficerLastna me as OfficerName
from [Log] a, Officer b
where a.LogAssigned1 = b.OfficerID
Group By a.LogID

I've done this before, but this isn't working. It's giving the error
"it is not contained in either an aggregate function or the GROUP BY
clause" for each field other then LogID.

How can I do this? I want output similar to this:

LogCount LogDispatchID OfficerName
3 34 Tom Jones
4 22 John Smith
.... Etc

Thanks for any suggestions or ideas...

Sam Alex

Sep 6 '05 #1
2 3955
Assuming that there is only one possible officer and dispatcher for
each LogID, then you could do this:

select
count(a.LogID) as LogCount,
max(a.LogDispat cherID),
max(b.OfficerFi rstname + ' ' + b.OfficerLastna me) as
OfficerName
from
dbo.[Log] a
join dbo.Officer b
on a.LogAssigned1 = b.OfficerID
Group By
a.LogID

However, that may not be what you want, depending on what your data
looks like, if there can be several officers or dispatchers for each
LogID etc. So if this isn't helpful, or gives you the wrong answer, I
suggest you post CREATE TABLE and INSERT statements to set up a simple
test case - that way others can copy and paste into QA to test it.

http://www.aspfaq.com/etiquette.asp?id=5006

Simon

Sep 6 '05 #2
haggul
7 New Member
Try this :

select Count(a.LogID) as LogCount,
a.LogDispatcher ID,
b.OfficerFirstn ame + ' ' + b.OfficerLastna me as OfficerName
from [Log] a, Officer b
where a.LogAssigned1 = b.OfficerID
Group By a.LogDispatcher ID, b.OfficerFirstn ame + ' ' + b.OfficerLastna me

You need to specify in the group by that values that will result in the record splits in the results
Sep 6 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2032
by: Bill | last post by:
I'm submitting data through a form with multiple rows on it, that goes out to a processing page, which then scrolls through the data row by row and sends it into a database table. It is truncating one of the strings, the book tile string, so that if there are three words in it, only the first is getting submitted to the table. I have a feeling that it is because I have numbered the fields that are being submitted to the processing page,...
4
17619
by: Tim Morrison | last post by:
SQL Server 2000 - MSDE 2000 Is there a way to get the number of current users logged into a SQL 2000 Server (also MSDE)? Cant be distinct users as most users are logged into the database using the same login. -- Tim Morrison -------------------------------------------------------------------------------- Vehicle Web Studio - The easiest way to create and maintain your vehicle related website.
7
2284
by: Schraalhans Keukenmeester | last post by:
X-Followup: comp.lang.php I have a PHP script that adds messages to a simple MySQL Database. (PHP 5.0.3, MySQL 4.1.1) One of the fields it stores is msgid. The new msgid is a count of all current msgs in the db plus one $query = 'select count(*) from messagesdb;'; $result = mysql_query ($query, $conn);
2
1785
by: JimJim | last post by:
Wondering if someone here could help me out, I've tried a number of ways to do this, and while I have managed to get it to work, Im SURE im not doing the optimal way, and it seems to be causing problems with my form which is throwing up '#Name? ' in some fields(at seemingly random times...) Basicly I have 3 tables, one being the 'top' table, with only 2 fields, a primary key (ID) and a second field Name, this table is called 'tblDests'
2
3139
by: cefrancke | last post by:
I can't seem to find a straight answer for my specific issue. Any help would be appreciated. I would like to count the various items in a table where the fields have a 'group' relationship. I would like to count the items per group. (Please forgive the inappropriate use of database terms.) An example follows:
1
2725
by: griemer | last post by:
I have a database like this id, field1,field2,field3,field4,field5 Database contains 100 rows, some rows have no fields filled, some 1field , some 2 fields etc. How would i count the number of fields filled in total? So the outcome is (number of fields filled in row1)+(number of fields
1
2929
by: Phoenix_ver10 | last post by:
I have a mailing list with multiple names going to the same addresses. I need one address with all the names for that address on it. I checked out the example on microsoft's site, but A: It doesn't work (error that there is an extra parenthise (sp?) ) and B: Will only let in two names for each record. If there are three, the middle on is deleted. Or to make things simpler, if nothing else, I'd like to add a field in the table that shows...
0
1656
by: midnight_use_only | last post by:
although i don't think this is do-able using SQL, i would ask and maybe an expert can help me out. assum i have the following table: Date Code Count 2006-06-06 abc 1234 2006-06-06 abc 14 2006-06-06 abc 87 2006-06-06 xyz 1353 2006-06-06 xyz 19 2006-06-06 xyz 86
22
12464
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=" & msDbFilename moConn.Properties("Persist Security Info") = False moConn.ConnectionString = msConnString moConn.CursorLocation = adUseClient moConn.Mode = adModeReadWrite' or using default...same result
0
8344
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8764
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8546
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8633
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7367
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6186
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
2762
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1993
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1752
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.