COUNT IN SQL QUERY

User avatar
sal21
PlatinumLounger
Posts: 4357
Joined: 26 Apr 2010, 17:36

COUNT IN SQL QUERY

Post by sal21 »

I use this query to count value in field

Code: Select all

SELECT RDT, RDTAB, COUNT(EI) AS SOMMAMD FROM L0928 GROUP BY  RDT, RDTAB
but based the joint of rdt&rdtab i need to count all field ei,up,ci,ep
how to?

User avatar
HansV
Administrator
Posts: 78512
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: COUNT IN SQL QUERY

Post by HansV »

Just add a comma and COUNT(fieldname) for each field that you want to count.

Keep in mind that COUNT(fieldname) will return the number of records with non-blank values in that field. If the fields are all filled in, the count will be the same for all fields.
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4357
Joined: 26 Apr 2010, 17:36

Re: COUNT IN SQL QUERY

Post by sal21 »

HansV wrote:Just add a comma and COUNT(fieldname) for each field that you want to count.

Keep in mind that COUNT(fieldname) will return the number of records with non-blank values in that field. If the fields are all filled in, the count will be the same for all fields.
???, sorry

ahhhhhhhhhhhhhhh. resolved.
Tks.

But i need to return in other column the joint rdt&"-"&rdtab, is posible?

this is my sql:

Code: Select all

SELECT L0928.RDT, L0928.RDTAB, Count(L0928.EI) AS SOMMAMDEI, Count(L0928.UP) AS SOMMAMDUP, Count(L0928.CI) AS SOMMAMDCI, Count(L0928.EP) AS SOMMAMDEP
FROM L0928
GROUP BY L0928.RDT, L0928.RDTAB;

User avatar
HansV
Administrator
Posts: 78512
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: COUNT IN SQL QUERY

Post by HansV »

Here is the SQL for a query based on the Orders table in the Northwind database that counts three fields by employee:

SELECT fkeyEmployeeID, Count(fkeyCustomerID) AS CountCustomers, Count(fkeyShipVia) AS CountShipVia, Count(curFreight) AS CountFreight
FROM tblOrders
GROUP BY fkeyEmployeeID;

Here is the query in design view:
x370.png
And in datasheet view:
x371.png
As you see, the three count fields have the same numbers in the first row, and also the same numbers in the second row, etc.
You do not have the required permissions to view the files attached to this post.
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4357
Joined: 26 Apr 2010, 17:36

Re: COUNT IN SQL QUERY

Post by sal21 »

HansV wrote:Here is the SQL for a query based on the Orders table in the Northwind database that counts three fields by employee:

SELECT fkeyEmployeeID, Count(fkeyCustomerID) AS CountCustomers, Count(fkeyShipVia) AS CountShipVia, Count(curFreight) AS CountFreight
FROM tblOrders
GROUP BY fkeyEmployeeID;

Here is the query in design view:
x370.png
And in datasheet view:
x371.png
As you see, the three count fields have the same numbers in the first row, and also the same numbers in the second row, etc.
:thankyou: :cheers: :clapping:

Note:
Can you add in icon menu of post the "pasta" and "pizza" for my personal use
:laugh: :laugh: :laugh: :laugh: