Quantcast
Channel: dBforums – Everything on Databases, Design, Developers and Administrators
Viewing all articles
Browse latest Browse all 13329

Extending a Query

$
0
0
I have an aggregate query that works fine, however I need to remove the hard-coded dates as follows;

SELECT Sum(IIf([vat],iif([weekendingdate] Between #04/01/2011# and #31/12/2099#, [takings]/1.2,iif([weekendingdate] BETWEEN #30/11/2008# and #01/01/2010#,[takings]/1.15,[takings]/1.175)),[takings])) AS Total, dates.WeekNo, [n-weeklytransactions].weekendingdate FROM departments INNER JOIN (([business units] INNER JOIN [n-weeklytransactions] ON [business units].buID = [n-weeklytransactions].businessUnit) INNER JOIN dates ON [n-weeklytransactions].ztotaldate = dates.Startdate) ON (departments.department = [n-weeklytransactions].department) AND (departments.bus_unit = [n-weeklytransactions].businessUnit) GROUP BY dates.WeekNo, [n-weeklytransactions].weekendingdate HAVING ((DatePart('yyyy',[n-weeklytransactions]![weekendingdate])=DatePart('yyyy',Now())-1)) ORDER BY dates.WeekNo;

This works fine
SELECT 1.2 as rate, Sum(IIf([vat],[takings]/rate,[takings])) AS Total, dates.WeekNo, [n-weeklytransactions].weekendingdate FROM departments INNER JOIN (([business units] INNER JOIN [n-weeklytransactions] ON [business units].buID = [n-weeklytransactions].businessUnit) INNER JOIN dates ON [n-weeklytransactions].ztotaldate = dates.Startdate) ON (departments.department = [n-weeklytransactions].department) AND (departments.bus_unit = [n-weeklytransactions].businessUnit) GROUP BY dates.WeekNo, [n-weeklytransactions].weekendingdate HAVING ((DatePart('yyyy',[n-weeklytransactions]![weekendingdate])=DatePart('yyyy',Now())-1)) ORDER BY dates.WeekNo;

What I need to do is to incorporate this
SELECT (select top 1 VAT_Rate
from VAT_Rates
where VAT_Rates.VAT_Date <= [n-weeklytransactions].ZTotalDate
order by VAT_Rates.VAT_Date desc) AS Rate, *
FROM [n-weeklytransactions] ;

I tried
SELECT Max((select VAT_Rate from VAT_Rates where VAT_Date <=ZTotalDate order by VAT_Rates.VAT_Date desc)) AS vrate, Sum(IIf([vat],[takings]/[Vrate],[takings])) AS Total, Month([n-weeklytransactions]![weekendingdate]) AS [Month], Year([n-weeklytransactions]![weekendingdate]) AS [Year]
FROM departments INNER JOIN (([business units] INNER JOIN [n-weeklytransactions] ON [business units].buID = [n-weeklytransactions].businessUnit) INNER JOIN dates ON [n-weeklytransactions].ztotaldate = dates.Startdate) ON (departments.department = [n-weeklytransactions].department) AND (departments.bus_unit = [n-weeklytransactions].businessUnit)
GROUP BY Month([n-weeklytransactions]![weekendingdate]), Year([n-weeklytransactions]![weekendingdate]), vrate
ORDER BY Month([n-weeklytransactions]![weekendingdate]);

but I get the following error "You tried to execut a query that does not include the specified expression 'ztotaldate' as part of an aggreate function"

Any ideas if this is possible?

Regards
Pete

Viewing all articles
Browse latest Browse all 13329

Trending Articles