I want to select the year and quarter parts of the date in a table rather than the whole date. I built the following query to achieve this:
SELECT Clients.ClientID,
Sum(AccountVals.ValuationAmount) AS SumOfValuationAmount,
Datepart("YYYY", AccountVals.valuationAmount) AS valYear,
Datepart("Q", AccountVals.valuationAmount) AS valQuarter
FROM (Accounts
INNER JOIN Clients
ON Accounts.ClientID = Clients.ClientID)
INNER JOIN AccountVals
ON Accounts.AccountID = AccountVals.AccountID
GROUP BY Clients.ClientID,
SumOfValuationAmount,
valYear,
valQuarter;
Although I have successfully used DATEPART
in a simple select query the fact that this query is both an aggregate function and have linked tables is throwing me. Any ideas?
EDIT: I realise my query doesn't have any criteria which means it could be trying to aggregate the result from different dates so it can't show the dateparts. I tried adding something like HAVING valYear = 2012 AND valQuarter = 3
but get the same error