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

Subquery - how to count in access

Assume the following table holding information about the planning date
and execution date for an imaginary "objects"

ObjectPlan

ObjectID PlannedDate ExecutedDate
1 19.03.04 28.03.04
2 19.03.04 24.03.04
3 19.03.04 19.04.04
1 19.04.04 20.04.04

Now, I want to display a count of the number of planned executions vs.
the real executions - grouped by monthly. So the resulting table would
be:

Month CountOfPlanned CountOfExecuted
03 3 2
04 1 2

How can this be achieved in Access? I tried the following:

Select Format(a.PlannedDate,"mm") As Month, Count(a.ObjectID) As
CountOfPlanned, _
(SELECT count(b.ObjectID)
FROM ObjectPlan As b
WHERE Format(b.ExecutedDate,"mm") = Format(a.PlannedDate,"mm")
) As CountOfExecuted
FROM ObjectPlan As a
GROUP BY Format(a.PlannedDate,"mm")

But this doesn't seem to do the trick. I could write two queries
counting separately then joining using union but there must be an
easier way ??

cheers,

Dag
Nov 12 '05 #1
3 5003
Dagpauk wrote:
Assume the following table holding information about the planning date
and execution date for an imaginary "objects"

ObjectPlan

ObjectID PlannedDate ExecutedDate
1 19.03.04 28.03.04
2 19.03.04 24.03.04
3 19.03.04 19.04.04
1 19.04.04 20.04.04

Now, I want to display a count of the number of planned executions vs.
the real executions - grouped by monthly. So the resulting table would
be:

Month CountOfPlanned CountOfExecuted
03 3 2
04 1 2

How can this be achieved in Access? I tried the following:

Select Format(a.PlannedDate,"mm") As Month, Count(a.ObjectID) As
CountOfPlanned, _
(SELECT count(b.ObjectID)
FROM ObjectPlan As b
WHERE Format(b.ExecutedDate,"mm") = Format(a.PlannedDate,"mm")
) As CountOfExecuted
FROM ObjectPlan As a
GROUP BY Format(a.PlannedDate,"mm")

But this doesn't seem to do the trick. I could write two queries
counting separately then joining using union but there must be an
easier way ??

cheers,

Dag


Not elegant but:

SELECT Format([PlannedDate],"mm/yy") AS [Month],
DCount("*","objectplan","month(planneddate)=" & Month([plannedDate]) & "
and year(plannedDate)=" & Year([plannedDate])) AS CountOfPlanned,
DCount("*","objectplan","month(Executedate)=" & Month([ExecuteDate]) & "
and year(ExecuteDate)=" & Year([ExecuteDate])) AS CountOfExecuted
FROM ObjectPlan
GROUP BY Format([PlannedDate],"mm/yy"),
DCount("*","objectplan","month(planneddate)=" & Month([plannedDate]) & "
and year(plannedDate)=" & Year([plannedDate])),
DCount("*","objectplan","month(Executedate)=" & Month([ExecuteDate]) & "
and year(ExecuteDate)=" & Year([ExecuteDate]));

HTH
--
But why is the Rum gone?
Nov 12 '05 #2
You might easily get a similar result - but not exactly what you are looking
for with this:
SELECT Count(Test.ObjectID) AS CountOfObjectID, Month([Test]![PlannedDate])
AS PMonth, Month([Test]![ExecutedDate]) AS MonthEx
FROM Test
GROUP BY Month([Test]![PlannedDate]), Month([Test]![ExecutedDate]);

It will look like this:
Count PMonth EMonth
2 3 3
1 3 4
1 4 4

Telling you: 2 object where both planned and executed in moth 3. 1 object
planned in moth 3 and executed in moth 4, and 1 object planned and executed
in moth 4. Not exactly what you wanted, but done only in one simple query.

Mvh
Rolfern

"Dagpauk" <da*****@yahoo.co.uk> wrote in message
news:54**************************@posting.google.c om...
Assume the following table holding information about the planning date
and execution date for an imaginary "objects"

ObjectPlan

ObjectID PlannedDate ExecutedDate
1 19.03.04 28.03.04
2 19.03.04 24.03.04
3 19.03.04 19.04.04
1 19.04.04 20.04.04

Now, I want to display a count of the number of planned executions vs.
the real executions - grouped by monthly. So the resulting table would
be:

Month CountOfPlanned CountOfExecuted
03 3 2
04 1 2

How can this be achieved in Access? I tried the following:

Select Format(a.PlannedDate,"mm") As Month, Count(a.ObjectID) As
CountOfPlanned, _
(SELECT count(b.ObjectID)
FROM ObjectPlan As b
WHERE Format(b.ExecutedDate,"mm") = Format(a.PlannedDate,"mm")
) As CountOfExecuted
FROM ObjectPlan As a
GROUP BY Format(a.PlannedDate,"mm")

But this doesn't seem to do the trick. I could write two queries
counting separately then joining using union but there must be an
easier way ??

cheers,

Dag

Nov 12 '05 #3
Thanks for the ideas guys. In the end I decided to go for a union
query like this:

select month(PlannedDate) As M, Count(PlannedDate) As Planned, 0 As
Executed
from ObjectTable
group by month(PlannedDate)
union
select month(ExecutedDate) As M, 0 As Planned, Count(ExecutedDate) As
Executed
from ObjectTable
group by month(ExecutedDate)
Then I created a final query summing it all up:

select M, sum(Planned), sum(Executed)
from ObjectTable
group by M

I did this to include all cases - for example if an object was
Executed in a month in which no other objects were planned:

Month Planned Executed
06 0 1

The union query shouldn't be too costly as it groups by month creating
at the most 12x12 = 144 records (???)

I guess a cross-tab query could have easily solved this problem but I
would the solution to be as "pure" as possible in case the application
is to be used with Oracle/SQL-Server.

cheers,

Dag

"R.A.Balder" <R_********@xhotmail.com> wrote in message news:<IA*********************@juliett.dax.net>...
You might easily get a similar result - but not exactly what you are looking
for with this:
SELECT Count(Test.ObjectID) AS CountOfObjectID, Month([Test]![PlannedDate])
AS PMonth, Month([Test]![ExecutedDate]) AS MonthEx
FROM Test
GROUP BY Month([Test]![PlannedDate]), Month([Test]![ExecutedDate]);

It will look like this:
Count PMonth EMonth
2 3 3
1 3 4
1 4 4

Telling you: 2 object where both planned and executed in moth 3. 1 object
planned in moth 3 and executed in moth 4, and 1 object planned and executed
in moth 4. Not exactly what you wanted, but done only in one simple query.

Mvh
Rolfern

"Dagpauk" <da*****@yahoo.co.uk> wrote in message
news:54**************************@posting.google.c om...
Assume the following table holding information about the planning date
and execution date for an imaginary "objects"

ObjectPlan

ObjectID PlannedDate ExecutedDate
1 19.03.04 28.03.04
2 19.03.04 24.03.04
3 19.03.04 19.04.04
1 19.04.04 20.04.04

Now, I want to display a count of the number of planned executions vs.
the real executions - grouped by monthly. So the resulting table would
be:

Month CountOfPlanned CountOfExecuted
03 3 2
04 1 2

How can this be achieved in Access? I tried the following:

Select Format(a.PlannedDate,"mm") As Month, Count(a.ObjectID) As
CountOfPlanned, _
(SELECT count(b.ObjectID)
FROM ObjectPlan As b
WHERE Format(b.ExecutedDate,"mm") = Format(a.PlannedDate,"mm")
) As CountOfExecuted
FROM ObjectPlan As a
GROUP BY Format(a.PlannedDate,"mm")

But this doesn't seem to do the trick. I could write two queries
counting separately then joining using union but there must be an
easier way ??

cheers,

Dag

Nov 12 '05 #4

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

Similar topics

5
by: Haisoo Shin | last post by:
Hello.. I am working on a PHP/PEAR web page that shows statistics of how many people read a certain article during given period of time. I have, say, two tables called 'books' and 'logs'. The...
3
by: Nachi | last post by:
I am getting 2 resultsets depending on conditon, In the second conditon i am getting the above error could anyone help me.......... CREATE proc sp_count_AllNewsPapers @CustomerId int as...
2
by: Greg Stark | last post by:
I find I often want to be able to do joins against views where the view are aggregates on a column that has an index. Ie, something like SELECT a.*, v.n FROM a JOIN (select a_id,count(*) as n...
0
by: Greg Stark | last post by:
Postgresql 7.4b2 (approximately, compiled out of CVS) When I have a subquery that has a complex subquery as one of the result columns, and then that result column is used multiple times in the...
4
by: Kenny G | last post by:
Below is a query that I currently have. I need to produce a subquery so that the top five of the CodeCount is returned. I appreciate your help. SELECT .PX_SURGEON, .PX_CODE, Count(.PX_CODE)...
2
by: Edwin Pauli | last post by:
Hi, Yesterday i had upgrade my PostgreSQL server from version 7.2.4 to 7.4.1. There are troubles with a subquery after the upgrade. Here is the query: SELECT team_naam, team_id, wpim, (
3
by: laurenq uantrell | last post by:
I'm trying to return an integer from the following table that returns the number of unique cities: tblEmployees Name City John Boston Frank New York Jim Omaha Betty ...
0
by: JT | last post by:
I'm using access 2000 and with assistance thought I had this problem licked. This query works: SELECT Q3.id, Q3.date, Max(Q3.Rank) AS Ninetieth FROM query3 AS Q3 WHERE...
2
by: LordGoran | last post by:
I'm stumped with this one. I'm trying to recode an SQL statement written in Access that will work for MySQL (4.1.22). In Access it used a stored Query, I'd like to use a subquery to replace the...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
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.