Numpy.FV Weekly payments and Daily interest - numpy

Is there a way to set payments weekly but conpound fv daily?
like this, but using numpy.
pv * daily interest = fv1
fv1 * daily interest = fv2
fv2 * daily interest = fv3
...
(fv7 + 50) * daily interest = fv8
fv8 * daily interest = fv9
...
(fv14+50)*daily interest = fv15
value= 0
interest = 0.1
period = 3650
payments = 50
print (npf.fv(interest,period,payments,value,when='end'))

Related

MS Access: match XRate to date

I have three tables: Sales, Currency, Exchange Rate
I need the appropriate exchange rate for every sale and foreign currency transaction to be calculated to our local currency. The Exchange Rate Table has values for each foreign currency, but only for one date in each month, the last day.
I have attempted the following thus far, which works, if the sales date happens to be exactely the last day of a month:
SELECT
qry_SALES.Payment_ID,
qry_SALES.Product_Name,
qry_SALES.Sales,
qry_SALES.SalesDate,
qry_SALES.SalesCcy,
tbl_XRate.XRate_CHF,
tbl_XRate.XDate
FROM
qry_SALES INNER JOIN
(tbl_Currency INNER JOIN tbl_XRate ON tbl_Currency.Currency_ID = tbl_XRate.Currency_ID)
ON (qry_SALES.SalesDate = tbl_XRate.XDate) AND (qry_SALES.SalesCcy = tbl_Currency.Ccy)
ORDER BY qry_SALES.SalesDate;
How do I get transactions that are during a month to match up with the exchange rate table's last value?
Try this using the matching ultimo date of the month:
FROM
qry_SALES
INNER JOIN
(tbl_Currency
INNER JOIN tbl_XRate
ON tbl_Currency.Currency_ID = tbl_XRate.Currency_ID)
ON (DateSerial(Year(qry_SALES.SalesDate), Month(qry_SALES.SalesDate) + 1, 0) = tbl_XRate.XDate)
AND (qry_SALES.SalesCcy = tbl_Currency.Ccy)
or:
FROM
qry_SALES
INNER JOIN
(tbl_Currency
INNER JOIN tbl_XRate
ON tbl_Currency.Currency_ID = tbl_XRate.Currency_ID)
ON (qry_SALES.SalesCcy = tbl_Currency.Ccy)
WHERE
DateSerial(Year(qry_SALES.SalesDate), Month(qry_SALES.SalesDate) + 1, 0) = tbl_XRate.XDate

altering query in db2 to fix count from a join

I'm getting an aggregated count of records for orders and I'm getting the expected count on this basic query:
SELECT
count(*) as sales_180,
180/count(*) as velocity
FROM custgroup g
WHERE g.cstnoc = 10617
AND g.framec = 4847
AND g.covr1c = 1763
AND g.colr1c = 29
AND date(substr(g.extd1d,1,4)||'-'||substr(g.EXTD1d,5,2)||'-'||substr(g.EXTD1d,7,2) ) between current_Date - 180 DAY AND current_Date
But as soon as I add back in my joins and joined values then my count goes from 1 (which it should be) to over 200. All I need from these joins is the customer ID and the manager number. so even if my count is high, I'm basically just trying to say "for this cstnoc, give me the slsupr and xlsno"
How can I perform this below query without affecting the count? I only want my count (sales_180 and velocity) coming from the custgroup table based on my where clause, but I then just want one value of the xcstno and xslsno based on the cstnoc.
SELECT
count(*) as sales_180,
180/count(*) as velocity,
c.xslsno as CustID,
cr.slsupr as Manager
FROM custgroup g
inner join customers c
on g.cstnoc = c.xcstno
inner join managers cr
on c.xslsno = cr.xslsno
WHERE g.cstnoc = 10617
AND g.framec = 4847
AND g.covr1c = 1763
AND g.colr1c = 29
AND date(substr(g.extd1d,1,4)||'-'||substr(g.EXTD1d,5,2)||'-'||substr(g.EXTD1d,7,2) ) between current_Date - 180 DAY AND current_Date
GROUP BY c.xslsno, cr.slsupr
You are producing multiple rows when joining, so your count is now counting all the resulting rows with all that [unintended] multiplicity.
The solution? Use a table expression to pre-compute your count, and then you can join it to the other tables, as in:
select
g2.sales_180,
g2.velocity,
c.xslsno as CustID,
cr.slsupr as Manager
from customers c
join managers cr on c.xslsno = cr.xslsno
join ( -- here the Table Expression starts
SELECT
count(*) as sales_180,
180/count(*) as velocity
FROM custgroup g
WHERE g.cstnoc = 10617
AND g.framec = 4847
AND g.covr1c = 1763
AND g.colr1c = 29
AND date(substr(g.extd1d,1,4)||'-'||substr(g.EXTD1d,5,2)
||'-'||substr(g.EXTD1d,7,2) )
between current_Date - 180 DAY AND current_Date
) g2 on g2.cstnoc = c.xcstno
You can also use a Common Table Expression (CTE) that will produce the same result:
with g2 as (
SELECT
count(*) as sales_180,
180/count(*) as velocity
FROM custgroup g
WHERE g.cstnoc = 10617
AND g.framec = 4847
AND g.covr1c = 1763
AND g.colr1c = 29
AND date(substr(g.extd1d,1,4)||'-'||substr(g.EXTD1d,5,2)
||'-'||substr(g.EXTD1d,7,2) )
between current_Date - 180 DAY AND current_Date
)
select
g2.sales_180,
g2.velocity,
c.xslsno as CustID,
cr.slsupr as Manager
from customers c
join managers cr on c.xslsno = cr.xslsno
join g2 on g2.cstnoc = c.xcstno

Update table with rolling average from itself

I'm trying to update a temp table with a rolling average calculation (MS Access 2010.)
As a select query, this works to calculate a 3 month rolling average but is slow so I'd rather have the values stored and updated only when necessary:
SELECT tempQORDistGrouped.Type, tempQORDistGrouped.Supplier, tempQORDistGrouped.DepBkMo, tempQORDistGrouped.Amt, tempQORDistGrouped.Brands, tempQORDistGrouped.T2, tempQORDistGrouped.Brand, (select avg(rolavg.Amt) from tempQORDistGrouped as rolavg
where rolavg.Type = tempQORDistGrouped.Type
and rolavg.Supplier = tempQORDistGrouped.Supplier
and rolavg.Brands = tempQORDistGrouped.Brands
and rolavg.Brand = tempQORDistGrouped.Brand
and rolavg.DepBkMo between dateadd("m",-2,tempQORDistGrouped.DepBkMo) and tempQORDistGrouped.depbkmo) AS AvgAmt
FROM tempQORDistGrouped;
I've tried the update query below but I think my inner join syntax is bad as it won't recognize x1.Type as a valid field (do I need to include these as part of the inner join fields rather than in the where clause??):
UPDATE tempQORDistGrouped AS x1
INNER JOIN (SELECT itmID, avg(Amt) AS RolAvg
FROM tempQORDistGrouped
WHERE tempQORDistGrouped.Type = x1.Type
AND tempQORDistGrouped.Brand = x1.Brand
AND tempQORDistGrouped.Brands = x1.Brands
AND tempQORDistGrouped.T2 = x1.T2
AND tempQORDistGrouped.DepBkMo between dateadd("m",-2,x1.DepBkMo) and x1.DepBkMo
GROUP BY itmID
) AS x2
ON x1.itmID = x2.itmID
SET x1.3MonthRollingAmt = x2.RolAvg;
Cheers
Untested but should work.
I am doing a column level query to calculated the AVG and then mapping it back to the rest of the columns
Try this:
UPDATE tempQORDistGrouped AS x1
INNER JOIN (
SELECT itmID
, (
SELECT avg(Amt) amt
FROM tempQORDistGrouped x4
WHERE x4.Type = x3.Type
AND x4.Brand = x3.Brand
AND x4.Brands = x3.Brands
AND x4.T2 = x3.T2
AND x4.DepBkMo BETWEEN dateadd("m", - 2, x3.DepBkMo) AND x3.DepBkMo
) AS RolAvg
, x3.Brand
, x3.Brands
, x3.DepBkMo
, x3.T2
FROM tempQORDistGrouped x3
) AS x2
ON x1.itmID = x2.itmID
AND x1.Brand = x2.Brand
AND x1.Brands = x2.Brands
AND x1.T2 = x2.T2
AND x1.DepBkMo BETWEEN dateadd("m", - 2, x2.DepBkMo) AND x2.DepBkMo
SET x1.3MonthRollingAmt = x2.RolAvg;

Postgres Update column with another rows data

Ok I have two tables
measures
attr_id, period, net_orders, ref_1 (key = attr_id,period)
and
policy
attr_id, lead_time
What I need to do is grab the 'net_orders' from measure at period (which is a date), Add the 'lead_time' and update the measure table 'ref_1' where period = period+lead
I currently have the select that gets me the data I need but I keep losing myself in my head when trying to figure out the where clauses.
SELECT
m.attr_id,
m.period,
m.net_orders,
p.lead_time,
DATE(m.period) + CAST(p.lead_time as INTEGER) as updateperiod
FROM
measures m
INNER JOIN policy p ON p.attr_id = m.attr_id
I am stuck with some of the following query - aka incomplete
UPDATE
measures m
SET
ref_1 = (SELECT m1.net_orders FROM measures m1
WHERE m1.attr_id = m.attr_id AND m1.period = m.period)
WHERE
attr_id = (SELECT m3.attr_id
FROM measures m3 WHERE m3.attr_id = m.attr_id
AND m3.period = m.period)
AND m.period = (SELECT DATE(m2.period) + CAST(p2.lead_time AS INTEGER)
FROM measures m2 INNER JOIN policy p2 ON p2.attr_id = m2.attr_id
WHERE m2.attr_id = m.attr_id AND m2.period = m.period)
EDIT
update measures m
set reference_1 = s.net_orders
from (
select
m.attribute_id, period, net_orders,
DATE(period) + CAST(lead_time as integer) as periodlevel
from
measures m
inner join policies p on p.attribute_id = m.attribute_id
) s
where
m.attribute_id = s.attribute_id
and m.period = s.periodlevel
This is the query that has ended up working. I was getting errors with first answer but looks like it is working now!
update measures m
set ref_1 = s.net_orders
from (
select
m.attr_id, period, net_orders,
period::date + lead_time::int period
from
measures m
inner join
policy using(attr_id)
) s
where
s.attr_id = m.attr_id
and s.period = m.period::date

LightSwitch Linq To Entities query problem !

I'm very new to Linq and Entities Framework. I'm trying to create a WCF RIA Service into a LightSwitch Orders Management application I'm creating. I've already created 7 or 8 queries using Linq (with lot of headaches) but I succeded. Now I need to create a query who gets TotalSales and TotalExpenses by month by using three tables :
DetailsVentes (SaleDetails) composed of : DateOfOrder, Quantity, UnitPrice, Total.... etc
DetailsAchat (PurchaseDetails) composed of the same fields
Charges (Expenses) composed of : DateOfExpense, ExpenseType, Cost....etc
Now I want to have a query that finds the Net Income per month (income=TotalSales-TotalPurchases-TotalExpenses)
I tried this query
Return From od In Me.Context.DetailsVentes
From od2 In Me.Context.DetailsAchats
From od3 In Me.Context.Charges
Group By Month = od.Vente.DateCommande.Month, Year = od.Vente.DateCommande.Year
Into g = Group
Select New TCR With {.Month = Month, .Year = Year,
.MonthYearString = Month & "/" & Year,
.TotalVentes = g.Sum(Function(s) (s.od.PrixUnitaire * s.od.Quantité)),
.TotalAchats = g.Sum(Function(s) (s.od2.PrixAchat * s.od2.Quantité)),
.TotalCharges = g.Sum(Function(s) (s.od3.Cout))}
Where Vente is Sale (Order) and DetailVentes is OrderDetails
Achat is Purchase, and DetailAchat PurchaseDetail
a Charge is an Expense.
The Problem is I got weird results. All values (TotalVentes, TotalAchats, TotalCharges) are multiplicated by 13 !
For eg if I have 10 000 USD totalSales for June 2011, this query returns 130 000 USD !
I'm really stuck 4 days trying to get the correct query.
I repeat I'm new to Linq :p
Thank you very much.
Edit : I tried #Jason advice I did this :
Return From c In Me.Context.Charges
Join v In Me.Context.DetailsVentes On c.DateCharge.Month Equals v.Vente.DateCommande.Month And c.DateCharge.Year Equals v.Vente.DateCommande.Year
Join a In Me.Context.DetailsAchats On c.DateCharge.Month Equals a.Achat.DateCommande.Month And c.DateCharge.Year Equals a.Achat.DateCommande.Year
Group By month = c.DateCharge.Month, Year = c.DateCharge.Year
Into g = Group
Select New TCR With {.Month = month, .Year = Year, .MonthYearString = "", .TotalAchats = g.Sum(Function(c) (c.a.Quantité * c.a.PrixAchat)), .TotalCharges = 45000, .TotalVentes = 250000, .TCRSingle = 0}
And results are still very weird (very high) ! Any exemple I can follow ??
Thank you.
I finally had it works by splitting the query to 3 queries like this :
Dim vt = From od In Me.Context.DetailsVentes
Group By Month = od.Vente.DateCommande.Month, Year = od.Vente.DateCommande.Year
Into g = Group
Select New VentesParMois With {.Month = Month, .Year = Year,
.TotalVentesSingle = g.Sum(Function(od) _
(od.PrixUnitaire * od.Quantité))}
Dim at = From od In Me.Context.DetailsAchats
Group By Month = od.Achat.DateCommande.Month, Year = od.Achat.DateCommande.Year
Into g = Group
Select New AchatsParMois With {.Month = Month, .Year = Year,
.TotalAchatsSingle = g.Sum(Function(od) _
(od.PrixAchat * od.Quantité))}
Dim ct = From od In Me.Context.Charges
Group By Month = od.DateCharge.Month, Year = od.DateCharge.Year
Into g = Group
Select New ChargesParMois With {.Month = Month, .Year = Year,
.TotalChargesSingle = g.Sum(Function(od) _
(od.Montant))}
Dim q = From a In at.AsEnumerable, v In vt.AsEnumerable, c In ct.AsEnumerable
Where a.MonthYear = v.MonthYear AndAlso a.MonthYear = c.MonthYear
Select New TCR With {.Month = a.Month, .Year = a.Year, .TotalAchats = a.TotalAchats, .TotalVentes = v.TotalVentes, .TotalCharges = c.TotalCharges}
Return q.AsQueryable
Thank you very much Jason.
I suspect you want a join somewhere in your query rather than a cartesian product (a join without any join conditions).