|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
You don't need a fax machine to get faxes. Get a fax-to-email fax number from CallWave. Try it free.
|
|
#1
|
|||
|
|||
|
Access Query
Hi all,
I'm having difficulties getting this query together. I have a table cashflow containing several fields: ProjectID, Year, Cash_in Cash_out Now if i have three records from One project eg: IDprojectNr_FK, Year, Total_Cash_in, Total_CashOut 1, 2000, 200, 300 1, 2001, 400, 200 1, 2002, 400, 400 2, 2001, 2000, 3000 .. ...... ...... ....... Goal is to derive a Payback field that gives me the first year in which the project was payedback. So in the case of ProjectID 1 it would be year 2001 (200 + 400) > (300 + 200) payback field = sum (Total_cash_in) >= sum(Total_cash_out) but if i use the sum function it will calculate every record of IDprojectNR_FK 1 it has to stop where the criteria is met. this is what i have so far: SELECT tblCashflow.ID_ProjectNr_FK, Min(tblCashflow.Jaar) AS MinVanJaar, (Min([tblCashflow].[Jaar])-Min([tbl2].[jaar]))+1 AS payback FROM tblCashflow, tblcashflow AS tbl2 WHERE (((tblCashflow.Total_cash_in)>[tblCashFlow].[Total_cash_out]) AND ((tbl2.ID_ProjectNr_FK)=[tblCashFlow].[ID_ProjectNR_FK])) GROUP BY tblCashflow.ID_ProjectNr_FK, tbl2.ID_ProjectNr_FK; But this does not work cause it checks if Total_cash_IN > Total_cash_out, Only per record! and if i use the sum function then it checks it for every record. so that also does not work. any ideas? any help is appreciated. Sincerely Yours, D. Spangenberg |
|
#2
|
||||
|
||||
|
if this were ms sql server, you would use
Code:
select dt.IDprojectNr_FK
, min(dt.Jaar)
from (
select tbl1.IDprojectNr_FK
, tbl1.Jaar
, sum(tbl2.Total_Cash_in) as SumIn
, sum(tbl2.Total_CashOut) as SumOut
, sum(tbl2.Total_Cash_in)
- sum(tbl2.Total_CashOut) as SumDiff
from tblCashflow as tbl1
inner
join tblCashflow as tbl2
on tbl1.IDprojectNr_FK >= tbl2.IDprojectNr_FK
and tbl1.Jaar >= tbl2.Jaar
group
by tbl1.IDprojectNr_FK
, tbl1.Jaar
having sum(tbl2.Total_Cash_in)
> sum(tbl2.Total_CashOut)
) as dt
group
by dt.IDprojectNr_FK
|
|
#3
|
|||
|
|||
|
Hi r937,
Thanks for the reply, unfortunately this solution does not work for me eg: if i have the following table Code:
IDProjectNR_FK, Year, Total_Cash_IN, Total_Cash_out 1 2000 200 400 1 2001 50 25 1 2002 400 100 in this table the first year in which the table was paid back is: 2002 NOT 2001 cause it must be calculated over the cumulative cash_ins and outs and your code selects the first year which meets the criteria cash_in > cash_out per record.. correct me if i'm wrong. any other ideas? Sincerly Yours D.Spangenberg |
|
#4
|
||||
|
||||
|
Quote:
try this, i tested it: Code:
select dt.IDprojectNr_FK
, min(dt.Jaar)
from (
select tbl1.IDprojectNr_FK
, tbl1.Jaar
, sum(tbl2.Total_Cash_in) as SumIn
, sum(tbl2.Total_CashOut) as SumOut
, sum(tbl2.Total_Cash_in)
- sum(tbl2.Total_CashOut) as SumDiff
from tblCashflow as tbl1
inner
join tblCashflow as tbl2
on tbl1.IDprojectNr_FK = tbl2.IDprojectNr_FK
and tbl1.Jaar >= tbl2.Jaar
group
by tbl1.IDprojectNr_FK
, tbl1.Jaar
having sum(tbl2.Total_Cash_in)
> sum(tbl2.Total_CashOut)
) as dt
group
by dt.IDprojectNr_FK
|
|
#5
|
|||
|
|||
|
Thanks a million! works like a charm.
Sincerely Yours D.Spangenberg |
![]() |
| Viewing: Dev Shed Forums > Databases > MS SQL Development > Access Query |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|