Wrox Programmer Forums
|
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 28th, 2008, 05:35 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default TRY THIS CODE

Okay...first of all, create a STORED QUERY in Access that looks like this:
Code:
SELECT a, b, c, sum(d) AS total, max(h) AS maxDate
FROM Table1
GROUP BY a, b, c
You'll recognize this as almost the same query you said worked for you, before.

But this time use Access to create the query and then give it a name you will remember. I named it
SumsWithMaxDate

And *NOW* you can use *THIS* query to get the results you are after, I think:
Code:
SELECT SumsWithMaxDate.a, SumsWithMaxDate.b, SumsWithMaxDate.c, SumsWithMaxDate.total, Table1.f, Table1.g
FROM Table1, SumsWithMaxDate
WHERE Table1.h = SumsWithMaxDate.maxDate 
AND Table1.c = SumsWithMaxDate.c
AND Table1.b = SumsWithMaxDate.b
AND Table1.a = SumsWithMaxDate.a
See if that works for you.
 
Old December 28th, 2008, 05:39 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default THIS ALSO WORKED...

I thought this query might be too complex for poor little Access, but it *did* work all in one query, after all. I got the identical results as prior post by doing this all in one:
Code:
SELECT SumsWithMaxDate.a, SumsWithMaxDate.b, SumsWithMaxDate.c, SumsWithMaxDate.total, Table1.f, Table1.g
FROM Table1, ( 
    SELECT a, b, c, sum(d) AS total, max(h) AS maxDate
    FROM Table1
    GROUP BY a, b, c
    ) AS  SumsWithMaxDate
WHERE Table1.h = SumsWithMaxDate.maxDate 
AND Table1.c = SumsWithMaxDate.c
AND Table1.b = SumsWithMaxDate.b
AND Table1.a = SumsWithMaxDate.a
Naturally, you can add a final ORDER BY clause to that (don't need one for the inner SELECT...would be useless).
 
Old December 31st, 2008, 05:22 AM
Authorized User
 
Join Date: Oct 2008
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I tried this:
Quote:
SELECT SumsWithMaxDate.a, SumsWithMaxDate.b, SumsWithMaxDate.c, SumsWithMaxDate.total, Table1.f, Table1.g
FROM Table1, SumsWithMaxDate
WHERE Table1.h = SumsWithMaxDate.maxDate
AND Table1.c = SumsWithMaxDate.c
AND Table1.b = SumsWithMaxDate.b
AND Table1.a = SumsWithMaxDate.a
The Microsoft Jet database engine cannot find the input table or query 'SumsWithMaxDate'. Make sure it exists and that its name is spelled correctly.
but it showed me this error:
The Microsoft Jet database engine cannot find the input table or query 'SumsWithMaxDate'. Make sure it exists and that its name is spelled correctly.

Then I used this
Quote:
SELECT SumsWithMaxDate.a, SumsWithMaxDate.b, SumsWithMaxDate.c, SumsWithMaxDate.total, Table1.f, Table1.g
FROM Table1, (
SELECT a, b, c, sum(d) AS total, max(h) AS maxDate
FROM Table1
GROUP BY a, b, c
) AS SumsWithMaxDate
WHERE Table1.h = SumsWithMaxDate.maxDate
AND Table1.c = SumsWithMaxDate.c
AND Table1.b = SumsWithMaxDate.b
AND Table1.a = SumsWithMaxDate.a
abd it showed me this error:
No value given for one or more required parameters.

By the way the select or chosing as condition by Max(H) is only for F and G columns.

Last edited by Mangore; December 31st, 2008 at 05:29 AM..
 
Old December 31st, 2008, 07:44 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

I really *DID* creaqte that table in an Access DB on my machine and I really *DID* run those queries. So I have no idea why they didn't work for you.

Did you try them *IN ACCESS*??? WITHOUT ASP/ADO/VBS getting in the way???

And for the first case, *did* you create the STORED QUERY as I noted? I have to assume that you didn't or you couldn't have gotten that error.

> By the way the select or chosing as condition by Max(H) is only for F and G columns.

Yes, I know. You want to see my actual table and actual results???
 
Old January 1st, 2009, 08:52 AM
Authorized User
 
Join Date: Oct 2008
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I'm so sorry I was confusing. I didn't creat TAble called SumsWithMaxDate
do you mean I have to Creat table called SumsWithMaxDate and creat in it these columns a,b,c,d,f,g,h...
 
Old January 1st, 2009, 11:09 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Not a TABLE. A QUERY.

You start up Access.
You open up your ".mdb" file.
In the main interior panel/window, click on QUERIES.
In the QUERIES window, click on CREATE QUERY IN DESIGN VIEW.
When the SHOW TABLE popup appears, just close it.
Click on the VIEW menu (top of the main window) and then on the SQL VIEW menu item.

Now type in the SQL query I gave you into that text window.

Now click the close box (the X at the top right) of that SQL view text window.

Say "YES" to "Do you want to save..."

When asked for the name of the query, use the name I gave you (or one of your choice).

Presto. You have created a STORED QUERY in Access.

For the most part, you can treat a STORED QUERY just the same as a table!!!

TRY IT!





Similar Threads
Thread Thread Starter Forum Replies Last Post
SQL problem MArk_dB BOOK: Beginning ASP 3.0 1 December 16th, 2007 07:33 AM
SQL Problem Brendan Bartley Access 1 November 9th, 2007 01:33 PM
sql problem shrisangeeta Classic ASP Databases 2 May 24th, 2006 09:58 AM
SQL problem Clive Astley Access 4 October 19th, 2004 01:49 AM
SQL Problem tjw Access 1 November 12th, 2003 09:22 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.