簡體   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) ;

此查詢的輸出是

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

我想在底部添加總計,例如,

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

第 5 列和最后一列不必有 %。

在這種情況下,最簡單的方法可能是:

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