A Microsoft Office (Excel, Word) forum. OfficeFrustration

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » OfficeFrustration forum » Microsoft Access » Running & Setting Up Queries
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

Count in Query



 
 
Thread Tools Display Modes
  #1  
Old January 8th, 2007, 03:52 PM posted to microsoft.public.access.queries
David M C
external usenet poster
 
Posts: 59
Default Count in Query

I have two tables:

WorkOrder:

WorkOrderID (Primary Key)
JobNumber
OrderDate
Deadline
CompletionDate
Status

ResponseInvoice:

ResponseInvoiceID (Primary Key Autonumber)
JobNumber
InvoiceNumber
InvoiceDate

In a query, I need to see (on a per Job Number basis):

Number of Orders Raised (easy, just a Count on the ID column)
Number of Orders Completed Within Target ( Count of Orders whose
CompletionDate Deadline)
Number Of Orders Invoiced
Number Of Orders Awaiting Invoice (Status = 5)

I can do the first one, but I can't combine a Count with anything in a
criteria field. Any help would be much appreciated.

Thanks,

Dave
  #2  
Old January 8th, 2007, 07:56 PM posted to microsoft.public.access.queries
Ken Sheridan
external usenet poster
 
Posts: 3,433
Default Count in Query

Dave:

It should be possible to compute the second and last columns by summing the
return value of an expression which evaluates to 0 or 1, the third by means
of a subquery containing a further subquery. Try this:

SELECT
JobNumber,
COUNT(*) As TotalOrders,
SUM(IIF(CompletionDate Deadline,1,0)) AS WithinTarget,
(SELECT COUNT *
FROM WorkOrder AS WO2
WHERE WO2.JobNumber = WO1.JobNumber
AND EXISTS
(SELECT *
FROM ResponseInvoice
WHERE ResponseInvoice.JobNumber = WO2.JobNumber))
AS Invoiced,
SUM(IIF(Status = 5,1,0)) AS AwaitingInvoice
FROM WorkOrder AS WO1
GROUP BY JobNumber;

Ken Sheridan
Stafford, England

"David M C" wrote:

I have two tables:

WorkOrder:

WorkOrderID (Primary Key)
JobNumber
OrderDate
Deadline
CompletionDate
Status

ResponseInvoice:

ResponseInvoiceID (Primary Key Autonumber)
JobNumber
InvoiceNumber
InvoiceDate

In a query, I need to see (on a per Job Number basis):

Number of Orders Raised (easy, just a Count on the ID column)
Number of Orders Completed Within Target ( Count of Orders whose
CompletionDate Deadline)
Number Of Orders Invoiced
Number Of Orders Awaiting Invoice (Status = 5)

I can do the first one, but I can't combine a Count with anything in a
criteria field. Any help would be much appreciated.

Thanks,

Dave


  #3  
Old January 8th, 2007, 08:02 PM posted to microsoft.public.access.queries
Dale Fye
external usenet poster
 
Posts: 2,651
Default Count in Query

David,

It might look something like this:

SELECT WO.JobNumber,
Count(WO.WorkOrderID) as OrdersRaised,
SUM(IIF(WO.CompletionDate = WO.Deadline, 1, 0)) as OnTime,
SUM(IIF(ISNULL(RI.ResponseInvoiceID), 0, 1)) as Invoiced,
SUM(IIF(ISNULL(RI.ResponseInvoiceID), 1, 0)) as AwaitingInvoice
FROM WorkOrder WO
LEFT JOIN ResponseInvoice RI
ON WO.JobNumber = RI.JobNumber
GROUP BY WO.JobNumber

HTH
Dale

--
Email address is not valid.
Please reply to newsgroup only.


"David M C" wrote:

I have two tables:

WorkOrder:

WorkOrderID (Primary Key)
JobNumber
OrderDate
Deadline
CompletionDate
Status

ResponseInvoice:

ResponseInvoiceID (Primary Key Autonumber)
JobNumber
InvoiceNumber
InvoiceDate

In a query, I need to see (on a per Job Number basis):

Number of Orders Raised (easy, just a Count on the ID column)
Number of Orders Completed Within Target ( Count of Orders whose
CompletionDate Deadline)
Number Of Orders Invoiced
Number Of Orders Awaiting Invoice (Status = 5)

I can do the first one, but I can't combine a Count with anything in a
criteria field. Any help would be much appreciated.

Thanks,

Dave

  #4  
Old January 8th, 2007, 08:10 PM posted to microsoft.public.access.queries
Dale Fye
external usenet poster
 
Posts: 2,651
Default Count in Query

It didn't occur to me that there might be more than one invoice for each WO.

--
Email address is not valid.
Please reply to newsgroup only.


"Ken Sheridan" wrote:

Dave:

It should be possible to compute the second and last columns by summing the
return value of an expression which evaluates to 0 or 1, the third by means
of a subquery containing a further subquery. Try this:

SELECT
JobNumber,
COUNT(*) As TotalOrders,
SUM(IIF(CompletionDate Deadline,1,0)) AS WithinTarget,
(SELECT COUNT *
FROM WorkOrder AS WO2
WHERE WO2.JobNumber = WO1.JobNumber
AND EXISTS
(SELECT *
FROM ResponseInvoice
WHERE ResponseInvoice.JobNumber = WO2.JobNumber))
AS Invoiced,
SUM(IIF(Status = 5,1,0)) AS AwaitingInvoice
FROM WorkOrder AS WO1
GROUP BY JobNumber;

Ken Sheridan
Stafford, England

"David M C" wrote:

I have two tables:

WorkOrder:

WorkOrderID (Primary Key)
JobNumber
OrderDate
Deadline
CompletionDate
Status

ResponseInvoice:

ResponseInvoiceID (Primary Key Autonumber)
JobNumber
InvoiceNumber
InvoiceDate

In a query, I need to see (on a per Job Number basis):

Number of Orders Raised (easy, just a Count on the ID column)
Number of Orders Completed Within Target ( Count of Orders whose
CompletionDate Deadline)
Number Of Orders Invoiced
Number Of Orders Awaiting Invoice (Status = 5)

I can do the first one, but I can't combine a Count with anything in a
criteria field. Any help would be much appreciated.

Thanks,

Dave


  #5  
Old January 8th, 2007, 08:56 PM posted to microsoft.public.access.queries
Ken Sheridan
external usenet poster
 
Posts: 3,433
Default Count in Query

The invoices seem to reference JobNumber which is a non-key column in the
work orders table, which implies this. It also points to a flaw in the
logical model, I think.

Ken Sheridan
Stafford, England

"Dale Fye" wrote:

It didn't occur to me that there might be more than one invoice for each WO.

--
Email address is not valid.
Please reply to newsgroup only.


"Ken Sheridan" wrote:

Dave:

It should be possible to compute the second and last columns by summing the
return value of an expression which evaluates to 0 or 1, the third by means
of a subquery containing a further subquery. Try this:

SELECT
JobNumber,
COUNT(*) As TotalOrders,
SUM(IIF(CompletionDate Deadline,1,0)) AS WithinTarget,
(SELECT COUNT *
FROM WorkOrder AS WO2
WHERE WO2.JobNumber = WO1.JobNumber
AND EXISTS
(SELECT *
FROM ResponseInvoice
WHERE ResponseInvoice.JobNumber = WO2.JobNumber))
AS Invoiced,
SUM(IIF(Status = 5,1,0)) AS AwaitingInvoice
FROM WorkOrder AS WO1
GROUP BY JobNumber;

Ken Sheridan
Stafford, England

"David M C" wrote:

I have two tables:

WorkOrder:

WorkOrderID (Primary Key)
JobNumber
OrderDate
Deadline
CompletionDate
Status

ResponseInvoice:

ResponseInvoiceID (Primary Key Autonumber)
JobNumber
InvoiceNumber
InvoiceDate

In a query, I need to see (on a per Job Number basis):

Number of Orders Raised (easy, just a Count on the ID column)
Number of Orders Completed Within Target ( Count of Orders whose
CompletionDate Deadline)
Number Of Orders Invoiced
Number Of Orders Awaiting Invoice (Status = 5)

I can do the first one, but I can't combine a Count with anything in a
criteria field. Any help would be much appreciated.

Thanks,

Dave


  #6  
Old January 9th, 2007, 10:18 AM posted to microsoft.public.access.queries
David M C
external usenet poster
 
Posts: 59
Default Count in Query

Ken, I've tried your solution but get a syntax error in the two subqueries
between:

(SELECT COUNT *

and

WO2.JobNumber))

I'll try and figure it out.

Dale, yours almost works, but I get OrdersRaised and Invoiced counted twice.
I had this problem previously when including both the WorkOrder and
ResponseInvoice tables.

Regarding JobNumbers etc. Every contract we receive is given a JobNumber. I
have another table that stores the jobs. JobNumber is a foreign key in
WorkOrder, and a foreign key in ResponseInvoice. Every JobNumber can have
many WorkOrders, every JobNumber can have many ResponseInvoices.
ResponseInvoice is not related to WorkOrder because an invoice can be created
as a seperate entity (ie, an invoice with no related WorkOrder). For
accounting purposes, every invoice must have it's associated JobNumber
included (as does every purchase order, timesheet etc etc etc).

Anway, thanks for all your help.

Dave

"Ken Sheridan" wrote:

The invoices seem to reference JobNumber which is a non-key column in the
work orders table, which implies this. It also points to a flaw in the
logical model, I think.

Ken Sheridan
Stafford, England

"Dale Fye" wrote:

It didn't occur to me that there might be more than one invoice for each WO.

--
Email address is not valid.
Please reply to newsgroup only.


"Ken Sheridan" wrote:

Dave:

It should be possible to compute the second and last columns by summing the
return value of an expression which evaluates to 0 or 1, the third by means
of a subquery containing a further subquery. Try this:

SELECT
JobNumber,
COUNT(*) As TotalOrders,
SUM(IIF(CompletionDate Deadline,1,0)) AS WithinTarget,
(SELECT COUNT *
FROM WorkOrder AS WO2
WHERE WO2.JobNumber = WO1.JobNumber
AND EXISTS
(SELECT *
FROM ResponseInvoice
WHERE ResponseInvoice.JobNumber = WO2.JobNumber))
AS Invoiced,
SUM(IIF(Status = 5,1,0)) AS AwaitingInvoice
FROM WorkOrder AS WO1
GROUP BY JobNumber;

Ken Sheridan
Stafford, England

"David M C" wrote:

I have two tables:

WorkOrder:

WorkOrderID (Primary Key)
JobNumber
OrderDate
Deadline
CompletionDate
Status

ResponseInvoice:

ResponseInvoiceID (Primary Key Autonumber)
JobNumber
InvoiceNumber
InvoiceDate

In a query, I need to see (on a per Job Number basis):

Number of Orders Raised (easy, just a Count on the ID column)
Number of Orders Completed Within Target ( Count of Orders whose
CompletionDate Deadline)
Number Of Orders Invoiced
Number Of Orders Awaiting Invoice (Status = 5)

I can do the first one, but I can't combine a Count with anything in a
criteria field. Any help would be much appreciated.

Thanks,

Dave

  #7  
Old January 9th, 2007, 11:07 AM posted to microsoft.public.access.queries
Ken Sheridan
external usenet poster
 
Posts: 3,433
Default Count in Query

Dave:

Mea culpa; the asterisk should be within parentheses in the first subquery:

SELECT COUNT(*)

but not in the second:

SELECT *

Ken Sheridan
Stafford, England

"David M C" wrote:

Ken, I've tried your solution but get a syntax error in the two subqueries
between:

(SELECT COUNT *

and

WO2.JobNumber))

I'll try and figure it out.

Dale, yours almost works, but I get OrdersRaised and Invoiced counted twice.
I had this problem previously when including both the WorkOrder and
ResponseInvoice tables.

Regarding JobNumbers etc. Every contract we receive is given a JobNumber. I
have another table that stores the jobs. JobNumber is a foreign key in
WorkOrder, and a foreign key in ResponseInvoice. Every JobNumber can have
many WorkOrders, every JobNumber can have many ResponseInvoices.
ResponseInvoice is not related to WorkOrder because an invoice can be created
as a seperate entity (ie, an invoice with no related WorkOrder). For
accounting purposes, every invoice must have it's associated JobNumber
included (as does every purchase order, timesheet etc etc etc).

Anway, thanks for all your help.

Dave

"Ken Sheridan" wrote:

The invoices seem to reference JobNumber which is a non-key column in the
work orders table, which implies this. It also points to a flaw in the
logical model, I think.

Ken Sheridan
Stafford, England

"Dale Fye" wrote:

It didn't occur to me that there might be more than one invoice for each WO.

--
Email address is not valid.
Please reply to newsgroup only.


"Ken Sheridan" wrote:

Dave:

It should be possible to compute the second and last columns by summing the
return value of an expression which evaluates to 0 or 1, the third by means
of a subquery containing a further subquery. Try this:

SELECT
JobNumber,
COUNT(*) As TotalOrders,
SUM(IIF(CompletionDate Deadline,1,0)) AS WithinTarget,
(SELECT COUNT *
FROM WorkOrder AS WO2
WHERE WO2.JobNumber = WO1.JobNumber
AND EXISTS
(SELECT *
FROM ResponseInvoice
WHERE ResponseInvoice.JobNumber = WO2.JobNumber))
AS Invoiced,
SUM(IIF(Status = 5,1,0)) AS AwaitingInvoice
FROM WorkOrder AS WO1
GROUP BY JobNumber;

Ken Sheridan
Stafford, England

"David M C" wrote:

I have two tables:

WorkOrder:

WorkOrderID (Primary Key)
JobNumber
OrderDate
Deadline
CompletionDate
Status

ResponseInvoice:

ResponseInvoiceID (Primary Key Autonumber)
JobNumber
InvoiceNumber
InvoiceDate

In a query, I need to see (on a per Job Number basis):

Number of Orders Raised (easy, just a Count on the ID column)
Number of Orders Completed Within Target ( Count of Orders whose
CompletionDate Deadline)
Number Of Orders Invoiced
Number Of Orders Awaiting Invoice (Status = 5)

I can do the first one, but I can't combine a Count with anything in a
criteria field. Any help would be much appreciated.

Thanks,

Dave


  #8  
Old January 9th, 2007, 11:13 AM posted to microsoft.public.access.queries
David M C
external usenet poster
 
Posts: 59
Default Count in Query

Thank you, works perfectly.

"Ken Sheridan" wrote:

Dave:

Mea culpa; the asterisk should be within parentheses in the first subquery:

SELECT COUNT(*)

but not in the second:

SELECT *

Ken Sheridan
Stafford, England

"David M C" wrote:

Ken, I've tried your solution but get a syntax error in the two subqueries
between:

(SELECT COUNT *

and

WO2.JobNumber))

I'll try and figure it out.

Dale, yours almost works, but I get OrdersRaised and Invoiced counted twice.
I had this problem previously when including both the WorkOrder and
ResponseInvoice tables.

Regarding JobNumbers etc. Every contract we receive is given a JobNumber. I
have another table that stores the jobs. JobNumber is a foreign key in
WorkOrder, and a foreign key in ResponseInvoice. Every JobNumber can have
many WorkOrders, every JobNumber can have many ResponseInvoices.
ResponseInvoice is not related to WorkOrder because an invoice can be created
as a seperate entity (ie, an invoice with no related WorkOrder). For
accounting purposes, every invoice must have it's associated JobNumber
included (as does every purchase order, timesheet etc etc etc).

Anway, thanks for all your help.

Dave

"Ken Sheridan" wrote:

The invoices seem to reference JobNumber which is a non-key column in the
work orders table, which implies this. It also points to a flaw in the
logical model, I think.

Ken Sheridan
Stafford, England

"Dale Fye" wrote:

It didn't occur to me that there might be more than one invoice for each WO.

--
Email address is not valid.
Please reply to newsgroup only.


"Ken Sheridan" wrote:

Dave:

It should be possible to compute the second and last columns by summing the
return value of an expression which evaluates to 0 or 1, the third by means
of a subquery containing a further subquery. Try this:

SELECT
JobNumber,
COUNT(*) As TotalOrders,
SUM(IIF(CompletionDate Deadline,1,0)) AS WithinTarget,
(SELECT COUNT *
FROM WorkOrder AS WO2
WHERE WO2.JobNumber = WO1.JobNumber
AND EXISTS
(SELECT *
FROM ResponseInvoice
WHERE ResponseInvoice.JobNumber = WO2.JobNumber))
AS Invoiced,
SUM(IIF(Status = 5,1,0)) AS AwaitingInvoice
FROM WorkOrder AS WO1
GROUP BY JobNumber;

Ken Sheridan
Stafford, England

"David M C" wrote:

I have two tables:

WorkOrder:

WorkOrderID (Primary Key)
JobNumber
OrderDate
Deadline
CompletionDate
Status

ResponseInvoice:

ResponseInvoiceID (Primary Key Autonumber)
JobNumber
InvoiceNumber
InvoiceDate

In a query, I need to see (on a per Job Number basis):

Number of Orders Raised (easy, just a Count on the ID column)
Number of Orders Completed Within Target ( Count of Orders whose
CompletionDate Deadline)
Number Of Orders Invoiced
Number Of Orders Awaiting Invoice (Status = 5)

I can do the first one, but I can't combine a Count with anything in a
criteria field. Any help would be much appreciated.

Thanks,

Dave


 




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT +1. The time now is 06:50 AM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 OfficeFrustration.
The comments are property of their posters.