473,404 Members | 2,137 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,404 software developers and data experts.

GROUP BY problem in access

hi,

i have a table( tblUpdate) with the following fields

UpdateID, UpdateDate, ActionID

UpdateID is the primary key
ActionID is a foreign key

i want to find the latest (by date) update of a particluar action.

and i can do this using the following:

SELECT Max(tblUpdate.UpdateDate) AS MaxOfUpdateDate, tblUpdate.ActionID
FROM tblUpdate
GROUP BY tblUpdate.ActionID;
this gives me :

"MaxOfUpdateDate","ActionID"
24/9/2005 ,24
26/8/2005 ,25
11/9/2005 ,26
26/9/2005 ,28

which is good but i also need the primary key, UpdateID

If i add updateID into the query i get

SELECT Max(tblUpdate.UpdateDate) AS MaxOfUpdateDate, tblUpdate.UpdateID,
tblUpdate.ActionID
FROM tblUpdate
GROUP BY tblUpdate.UpdateID, tblUpdate.ActionID;

i now get this

"MaxOfUpdateDate","ActionID","UpdateID"
24/9/2005 ,24,23
22/9/2005 ,24,24
26/8/2005 ,25,25
26/9/2004 ,25,26
11/9/2005 ,26,27
26/9/2005 ,28,28

which is no good because im getting results for all the updateids

i really just want :

"MaxOfUpdateDate","ActionID"
24/9/2005 ,24
26/8/2005 ,25
11/9/2005 ,26
26/9/2005 ,28

but with the primary key for each one.

any help would be much appreciated
thanks in advance

jim

Nov 13 '05 #1
8 1513
<jim> wrote in message news:11*************@corp.supernews.com...
hi,

i have a table( tblUpdate) with the following fields

UpdateID, UpdateDate, ActionID

UpdateID is the primary key
ActionID is a foreign key

i want to find the latest (by date) update of a particluar action.

and i can do this using the following:

SELECT Max(tblUpdate.UpdateDate) AS MaxOfUpdateDate, tblUpdate.ActionID
FROM tblUpdate
GROUP BY tblUpdate.ActionID;
this gives me :

"MaxOfUpdateDate","ActionID"
24/9/2005 ,24
26/8/2005 ,25
11/9/2005 ,26
26/9/2005 ,28

which is good but i also need the primary key, UpdateID

If i add updateID into the query i get

SELECT Max(tblUpdate.UpdateDate) AS MaxOfUpdateDate, tblUpdate.UpdateID,
tblUpdate.ActionID
FROM tblUpdate
GROUP BY tblUpdate.UpdateID, tblUpdate.ActionID;

i now get this

"MaxOfUpdateDate","ActionID","UpdateID"
24/9/2005 ,24,23
22/9/2005 ,24,24
26/8/2005 ,25,25
26/9/2004 ,25,26
11/9/2005 ,26,27
26/9/2005 ,28,28

which is no good because im getting results for all the updateids

i really just want :

"MaxOfUpdateDate","ActionID"
24/9/2005 ,24
26/8/2005 ,25
11/9/2005 ,26
26/9/2005 ,28

but with the primary key for each one.

any help would be much appreciated
thanks in advance

jim


Create a 2nd query with two inputs, One will be your existing query and the
2nd will be tblUpdate. Link the two inputs by UpdateID. Put all three fields
in the output. If this doesn't make sense, then respond back and I'll write
the full SQL stmt.
Fred Zuckerman

Nov 13 '05 #2
Create a 2nd query with two inputs, One will be your existing query and
the
2nd will be tblUpdate. Link the two inputs by UpdateID. Put all three
fields
in the output. If this doesn't make sense, then respond back and I'll
write
the full SQL stmt.
Fred Zuckerman

many thanks for your reply fred

but how can i link the two inputs by UpdateID when i dont have UpdateID in
the the existing query....

Nov 13 '05 #3
On Tue, 27 Sep 2005 21:24:51 +0100, <jim> wrote:
Create a 2nd query with two inputs, One will be your existing query and
the
2nd will be tblUpdate. Link the two inputs by UpdateID. Put all three
fields
in the output. If this doesn't make sense, then respond back and I'll
write
the full SQL stmt.
Fred Zuckerman

many thanks for your reply fred

but how can i link the two inputs by UpdateID when i dont have UpdateID in
the the existing query....


Try this:

SELECT a.* FROM tblUpdate a INNER JOIN
[SELECT Max(tblUpdate.UpdateDate) AS MaxOfUpdateDate,
tblUpdate.ActionID FROM tblUpdate
GROUP BY tblUpdate.ActionID]. as c
ON a.ActionID & UpdateDate = c.ActionID & MaxOfUpdateDate

Note that this is not a perfect solution. If your table also contains
the following:

"UpdateDate","ActionID","UpdateID"
22/9/2005 ,2,29
22/9/2005 ,2,30

You are going to end up with referencing only one of these and I
think, although I'm not sure, that it is random as to whether the 29
or the 30 comes out.

To rectify this, you can make the combination of ActionID & UpdateDate
a unique key. If you do that, then the query "works", I think.

mike

Nov 13 '05 #4
> Try this:

SELECT a.* FROM tblUpdate a INNER JOIN
[SELECT Max(tblUpdate.UpdateDate) AS MaxOfUpdateDate,
tblUpdate.ActionID FROM tblUpdate
GROUP BY tblUpdate.ActionID]. as c
ON a.ActionID & UpdateDate = c.ActionID & MaxOfUpdateDate

Note that this is not a perfect solution. If your table also contains
the following:

"UpdateDate","ActionID","UpdateID"
22/9/2005 ,2,29
22/9/2005 ,2,30

You are going to end up with referencing only one of these and I
think, although I'm not sure, that it is random as to whether the 29
or the 30 comes out.

To rectify this, you can make the combination of ActionID & UpdateDate
a unique key. If you do that, then the query "works", I think.

mike

Mike, you are a genius thanks.

I will alter the UpdateDate field to include the time so ActionID &
UpdateDate can be a unique key and more than one update can be made on the
same day.If you don't mind could you talk me through your SQL statement, in
particular could you explain the a. & c.

Thanks again

jim
Nov 13 '05 #5
On Wed, 28 Sep 2005 09:22:24 +0100, <jim> wrote:
Try this:

SELECT a.* FROM tblUpdate a INNER JOIN
[SELECT Max(tblUpdate.UpdateDate) AS MaxOfUpdateDate,
tblUpdate.ActionID FROM tblUpdate
GROUP BY tblUpdate.ActionID]. as c
ON a.ActionID & UpdateDate = c.ActionID & MaxOfUpdateDate

Note that this is not a perfect solution. If your table also contains
the following:

"UpdateDate","ActionID","UpdateID"
22/9/2005 ,2,29
22/9/2005 ,2,30

You are going to end up with referencing only one of these and I
think, although I'm not sure, that it is random as to whether the 29
or the 30 comes out.

To rectify this, you can make the combination of ActionID & UpdateDate
a unique key. If you do that, then the query "works", I think.

mike

Mike, you are a genius thanks.

I will alter the UpdateDate field to include the time so ActionID &
UpdateDate can be a unique key and more than one update can be made on the
same day.If you don't mind could you talk me through your SQL statement, in
particular could you explain the a. & c.


Describing SQL in english is laborious. Below is a typically futile
attempt. Hope it gives some insight.

I am not particularly skilled at SQL. If *I* can suss out the correct
syntax, it can't be a terribly complicated bit of logic.

Fred gave an answer that was close:

1. Create a 2nd query with two inputs
1a One will be your existing query
1b and the 2nd will be tblUpdate.

The SQL statement I wrote is this "2nd query". It, as Fred indicated,
has two inputs. The way I wrote the query, I reversed the order that
Fred mentioned. Instead of the first one being your SQL statement and
the second one being tblUpdate, the SQL statement I wrote has
tblUpdate as the first input and your SQL statement for the second.

Here is the SQL I wrote:

SELECT a.* FROM tblUpdate a INNER JOIN
[SELECT Max(tblUpdate.UpdateDate) AS MaxOfUpdateDate,
tblUpdate.ActionID FROM tblUpdate
GROUP BY tblUpdate.ActionID]. as c
ON a.ActionID & UpdateDate = c.ActionID & MaxOfUpdateDate

2. Link the two inputs by UpdateID.

I linked the two inputs on both ActionID and UpdateDate, rather
than on UpdateID.

3. Put all three fields in the output.

Note that the initial selection is a.*, which takes all the
fields of table a. "a" is the abbreviation for tblUpdate (which is
specified just before the word INNER). "c" is the abbreviation for
the result of your query (which is specified by the phrase "as c" just
after your SQL statement. Access ignores the word "as". So I could
just as easily followed your SQL statement with just the letter "c";
or I could just as easily have used "as" before "a" just before the
word INNER. Either syntax is ok.

Here is the English equivalent to the SQL I wrote:

Select all of the fields from tblUpdate from the resulting rows that
you get by matching tblUpdate against a subset of itself. That is,
you want to take only those things that show up on both sides of the
comparison you are about to perform (this is an INNER JOIN). On the
left side of this comparison you will use the rows from tblUpdate. On
the right side of this comparison you will use the rows that result
from your SQL statement. The comparison is that the left side's
ActionID and UpdateDate fields must be equal to the right side's
ActionID and UpdateDate fields. Of course, the right hand side
doesn't have an UpdateDate field. Instead, it has the results from
your aggregate function (Max). That field is names MaxOfUpdateDate.

At its lowest level, where you combine everything you can, the query
is: select all the fields in tblupdate that result from joining
tblupdate on the left side against a subset of itself on the right
side. Let's call the left side "a", rather than tblUpdate. Let's
call the right side "c" rather than "the result we get from your
original SQL statement". I could just as easily used "a" and "b"
rather than "a" and "c". Or "tblUpdate_1" and "tblUpdate_2". It is
just an alias, which I choose to use more for its ability to be used
as an abbreviation.

Note that I haven't explained your original SQL statement, because
that is what you originally developed.

Hope that helps a bit.

mike

Nov 13 '05 #6
Thats brilliant mike, many thanks for your time and effort, it is much
appreciated!

cheers

jim
"Mike Preston" <mb******@pacbell.net.invalid> wrote in message
news:43****************@news.INDIVIDUAL.NET...
On Wed, 28 Sep 2005 09:22:24 +0100, <jim> wrote:
Try this:

SELECT a.* FROM tblUpdate a INNER JOIN
[SELECT Max(tblUpdate.UpdateDate) AS MaxOfUpdateDate,
tblUpdate.ActionID FROM tblUpdate
GROUP BY tblUpdate.ActionID]. as c
ON a.ActionID & UpdateDate = c.ActionID & MaxOfUpdateDate

Note that this is not a perfect solution. If your table also contains
the following:

"UpdateDate","ActionID","UpdateID"
22/9/2005 ,2,29
22/9/2005 ,2,30

You are going to end up with referencing only one of these and I
think, although I'm not sure, that it is random as to whether the 29
or the 30 comes out.

To rectify this, you can make the combination of ActionID & UpdateDate
a unique key. If you do that, then the query "works", I think.

mike

Mike, you are a genius thanks.

I will alter the UpdateDate field to include the time so ActionID &
UpdateDate can be a unique key and more than one update can be made on the
same day.If you don't mind could you talk me through your SQL statement,
in
particular could you explain the a. & c.


Describing SQL in english is laborious. Below is a typically futile
attempt. Hope it gives some insight.

I am not particularly skilled at SQL. If *I* can suss out the correct
syntax, it can't be a terribly complicated bit of logic.

Fred gave an answer that was close:

1. Create a 2nd query with two inputs
1a One will be your existing query
1b and the 2nd will be tblUpdate.

The SQL statement I wrote is this "2nd query". It, as Fred indicated,
has two inputs. The way I wrote the query, I reversed the order that
Fred mentioned. Instead of the first one being your SQL statement and
the second one being tblUpdate, the SQL statement I wrote has
tblUpdate as the first input and your SQL statement for the second.

Here is the SQL I wrote:

SELECT a.* FROM tblUpdate a INNER JOIN
[SELECT Max(tblUpdate.UpdateDate) AS MaxOfUpdateDate,
tblUpdate.ActionID FROM tblUpdate
GROUP BY tblUpdate.ActionID]. as c
ON a.ActionID & UpdateDate = c.ActionID & MaxOfUpdateDate

2. Link the two inputs by UpdateID.

I linked the two inputs on both ActionID and UpdateDate, rather
than on UpdateID.

3. Put all three fields in the output.

Note that the initial selection is a.*, which takes all the
fields of table a. "a" is the abbreviation for tblUpdate (which is
specified just before the word INNER). "c" is the abbreviation for
the result of your query (which is specified by the phrase "as c" just
after your SQL statement. Access ignores the word "as". So I could
just as easily followed your SQL statement with just the letter "c";
or I could just as easily have used "as" before "a" just before the
word INNER. Either syntax is ok.

Here is the English equivalent to the SQL I wrote:

Select all of the fields from tblUpdate from the resulting rows that
you get by matching tblUpdate against a subset of itself. That is,
you want to take only those things that show up on both sides of the
comparison you are about to perform (this is an INNER JOIN). On the
left side of this comparison you will use the rows from tblUpdate. On
the right side of this comparison you will use the rows that result
from your SQL statement. The comparison is that the left side's
ActionID and UpdateDate fields must be equal to the right side's
ActionID and UpdateDate fields. Of course, the right hand side
doesn't have an UpdateDate field. Instead, it has the results from
your aggregate function (Max). That field is names MaxOfUpdateDate.

At its lowest level, where you combine everything you can, the query
is: select all the fields in tblupdate that result from joining
tblupdate on the left side against a subset of itself on the right
side. Let's call the left side "a", rather than tblUpdate. Let's
call the right side "c" rather than "the result we get from your
original SQL statement". I could just as easily used "a" and "b"
rather than "a" and "c". Or "tblUpdate_1" and "tblUpdate_2". It is
just an alias, which I choose to use more for its ability to be used
as an abbreviation.

Note that I haven't explained your original SQL statement, because
that is what you originally developed.

Hope that helps a bit.

mike

Nov 13 '05 #7
<jim> wrote in message news:11*************@corp.supernews.com...
Create a 2nd query with two inputs, One will be your existing query and
the
2nd will be tblUpdate. Link the two inputs by UpdateID. Put all three
fields
in the output. If this doesn't make sense, then respond back and I'll
write
the full SQL stmt.
Fred Zuckerman
many thanks for your reply fred

but how can i link the two inputs by UpdateID when i dont have UpdateID

in the the existing query....

I'm sorry, you are correct. You would link on UpdateDate & ActionID.
Since that may not not a key field, there might be duplicate UpdateID, which
will yield multiple records in the 2nd query.

If this is not what you want and are willing to accept any of the matching
UpdateID, then change your 2nd query to include the 1st query alone, and a
column defined by:

(caution for line wrap)
DLookup("UpdateID","tblUpdate","ActionID=" & [ActionID] & " And
UpdateDate=#" & [UpdateDate] & "#")
(caution for line wrap)

If ActionID is text then you'll need some extra (') symbols.

Good Luck
Fred
Nov 13 '05 #8
> I'm sorry, you are correct. You would link on UpdateDate & ActionID.
Since that may not not a key field, there might be duplicate UpdateID,
which
will yield multiple records in the 2nd query.

If this is not what you want and are willing to accept any of the matching
UpdateID, then change your 2nd query to include the 1st query alone, and a
column defined by:

(caution for line wrap)
DLookup("UpdateID","tblUpdate","ActionID=" & [ActionID] & " And
UpdateDate=#" & [UpdateDate] & "#")
(caution for line wrap)

If ActionID is text then you'll need some extra (') symbols.

Good Luck
Fred


Many thanks for replying - i think i actually get it now... thanks again

jim
Nov 13 '05 #9

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

Similar topics

2
by: Tom Loach | last post by:
Our system administrator set up an NT server group in order to allow our users to login to our application via https to our sql server. The group appears as a User in SQL Server when you look at...
2
by: Paxton | last post by:
Hi, I'm trying to display the total page views per page within a given date range, but the correct SQL is seemingly beyond me. I get the correct result with a straightforward Group By and Count...
0
by: Tom Loach | last post by:
Our system administrator set up an NT server group in order to allow our users to login to our application via https to our sql server. The group appears as a User in SQL Server when you look at...
3
by: CitizenPayne | last post by:
I have a problem with an access 97 query I have included the current query output. for every 'SPEC' there are 6 possible values for 'INTMAN' ( 1 to 6). I need to group by 'SPEC' where INTMAN =...
1
by: Angelo | last post by:
I have a user (john.doe) as a member of a group (MyGroup). I have assigned group permission to view and run the queries of that db but then when I log in with that user, I still can't access the...
18
by: Barry Wright | last post by:
Hi All, I have been connecting to this group through Rogers cable my internet service provider. However, Rogers has announced that effective Dec 15th they will no longer providing access to...
1
by: jim | last post by:
Hi, I was wondering if anyone out there can help me with this SQL problem: I have a database that has two tables: EMPLOYEE and JOB_TITLE The EMPLOYEE Table consists of a salary and job_title...
14
by: Kevin G. Anderson | last post by:
What: CAUG Meeting - QuickBooks IIF Files; Total Access Analyzer; CAUG Social When: Thursday, May 25, 2006, 6PM Who: Chris Monaghan and Kevin Anderson Where: The Information Management Group...
1
by: zafm86 | last post by:
Hi everyone! I'm sure my problem is pretty easy to solve but I've been working on it for a long and my my brain is not working correctly anymore. I'm working with an AS400 and I mhave to do an...
1
by: seanhirshberg | last post by:
I have code that current sends spreadsheets by email from a user's Outlook. I need to modify the code to send the email from their group email box, so the recipient can respond to the group email....
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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
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...
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...
0
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...

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.