简体   繁体   English

在同一查询中使用总计的 sql 查询

[英]sql query with totals in the same query

select 
    grade, pro_abo as procUpper, pro_with as procMiddle, 
    pro_below as procLower, pro_insuf as procNa, 
    cum_abo as cumulativeUpper, cum_with as cumulativeMiddle, 
    cum_below as cumulativeLower, cum_insuf as cumulativeNa
from 
    (select distinct v.* 
     from M_VW v, m_info mi
     where v.id = mi.id and mi.id_ti = 1094574915) ;

The output of this query is此查询的输出是

Grade       procUpper   procMiddle  procLower   procNa  cumulativeUpper cumulativeMiddle  cumulativeLower   cumulativeNa
HOD         100         100         100         4       100             100                 100             10
lecturer    100         100         100         10      100             100                 100             10
Professor   100         100         100         10      100             100                 100             10
RA          100         100         100         10      100             100                 100             10
PA          100         100         100         4       100             100                 100             10

I want to add totals at the bottom like ,我想在底部添加总计,例如,

Grade       procUpper   procMiddle  procLower   procNa  cumulativeUpper cumulativeMiddle  cumulativeLower   cumulativeNa
HOD         100         100         100         4       100             100                 100             10
lecturer    100         100         100         10      100             100                 100             10
Professor   100         100         100         10      100             100                 100             10
RA          100         100         100         10      100             100                 100             10
PA          100         100         100         4       100             100                 100             10
Total       100%        100%        100%        38      100%            100%                100%            50

The 5th column and the last column doesnt have to have %.第 5 列和最后一列不必有 %。

The simplest method in this case is probably:在这种情况下,最简单的方法可能是:

with t as (
      select grade, pro_abo as procUpper, pro_with as procMiddle, pro_below as procLower,
             pro_insuf as procNa, cum_abo as cumulativeUpper, cum_with as cumulativeMiddle, cum_below as cumulativeLower,
             cum_insuf as cumulativeNa
      from (select distinct v.*
            from M_VW v join
                 m_info mi
            where v.id = mi.id and mi.id_ti = 1094574915
         ) v
      )
select t.*
from t
union all
select NULL, sum(pro_abo), sum(pro_with), sum(pro_below), sum(pro_insuf), sum(cum_abo), sum(cum_with), sum(cum_below)
from t
order by grade nulls last;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM