Click here to Skip to main content
15,887,326 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello ... i want to calculate the sum of the amount ... but i am getting a blank column when i run in ms-access sql view and nothing appears when i use this query in software.

SQL
SELECT AllocateAsset.Item, AllocateAsset.ItemModel, Count(AllocateAsset.ItemModel) AS CountOfItem, (CountOfItem*(SELECT Rate FROM rateList  WHERE rateList.MakeModel=AllocateAsset.ItemModel ))/4 AS Amount, SUM(Amount) AS Total

FROM aboutPM INNER JOIN (AllocateAsset INNER JOIN PMReport ON AllocateAsset.ID=PMReport.AssetID) ON aboutPM.ID=PMReport.AboutPMID

WHERE (((aboutPM.Year)="2015") AND ((aboutPM.Period)="Jan-Feb-Mar"))

GROUP BY AllocateAsset.Item, AllocateAsset.ItemModel

ORDER BY AllocateAsset.Item;



please help me out
Posted
Updated 30-Sep-15 20:17pm
v2
Comments
Naveen.Sanagasetti 1-Oct-15 2:19am    
SELECT Rate FROM rateList WHERE rateList.MakeModel=AllocateAsset.ItemModel

Please check the result set of this...
Akshay Shringi 1-Oct-15 2:23am    
after removing sum(Amount) As total ... the query works fine
Naveen.Sanagasetti 1-Oct-15 5:04am    
Enjoy :)
Akshay Shringi 1-Oct-15 5:56am    
but i need to do the sum of the column Amount ... and i am not getting it
Jörgen Andersson 1-Oct-15 8:27am    
You can't refer to the result from one column in another column. You need to use a CTE or a subquery.

1 solution

Try this:
SQL
SELECT t1.*, SUM(t1.Amount) AS Total
FROM (
    SELECT AllocateAsset.Item, AllocateAsset.ItemModel, Count(AllocateAsset.ItemModel) AS CountOfItem,
        (CountOfItem*(SELECT Rate FROM rateList  WHERE rateList.MakeModel=AllocateAsset.ItemModel ))/4 AS Amount
    FROM aboutPM INNER JOIN 
        (AllocateAsset INNER JOIN 
            PMReport ON AllocateAsset.ID=PMReport.AssetID) ON aboutPM.ID=PMReport.AboutPMID
    WHERE (((aboutPM.Year)="2015") AND ((aboutPM.Period)="Jan-Feb-Mar"))
    GROUP BY AllocateAsset.Item, AllocateAsset.ItemModel
    ORDER BY AllocateAsset.Item;
) AS t1
GROUP BY t1.Item, t1.ItemModel, t1.CountOfItem


Does Year field is varchar data type? It should be numeric(integer) data type!
 
Share this answer
 
Comments
Akshay Shringi 3-Oct-15 6:22am    
yes year is a integer datatype
Akshay Shringi 3-Oct-15 6:23am    
what is t1 ... have you made a table form this query ? its say error in FROM clause
Maciej Los 3-Oct-15 17:01pm    
t1 is an alias for subquery. Please, correct this: SELECT t1.*, SUM(t1.Amount) AS Total. There was a bug when i posted my answer.
If Year is numeric field, change this part of query as follow: (aboutPM.Year)=2015
Akshay Shringi 5-Oct-15 1:12am    
error in From Clause ... does i have to make the query as a table t1 ?
Maciej Los 5-Oct-15 1:46am    
No!
Please, try this SELECT t1.Item, t1.ItemModel, t1.CountOfItem, (t1.Amount) As Total
FROM (
--subquery here!
) AS t1
GROUP BY ...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900