473,408 Members | 2,087 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

SQL query - combine sum() with getting latest record by date field

Hello group,
I have an SQL statement which pulls data from a table as follows:
" SELECT tblSites.sites_siteno, " & _
" tblSites.sites_sitename, " & _
" Sum(tblStockResults.stkr_result) AS SumOfstkr_result, " & _
" Sum(tblStockResults.stkr_takings) AS SumOfstkr_takings, " & _
" Sum(tblStockResults.stkr_gp) AS SumOfstkr_gp, " & _
" last(tblStockResults.stkr_closestkval) AS LastOfstkr_closestkval,

" & _
" Last(tblStockResults.stkr_SoHDays) AS LastOfstkr_SoHDays, " & _
" Last(tblStockResults.stkr_endat) AS LastOfstkr_endat, " & _
" sum(tblStockResults.stkr_cashdiff) as SumOfstkr_cashdiff, " & _
" sum(tblstockresults.stkr_resultcost) as SumOfstkr_resultcost "
" FROM tblSites " & _
" INNER JOIN tblStockResults " & _
" ON tblSites.sites_siteid = tblStockResults.stkr_siteid " & _
" WHERE tblStockResults.stkr_endat Is Not Null " & _
" GROUP BY tblSites.sites_siteno, tblSites.sites_sitename; "
The three 'last' statements must be the latest values (by date -
stkr_endat) from that particular site (siteid) but the 'sum' statements

must remain as a sum of all values from that site.
However, if the data is originally fed into the table out of date order

then this always pulls up the last record to be physically entered
instead of the latest record by date (if that makes sense!)
How can I change this so that it always pulls up the latest dated
record (from stkr_endat) and not just the last one to be entered?
Thanks in advance,
Alan

Jan 6 '06 #1
5 7184
PS. I know this is an Access group but I am doing this in Access.

Alan

Jan 6 '06 #2
On 6 Jan 2006 09:05:37 -0800, al****************@yahoo.co.uk wrote:
Hello group,
I have an SQL statement which pulls data from a table as follows:
" SELECT tblSites.sites_siteno, " & _
" tblSites.sites_sitename, " & _
" Sum(tblStockResults.stkr_result) AS SumOfstkr_result, " & _
" Sum(tblStockResults.stkr_takings) AS SumOfstkr_takings, " & _
" Sum(tblStockResults.stkr_gp) AS SumOfstkr_gp, " & _
" last(tblStockResults.stkr_closestkval) AS LastOfstkr_closestkval,

" & _
" Last(tblStockResults.stkr_SoHDays) AS LastOfstkr_SoHDays, " & _
" Last(tblStockResults.stkr_endat) AS LastOfstkr_endat, " & _
" sum(tblStockResults.stkr_cashdiff) as SumOfstkr_cashdiff, " & _
" sum(tblstockresults.stkr_resultcost) as SumOfstkr_resultcost "
" FROM tblSites " & _
" INNER JOIN tblStockResults " & _
" ON tblSites.sites_siteid = tblStockResults.stkr_siteid " & _
" WHERE tblStockResults.stkr_endat Is Not Null " & _
" GROUP BY tblSites.sites_siteno, tblSites.sites_sitename; "
The three 'last' statements must be the latest values (by date -
stkr_endat) from that particular site (siteid) but the 'sum' statements

must remain as a sum of all values from that site.
However, if the data is originally fed into the table out of date order

then this always pulls up the last record to be physically entered
instead of the latest record by date (if that makes sense!)
How can I change this so that it always pulls up the latest dated
record (from stkr_endat) and not just the last one to be entered?
Thanks in advance,
Alan


Use Max instead of Last. eg -

Max(tblStockResults.stkr_SoHDays) AS LastOfstkr_SoHDays
Wayne Gillespie
Gosford NSW Australia
Jan 7 '06 #3
PS. I know this is an Access group but I am doing this in Access.

Alan

Jan 7 '06 #4

Wayne Gillespie wrote:

Use Max instead of Last. eg -

Max(tblStockResults.stkr_SoHDays) AS LastOfstkr_SoHDays
Wayne Gillespie
Gosford NSW Australia


OK, but this will olny work for the date field (stkr_endat), if I use
it for the other two fields then it will return the *highest* value
which will not always be the correct one.

stkr_Closestkval is a currency value and stkr_SoHDays is a numerical
value.

Thanks,

Alan

Jan 9 '06 #5
<al****************@yahoo.co.uk> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...

Wayne Gillespie wrote:

Use Max instead of Last. eg -

Max(tblStockResults.stkr_SoHDays) AS LastOfstkr_SoHDays
Wayne Gillespie
Gosford NSW Australia


OK, but this will olny work for the date field (stkr_endat), if I use
it for the other two fields then it will return the *highest* value
which will not always be the correct one.

stkr_Closestkval is a currency value and stkr_SoHDays is a numerical
value.

Thanks,

Alan

The following SQL is not tested, but may work as it is. If it does need
tweaking, it at least gives you the idea for a general solution.
On thing that crosses my mind is what happens if tblStockResults has two
records for the same site number on the same date. Perhaps you have an
index which prevents this, but if not, you could use the primary key of the
table to make sure that the subquery always selects one record. The
subqueries then need ORDER BY stkr_endat DESC, stkr_id DESC assuming that
stkr_id is the name of this field:
SELECT
S.sites_siteno,
S.sites_sitename,
T.*,
(SELECT TOP 1 stkr_closestkval FROM tblStockResults
WHERE T.stkr_siteid=tblStockResults.stkr_siteid
ORDER BY stkr_endat DESC) AS LastOfstkr_closestkval,
(SELECT TOP 1 stkr_SoHDays FROM tblStockResults
WHERE T.stkr_siteid=tblStockResults.stkr_siteid
ORDER BY stkr_endat DESC) AS LastOfstkr_SoHDays,
(SELECT TOP 1 stkr_endat FROM tblStockResults
WHERE T.stkr_siteid=tblStockResults.stkr_siteid
ORDER BY stkr_endat DESC) AS LastOfstkr_endat
FROM (SELECT
stkr_siteid,
Sum(stkr_result) AS SumOfstkr_result,
Sum(stkr_takings) AS SumOfstkr_takings,
Sum(stkr_cashdiff) as SumOfstkr_cashdiff,
Sum(stkr_resultcost) as SumOfstkr_resultcost
FROM tblStockResults
WHERE stkr_endat Is Not Null
GROUP BY stkr_siteid) AS T
INNER JOIN tblSites AS S
ON S.sites_siteno=T.stkr_siteid


Jan 10 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Cady Steldyn | last post by:
Example: Date | ItemCode | Stock_In_qty | Stock_Out_qty | Bal_qty ------------------------------------------------------------------ 12/09/2003 | A100 | 20 | 0 ...
6
by: paii | last post by:
I have a table that stores job milestone dates. The 2 milestones I am interested in are "Ship Date" TypeID 1 and "Revised Ship Date" TypeID 18. All jobs have TypeID 1 only some jobs have TypeID 18....
7
by: Arild Larsen | last post by:
Hei I'd like to find total of one field based on the size of another field Pipedim Length 2 2 2 3 2 2 1 3 1 4 3 6 3 1
2
by: Dom | last post by:
I need to run a query based on a query in Access. The second query has a number of conditions which all work well, but there is one more contition I need to set to make it run properly. the...
4
by: cefrancke | last post by:
Are there any ways to speed up a Cumulative sum in a query or perhaps another faster way to have a cumulative sum column (call a vba function?). For example, I want to sum up all values under...
1
by: ????? | last post by:
I have a query which shows the following fields and orders by every one in turn from left to right in ascending order. The fields are DATE, , WORK, *, DESCRIPTION and . How can I get a running...
5
by: mebrabham | last post by:
Hello, I am trying to create a running subtotal in a query and then graph the running subtotal for each city group. The data looks like this in the table (for illustration): My City My...
9
by: JJM0926 | last post by:
I'm trying to create a running totals query in access 97. I have followed the directions on how to do it from Microsofts website article id 138911. I took their code they had and replaced it with...
4
by: Philip_collins | last post by:
Hi! I have a dynamic query that has a form attached. I want to add up a field with the sum funtion, the field is netkg. Dim rec As Recordset Dim qdf As QueryDef Dim strsql Set db =...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.