从另一张表的列中减去一列的总和

时间:2019-12-07 07:21:03

标签: mysql asp.net vb.net

我尝试过的事情:

#include "Functions.h"
#include "PricingSimulation.h"


void DiscreteGBM::SetRiskFreeRate(double r1) {
    r = r1;
}

void DiscreteGBM::SetInitialStockPrice(double s) {
    S0 = s;
}

void DiscreteGBM::SetVolatility(double s) {
    sigma = s;
}

void DiscreteGBM::SetTimeStepSize(double d) {
    delta = d;
}

void DiscreteGBM::SetNumberSteps(int n) {
    numberSteps = n;
}

vector<double> DiscreteGBM::PricePath() const {
    vector<double> stock;
    vector<double> rnorm;
    rnorm = randn(numberSteps);
    stock[0] = S0;
    for (int i = 1; i <= numberSteps; i++) {
        stock[i] = stock[i - 1] * exp((r - 0.5 * sigma * sigma) * delta + sigma * sqrt(delta) * rnorm[i]);
    }
    return stock;
}

void EuropeanOption::SetCallPut(bool c) {
    callPut = c;
}

void EuropeanOption::SetStrike(double s) {
    strike = s;
}

double EuropeanOption::Payoff(double S_T) const {
    double value;
    if (callPut == 0)
        if ((S_T - strike) > 0)
            value = S_T - strike;
        else
            value = 0;
    else
        if ((strike - S_T) > 0)
            value = strike - S_T;
        else
            value = 0;
    return value;
}

double EuropeanOption::BlackScholesPrice() const {
    double value2, d1, d2;
    d1 = (1 / (sigma * sqrt(numberSteps * delta)) * (log(S0 / strike) + (r + sigma * sigma / 2) * numberSteps * delta));
    d2 = d1 - sigma * sqrt(numberSteps * delta);
    if (callPut == 0)
        value2 = normcdf(d1) * S0 - normcdf(d2) * strike * exp(-r * numberSteps * delta);
    else
        value2 = -normcdf(-d1) * S0 + normcdf(-d2) * strike * exp(-r * numberSteps * delta);
    return value2;
}

double EuropeanOption::MonteCarloPrice(int numberScenarios = 10000) const {
    int N = numberScenarios;
    double sum;
    double T = numberSteps * delta;
    vector <double> A;
    double O;
    for (int i = 1; i <= N; i++) {
        A = PricePath();
        O = A[numberSteps];
        sum += O;
    }
    double average = sum / N;
    return exp(-r * T) * average;
}

int main()
{
    SetSeed();
    EuropeanOption Test;
    for (double r = -0.1; r <= 0.1; r += 0.05)
        for (double sigma = 0.1; sigma <= 1; sigma += 0.3)
            for (int c = 0; c <= 1; c++)
            {
                Test.SetRiskFreeRate(r);
                Test.SetInitialStockPrice(100);
                Test.SetVolatility(sigma);
                Test.SetNumberSteps(100);
                Test.SetTimeStepSize(0.1);
                Test.SetStrike(130);
                Test.SetCallPut(c);
                cout << "r = " << r << ", sigma = " << sigma;
                cout << ", c = " << c << ": ";
                cout << Test.BlackScholesPrice() << " , ";
                cout << Test.MonteCarloPrice() << endl;
            }

    system("pause");
}

错误:选择列表中的'requestformtbl.employee_name'列无效,因为它既不包含在聚合函数中也不在GROUP BY子句中。

如果requestttbl.request_type =“假期”具有requesttbl.max_allotment = 20, 当在requestformtbl中插入新条目时,requestformtbl.request_type =“假期”和requestformtbl.total_day = 5 我想得到剩下的可休假

1 个答案:

答案 0 :(得分:0)

通过使用相关子查询来获取员工已使用天数的总和,无需使用GROUP BY就可以得到所需的结果:

SELECT rft.employee_name, 
       rft.request_type, 
       rft.total_day,
       rft.request_status, 
       rft.admin_remark, 
       rft.confirmed_by, 
       rft.date_confirmed, 
       rt.max_allotment,
       rt.max_allotment - (select sum(total_day) 
                           from requestformtbl rft2 
                           where rft2.employee_name = rft.employee_name) as Available
from requestformtbl rft
inner join requesttbl rt on rft.request_type = rt.request_type;

请注意,我已使用表别名使查询更具可读性。