Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi there hope you can help me!

I have the following table named StockTable

StockName|StockPrice | StockDate

DOW 23 01/01/10
RSDB 15 01/01/10
DOW 22 02/01/10
RSDB 11 02/01/10
DOW 24 03/01/10
RSDB 12 03/01/10
DOW 25 04/01/10
RSDB 16 04/01/10

I need to get the latest row by stockDate for each StockName which i have achieved with the following SQL:

SELECT StockName, StockPrice, StockDate
FROM StockTable
WHERE (StockDate = (SELECT MAX(StockDate)
FROM StockTable))


Which returns this table as the most recent date is 04/01/10


StockName|StockPrice | StockDate


DOW 25 04/01/10
RSDB 16 04/01/10


This next bit is the part i am struggling with

i need to get the sum of the StockPrice within the same sql statment to return

|StockPrice|

41


Hope you can help me and if you need any clarification then please ask!

Thanks
Posted

Your current query:

SELECT StockName, StockPrice, StockDate
FROM StockTable
WHERE (StockDate = (SELECT MAX(StockDate)
FROM StockTable))



New query:
SELECT SUM(StockPrice) as StockPriceSum
FROM StockTable
WHERE (StockDate = (SELECT MAX(StockDate)
FROM StockTable)) 


Result of it would be the SUM. E.g. 41 here.
 
Share this answer
 
THanks that works great!!!!

you are a life saver!!!
 
Share this answer
 

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