SQLITE别名列的总和

时间:2019-05-30 14:20:05

标签: c# sqlite

我确定这个问题已经问过几次了,但看来我自己解决不了。我想将Profit的总和作为sumProfit以及ST的总和作为sumST,我应该如何精确地编写查询?

SELECT C.Match_ID, M.Match_Date, T1.TeamName as HomeTeam,
T2.TeamName as AwayTeam, L.League_MyName, S.Season_Year, M.algo,
Round((tp.Home*100),3) as TOP,
case when Round((tp.Home*100),3)=0 then 0 
else Round(1/(tp.Home),3) end as TOd,                                                        
LW.Home as LW,
case when Pr.Home=0 then 0.0 else Round((tp2.Home*100),3) end as TV,
Pr.Home as BOd,
case when Pr.Home=0 then 0.0 else Round((1/Pr.Home)*100,3) end as BOP,
case when Pr.Home=0 then 0.0 when Pr.Home<2 then 100.0 else Round(100.0/(Pr.Home-1),2) end as ST,
case when Pr.Home =0 then 0.0 when LW.Home = 'W' then Round((case when Pr.Home<2 then 100.0 else Round(100.0/(Pr.Home-1),2) end) * (Pr.Home-1),2)  
when LW.Home = 'DNB' then 0.0 else -(case when Pr.Home<2 then 100.0 else Round(100.0/(Pr.Home-1),2) end) end as Profit
FROM Matches as M
inner join (select Home, Match_ID from Computations where Computation_Type_ID = 1) as tp on tp.Match_ID = C.Match_ID
inner join (select Home, Match_ID from Computations where Computation_Type_ID = 2) as tp2 on tp2.Match_ID = C.Match_ID
inner join Computations as C on C.Match_ID = M.Match_ID
inner join Leagues as L on L.Real_League_ID = M.Real_League_ID
inner join Season as S on S.Season_ID = M.Season_ID
inner join Teams as T1 on T1.Team_ID = M.Home_TeamID
inner join Teams as T2 on T2.Team_ID = M.Away_TeamID
inner join LostWon as LW on LW.Match_ID=C.Match_ID
inner join Prices as Pr on Pr.Match_ID=C.Match_ID
where M.Real_League_ID=44
group by C.Match_ID

1 个答案:

答案 0 :(得分:0)

只需将此作为内部查询,并在内部查询的最底部删除组

SELECT Match_ID, SUM(Profit) as SUMPROFIT, SUM(ST) SUMST FROM (
SELECT C.Match_ID, M.Match_Date, T1.TeamName as HomeTeam,
    T2.TeamName as AwayTeam, L.League_MyName, S.Season_Year, M.algo,
    Round((tp.Home*100),3) as TOP,
    case when Round((tp.Home*100),3)=0 then 0 
    else Round(1/(tp.Home),3) end as TOd,                                                        
    LW.Home as LW,
    case when Pr.Home=0 then 0.0 else Round((tp2.Home*100),3) end as TV,
    Pr.Home as BOd,
    case when Pr.Home=0 then 0.0 else Round((1/Pr.Home)*100,3) end as BOP,
    case when Pr.Home=0 then 0.0 when Pr.Home<2 then 100.0 else Round(100.0/(Pr.Home-1),2) end as ST,
    case when Pr.Home =0 then 0.0 when LW.Home = 'W' then Round((case when Pr.Home<2 then 100.0 else Round(100.0/(Pr.Home-1),2) end) * (Pr.Home-1),2)  
    when LW.Home = 'DNB' then 0.0 else -(case when Pr.Home<2 then 100.0 else Round(100.0/(Pr.Home-1),2) end) end as Profit
    FROM Matches as M
    inner join (select Home, Match_ID from Computations where Computation_Type_ID = 1) as tp on tp.Match_ID = C.Match_ID
    inner join (select Home, Match_ID from Computations where Computation_Type_ID = 2) as tp2 on tp2.Match_ID = C.Match_ID
    inner join Computations as C on C.Match_ID = M.Match_ID
    inner join Leagues as L on L.Real_League_ID = M.Real_League_ID
    inner join Season as S on S.Season_ID = M.Season_ID
    inner join Teams as T1 on T1.Team_ID = M.Home_TeamID
    inner join Teams as T2 on T2.Team_ID = M.Away_TeamID
    inner join LostWon as LW on LW.Match_ID=C.Match_ID
    inner join Prices as Pr on Pr.Match_ID=C.Match_ID
    where M.Real_League_ID=44)
GROUP BY Match_ID

如果要查看更多列,可以在外部查询中添加别名,但不要忘记也将它们按组添加。