如何通过计算多行获得结果表?
Name Type Value
------------------------------
Name1 Plus 5000
Name1 Minus 1000
Name2 Plus 3000
Name2 Minus 2000
所需结果:
Name Value(Plus-Minus)
------------------------------
Name1 4000
Name2 1000
答案 0 :(得分:2)
使用条件聚合,并为每个名称记录组取所有“正”值的差减去所有“负”值。
SELECT
Name,
SUM(CASE WHEN Type = 'Plus' THEN [Value] ELSE 0 END) -
SUM(CASE WHEN Type = 'Minus' THEN [Value] ELSE 0 END) AS diff
FROM yourTable
GROUP BY
Name;
答案 1 :(得分:1)
您可以使用条件聚合:
> suppressMessages(suppressWarnings(library(CVXR)))
> set.seed(10)
> n <- 10
> SAMPLES <- 100
> mu <- matrix(abs(rnorm(n)), nrow = n)
> Sigma <- matrix(rnorm(n^2), nrow = n, ncol = n)
> Sigma <- t(Sigma) %*% Sigma
> w <- Variable(n)
> ret <- t(mu) %*% w
> risk <- quad_form(w, Sigma)
> constraints <- list(w >= 0, sum(w) == 1,ret==mean(mu))
> prob <- Problem(Minimize(risk), constraints)
> result <- solve(prob,solver='MOSEK', verbose=TRUE)
Problem
Name :
Objective sense : min
Type : CONIC (conic optimization problem)
Constraints : 24
Cones : 1
Scalar variables : 23
Matrix variables : 0
Integer variables : 0
Optimizer started.
Presolve started.
Linear dependency checker started.
Linear dependency checker terminated.
Eliminator - tries : 0 time : 0.00
Lin. dep. - tries : 1 time : 0.00
Lin. dep. - number : 0
Presolve terminated. Time: 0.00
Problem
Name :
Objective sense : min
Type : CONIC (conic optimization problem)
Constraints : 24
Cones : 1
Scalar variables : 23
Matrix variables : 0
Integer variables : 0
Optimizer - threads : 8
Optimizer - solved problem : the primal
Optimizer - Constraints : 13
Optimizer - Cones : 1
Optimizer - Scalar variables : 22 conic : 12
Optimizer - Semi-definite variables: 0 scalarized : 0
Factor - setup time : 0.00 dense det. time : 0.00
Factor - ML order time : 0.00 GP order time : 0.00
Factor - nonzeros before factor : 89 after factor : 91
Factor - dense dim. : 0 flops : 2.54e+03
ITE PFEAS DFEAS GFEAS PRSTATUS POBJ DOBJ MU TIME
0 1.0e+00 6.9e+00 1.0e+00 0.00e+00 2.756709152e+01 2.756709152e+01 1.0e+00 0.00
1 2.7e-01 1.8e+00 2.2e-01 -6.02e-01 -9.936262607e+00 4.762263760e+00 2.7e-01 0.00
2 9.9e-02 6.8e-01 4.3e-01 1.23e+00 9.201188225e-01 -6.807855573e-01 9.9e-02 0.00
3 3.6e-02 2.5e-01 4.4e-01 1.97e+00 -1.264256365e-01 -5.225584219e-01 3.6e-02 0.00
4 9.2e-03 6.3e-02 1.8e-01 2.33e+00 2.868592743e-01 2.542157558e-01 9.2e-03 0.00
5 2.0e-03 1.4e-02 8.8e-02 1.17e+00 2.840102253e-01 2.769232377e-01 2.0e-03 0.00
6 3.7e-04 2.5e-03 3.9e-02 1.04e+00 2.879204145e-01 2.866174921e-01 3.7e-04 0.00
7 6.3e-05 4.3e-04 1.6e-02 1.02e+00 2.901039465e-01 2.898751147e-01 6.3e-05 0.00
8 1.3e-05 8.8e-05 7.4e-03 1.00e+00 2.902381000e-01 2.901914751e-01 1.3e-05 0.00
9 3.6e-06 2.5e-05 4.0e-03 1.00e+00 2.902501323e-01 2.902366424e-01 3.6e-06 0.00
10 1.1e-06 7.4e-06 2.2e-03 1.00e+00 2.902666158e-01 2.902625442e-01 1.1e-06 0.00
11 3.6e-07 2.5e-06 1.3e-03 1.00e+00 2.902689540e-01 2.902675850e-01 3.6e-07 0.00
12 5.7e-08 3.9e-07 5.1e-04 1.00e+00 2.902716546e-01 2.902714343e-01 5.7e-08 0.00
13 6.6e-09 4.6e-08 1.7e-04 1.00e+00 2.902720234e-01 2.902719974e-01 6.6e-09 0.00
Optimizer terminated. Time: 0.00
Interior-point solution summary
Problem status : PRIMAL_AND_DUAL_FEASIBLE
Solution status : OPTIMAL
Primal. obj: 2.9027202344e-01 nrm: 1e+00 Viol. con: 6e-09 var: 0e+00 cones: 0e+00
Dual. obj: 2.9027199740e-01 nrm: 1e+01 Viol. con: 0e+00 var: 8e-08 cones: 0e+00
> result$value
[1] 0.290272
>