Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / General 2 / May 2008

Tip: Looking for answers? Try searching our database.

Sub Query - Easy How to ??

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
WANNABE - 29 May 2008 01:23 GMT
I'm sure this is an easy question for someone out there, and I used to be able
to do this..
I have 2 queries that produce the results I need, but I would like to remember
how to do it with a sub-query.
My 2 queries are below, and the results are a list of distinct records.  The
newest of the last 2 years, one record per country.
I would greatly appreciate any assistance, Thank you very much.

SELECT [Country Profile].COUNTRY, [Country Profile].Period, [Country
Profile].LQR, [Country Profile].[MLT DEBT], [Country Profile].[MST DEBT],
[Country Profile].[SPLT DEBT], [Country Profile].[FLT DEBT], [Country
Profile].[Country Limit]
FROM [Country Profile], [qq_CountryLimits_Last2Yrs t2]
WHERE ((([Country Profile].COUNTRY)=[qq_CountryLimits_Last2Yrs t2].[country])
AND (([Country Profile].Period)=[qq_CountryLimits_Last2Yrs t2].[maxofperiod]))
ORDER BY [Country Profile].COUNTRY, [Country Profile].Period;

***qq_CountryLimits_Last2Yrs t2****
SELECT [Country Profile].COUNTRY, Max([Country Profile].Period) AS MaxOfPeriod
FROM [Country Profile]
GROUP BY [Country Profile].COUNTRY
HAVING (((Max([Country Profile].Period))=Year(Now()) Or (Max([Country
Profile].Period))=Year(Now())-1))
ORDER BY [Country Profile].COUNTRY, Max([Country Profile].Period);
Stefan Hoffmann - 29 May 2008 09:40 GMT
hi,

> I'm sure this is an easy question for someone out there, and I used to be able
> to do this..
> I have 2 queries that produce the results I need, but I would like to remember
> how to do it with a sub-query.

SELECT
    [Country Profile].COUNTRY, [Country Profile].Period,
    [Country Profile].LQR, [Country Profile].[MLT DEBT],
    [Country Profile].[MST DEBT], [Country Profile].[SPLT DEBT],
    [Country Profile].[FLT DEBT], [Country Profile].[Country Limit]
FROM    
    [Country Profile],
    (
    SELECT
        [Country Profile].COUNTRY,
        Max([Country Profile].Period) AS MaxOfPeriod
    FROM
        [Country Profile]
    GROUP BY [Country Profile].COUNTRY
    HAVING (Max([Country Profile].Period)=Year(Now())
        Or
        (Max([Country  Profile].Period))=Year(Now())-1))
    ORDER BY
        [Country Profile].COUNTRY, Max([Country Profile].Period
) [qq_CountryLimits_Last2Yrs t2]
WHERE ((([Country Profile].COUNTRY)=[qq_CountryLimits_Last2Yrs
t2].[country])
AND (([Country Profile].Period)=[qq_CountryLimits_Last2Yrs
t2].[maxofperiod]))
ORDER BY [Country Profile].COUNTRY, [Country Profile].Period;

But there are some caveats:

Such a query may be "too complex" for Jet, thus some of these queries
cannot been rewritten to utilize sub queries.

Such a query is often read-only, so you can't modify data.

mfG
--> stefan <--
WANNABE - 30 May 2008 13:28 GMT
Thank you for your time.  I must have phrased my question poorly, as the
response here still uses the second query and my intention is to COMBINE the 2
queries into one.  Can that be done and how?
=================================================
hi,

WANNABE wrote:
> I'm sure this is an easy question for someone out there, and I used to be able
> to do this..
> I have 2 queries that produce the results I need, but I would like to remember
> how to do it with a sub-query.

SELECT
[Country Profile].COUNTRY, [Country Profile].Period,
[Country Profile].LQR, [Country Profile].[MLT DEBT],
[Country Profile].[MST DEBT], [Country Profile].[SPLT DEBT],
[Country Profile].[FLT DEBT], [Country Profile].[Country Limit]
FROM
[Country Profile],
(
SELECT
[Country Profile].COUNTRY,
Max([Country Profile].Period) AS MaxOfPeriod
FROM
[Country Profile]
GROUP BY [Country Profile].COUNTRY
HAVING (Max([Country Profile].Period)=Year(Now())
Or
(Max([Country  Profile].Period))=Year(Now())-1))
ORDER BY
[Country Profile].COUNTRY, Max([Country Profile].Period
) [qq_CountryLimits_Last2Yrs t2]
WHERE ((([Country Profile].COUNTRY)=[qq_CountryLimits_Last2Yrs
t2].[country])
AND (([Country Profile].Period)=[qq_CountryLimits_Last2Yrs
t2].[maxofperiod]))
ORDER BY [Country Profile].COUNTRY, [Country Profile].Period;

But there are some caveats:

Such a query may be "too complex" for Jet, thus some of these queries
cannot been rewritten to utilize sub queries.

Such a query is often read-only, so you can't modify data.

mfG
--> stefan <--
Stefan Hoffmann - 30 May 2008 14:03 GMT
hi,

> Thank you for your time.  I must have phrased my question poorly, as the
> response here still uses the second query and my intention is to COMBINE the 2
> queries into one.  
Yup, that's true. My example uses a sub-query.

> Can that be done and how?
As you have to filter for an aggregate, no. Because you need to build
that aggregat and then you need to join it.

mfG
--> stefan <--
WANNABE - 30 May 2008 14:49 GMT
I thought I had found a way to do just that, in a query I needed 2 years ago,
but I could be mistaken.
Thanks for your time.
========================
hi,

WANNABE wrote:
> Thank you for your time.  I must have phrased my question poorly, as the
> response here still uses the second query and my intention is to COMBINE the 2
> queries into one.
Yup, that's true. My example uses a sub-query.

> Can that be done and how?
As you have to filter for an aggregate, no. Because you need to build
that aggregat and then you need to join it.

mfG
--> stefan <--
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.