473,788 Members | 2,814 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.U pdateDate) AS MaxOfUpdateDate , tblUpdate.Actio nID
FROM tblUpdate
GROUP BY tblUpdate.Actio nID;
this gives me :

"MaxOfUpdateDat e","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.U pdateDate) AS MaxOfUpdateDate , tblUpdate.Updat eID,
tblUpdate.Actio nID
FROM tblUpdate
GROUP BY tblUpdate.Updat eID, tblUpdate.Actio nID;

i now get this

"MaxOfUpdateDat e","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 :

"MaxOfUpdateDat e","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 1537
<jim> wrote in message news:11******** *****@corp.supe rnews.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.U pdateDate) AS MaxOfUpdateDate , tblUpdate.Actio nID
FROM tblUpdate
GROUP BY tblUpdate.Actio nID;
this gives me :

"MaxOfUpdateDat e","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.U pdateDate) AS MaxOfUpdateDate , tblUpdate.Updat eID,
tblUpdate.Actio nID
FROM tblUpdate
GROUP BY tblUpdate.Updat eID, tblUpdate.Actio nID;

i now get this

"MaxOfUpdateDat e","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 :

"MaxOfUpdateDat e","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.U pdateDate) AS MaxOfUpdateDate ,
tblUpdate.Actio nID FROM tblUpdate
GROUP BY tblUpdate.Actio nID]. 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","A ctionID","Updat eID"
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.U pdateDate) AS MaxOfUpdateDate ,
tblUpdate.Actio nID FROM tblUpdate
GROUP BY tblUpdate.Actio nID]. 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","A ctionID","Updat eID"
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.U pdateDate) AS MaxOfUpdateDate ,
tblUpdate.Actio nID FROM tblUpdate
GROUP BY tblUpdate.Actio nID]. 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","A ctionID","Updat eID"
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.U pdateDate) AS MaxOfUpdateDate ,
tblUpdate.Actio nID FROM tblUpdate
GROUP BY tblUpdate.Actio nID]. 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******@pacbe ll.net.invalid> wrote in message
news:43******** ********@news.I NDIVIDUAL.NET.. .
On Wed, 28 Sep 2005 09:22:24 +0100, <jim> wrote:
Try this:

SELECT a.* FROM tblUpdate a INNER JOIN
[SELECT Max(tblUpdate.U pdateDate) AS MaxOfUpdateDate ,
tblUpdate.Actio nID FROM tblUpdate
GROUP BY tblUpdate.Actio nID]. 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","A ctionID","Updat eID"
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.U pdateDate) AS MaxOfUpdateDate ,
tblUpdate.Actio nID FROM tblUpdate
GROUP BY tblUpdate.Actio nID]. 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.supe rnews.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("Update ID","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("Update ID","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
12693
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 it in Enterprise Manager. That said, I can not see the users associated with the group from Enterprise Manager, but know they can login to the database. The problem is this. When we login via the web we get access to the database without...
2
3091
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 clause eg SELECT DISTINCT tblPageViews.PageVisited, Count(tblPageViews.PageVisited) AS CountOfPageVisited FROM tblPageViews GROUP BY tblPageViews.PageVisited;
0
1162
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 it in Enterprise Manager. That said, I can not see the users associated with the group from Enterprise Manager, but know they can login to the database. The problem is this. When we login via the web we get access to the database without...
3
2195
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 then total the 'TOTAL' column for that group. example. new group total = 3 After this group the next grouping would display info for 'SPEC' = A1 and INTMAN = BETWEEN 2 AND 6 with the new group total = 19
1
1853
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 queries. However, when I change that individual user's permissions to allow access to the queries it goes through fine and he can access it. Don't understand what's going on here and certainly don't want to have to assign indiv permission to...
18
1629
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 "usenet". What is your preferred method of accessing this group? Regards, Barry Wright
1
1701
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 _code columns, among many others; the JOB_TITLE table contains job_title_code column, among many others.
14
2588
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 200 W. Monroe St. Suite 900 Chicago, IL 60606 (312) 222-9400 Location: www.imginc.com/IMG/About+IMG/chicago.htm
1
3618
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 "interface" in Access. It's like a little system to make a production plan. It must get information about part numbers, description, set where the part number belongs, quantity of part numbers that the set uses, stock number(this is a text value, it...
1
3235
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. Any ideas how I could do that? Here is part of my code: If qda.EOF = False And qda.BOF = False Then qda.MoveFirst Do While qda.EOF = False
0
9498
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
10366
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10110
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
8993
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
7517
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...
0
5399
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5536
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4070
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
3
2894
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.