sql添加小计和总计

Posted

技术标签:

【中文标题】sql添加小计和总计【英文标题】:sql add subtotal and grand total 【发布时间】:2019-04-16 05:47:19 【问题描述】:

我正在尝试按计划添加贷款小计和总周数,并在最后一行添加所有贷款和所有周的总计。

我尝试过分组集合和汇总,但结果没有改变或错误...

这是表格:

STUDENT (**St_ID**, St_LName, St_FName, Email, Prog_ID@) 
LOCATION **(Location_ID**, Loc_Bldg, Loc_Room) 
ITEM (**Item_ID**, Item_Manuf, Item_Model, Comments1) 
COMPUTER (**Comp_ID**, Comp_Name, Year, Cost, Location_ID@, Item_ID@, Vendor_ID@) 
LOAN (**Loan_ID**, St_ID@, Comp_ID@, Start_Date, Date_Returned) 
PROGRAM (**Prog_ID**, Name) 
VENDOR (**Vendor_ID,** Name, Contact_FName, Contact_LName, Phone, Email) 

我的查询和输出,但我不知道如何添加小计和总计...

select program.Name Prog_Name, student.st_Lname||', '||st_Fname st_name, loan_id, loc_bldg||', '||loc_room location,
           to_char((date_returned-start_date)/7, '99') weeks
    from program right join student using (prog_id)
                 left join loan using (st_id)
                 join computer using (comp_id)
                 join location using (location_id)
    group by grouping sets((program.Name,st_Lname||', '||st_Fname,loan_id, loc_bldg||', '||loc_room, 
    (date_returned-start_date)))
    order by 1,2;



PROG_NAME                      ST_NAME              LOAN_ LOCATION                    WEEKS
------------------------------ -------------------- ----- --------------------------- ----------
Information System             Jiang, Yaohan        0010  Cyert Hall, 0701              0
                               Jiang, Yaohan        0012  Cyert Hall, 0701              2
                               Jiang, Yaohan        0013  Cyert Hall, 0701              6
                               Jiang, Yaohan        0014  Tepper Quad, 1009             7
                               Jiang, Yaohan        0016  Warner Hall, 1304             7
                               Xiao, Shan           0007  Cyert Hall, 0701              9
                               Xu, Sheng            0001  Baker Building, 1101         11
                               Xu, Sheng            0006  Porter Hall, 1004             9
Information Technology         Ouyang, Hsuan        0004  Baker Building, 1101          1
                               Ouyang, Hsuan        0008  Tepper Quad, 1009             5
                               Peng, Bo             0003  Warner Hall, 1304             1
                               Peng, Bo             0015  Warner Hall, 1304
                               Wu, Shinyu           0002  Tepper Quad, 1009             4
                               Wu, Shinyu           0005  Tepper Quad, 1009             0
                               Yin, Abby            0009  Tepper Quad, 1009             1

【问题讨论】:

我用的是Oracle,我也在Oracle中加了“break on prog_name”,这样表就不会重复program_name了 您也许可以使用统计函数获得小计和总计。 【参考方案1】:

问题是 a) 您的分组集不正确,并且 b) 您没有任何聚合函数来执行分组集。

我认为以下是您所追求的:

WITH your_results AS (SELECT 'Information System' prog_name, 'Jiang, Yaohan' st_name, 10 loan, 'Cyert Hall, 0701' LOCATION, 0 weeks FROM dual UNION ALL
                      SELECT 'Information System' prog_name, 'Jiang, Yaohan' st_name, 12 loan, 'Cyert Hall, 0701' LOCATION, 2 weeks FROM dual UNION ALL
                      SELECT 'Information System' prog_name, 'Jiang, Yaohan' st_name, 13 loan, 'Cyert Hall, 0701' LOCATION, 6 weeks FROM dual UNION ALL
                      SELECT 'Information System' prog_name, 'Jiang, Yaohan' st_name, 14 loan, 'Tepper Quad, 1009' LOCATION, 7 weeks FROM dual UNION ALL
                      SELECT 'Information System' prog_name, 'Jiang, Yaohan' st_name, 16 loan, 'Warner Hall, 1304' LOCATION, 7 weeks FROM dual UNION ALL
                      SELECT 'Information System' prog_name, 'Xiao, Shan' st_name, 7 loan, 'Cyert Hall, 0701' LOCATION, 9 weeks FROM dual UNION ALL
                      SELECT 'Information System' prog_name, 'Xu, Sheng' st_name, 1 loan, 'Baker Building, 1101' LOCATION, 11 weeks FROM dual UNION ALL
                      SELECT 'Information System' prog_name, 'Xu, Sheng' st_name, 6 loan, 'Porter Hall, 1004' LOCATION, 9 weeks FROM dual UNION ALL
                      SELECT 'Information Technology' prog_name, 'Ouyang, Hsuan' st_name, 4 loan, 'Baker Building, 1101' LOCATION, 1 weeks FROM dual UNION ALL
                      SELECT 'Information Technology' prog_name, 'Ouyang, Hsuan' st_name, 8 loan, 'Tepper Quad' LOCATION, 5 weeks FROM dual UNION ALL
                      SELECT 'Information Technology' prog_name, 'Peng, Bo' st_name, 3 loan, 'Warner Hall, 1304' LOCATION, 1 weeks FROM dual UNION ALL
                      SELECT 'Information Technology' prog_name, 'Peng, Bo' st_name, 15 loan, 'Warner Hall, 1304' LOCATION, NULL weeks FROM dual UNION ALL
                      SELECT 'Information Technology' prog_name, 'Wu, Shinyu' st_name, 2 loan, 'Tepper Quad' LOCATION, 4 weeks FROM dual UNION ALL
                      SELECT 'Information Technology' prog_name, 'Wu, Shinyu' st_name, 5 loan, 'Tepper Quad' LOCATION, 0 weeks FROM dual UNION ALL
                      SELECT 'Information Technology' prog_name, 'Yin, Abby' st_name, 9 loan, 'Tepper Quad' LOCATION, 1 weeks FROM dual)
SELECT prog_name,
       st_name,
       sum(loan) loan,
       LOCATION,
       sum(weeks) weeks
FROM   your_results
GROUP BY GROUPING SETS ((prog_name, st_name, loan, location, weeks), (prog_name), ())
ORDER BY prog_name, st_name, weeks;

PROG_NAME              ST_NAME             LOAN LOCATION                  WEEKS
---------------------- ------------- ---------- -------------------- ----------
Information System     Jiang, Yaohan         10 Cyert Hall, 0701              0
Information System     Jiang, Yaohan         12 Cyert Hall, 0701              2
Information System     Jiang, Yaohan         13 Cyert Hall, 0701              6
Information System     Jiang, Yaohan         14 Tepper Quad, 1009             7
Information System     Jiang, Yaohan         16 Warner Hall, 1304             7
Information System     Xiao, Shan             7 Cyert Hall, 0701              9
Information System     Xu, Sheng              6 Porter Hall, 1004             9
Information System     Xu, Sheng              1 Baker Building, 1101         11
Information System                           79                              51
Information Technology Ouyang, Hsuan          4 Baker Building, 1101          1
Information Technology Ouyang, Hsuan          8 Tepper Quad, 1009             5
Information Technology Peng, Bo               3 Warner Hall, 1304             1
Information Technology Peng, Bo              15 Warner Hall, 1304    
Information Technology Wu, Shinyu             5 Tepper Quad, 1009             0
Information Technology Wu, Shinyu             2 Tepper Quad, 1009             4
Information Technology Yin, Abby              9 Tepper Quad, 1009             1
Information Technology                       46                              12
                                            125                              63

(您可以将 your_results 子查询替换为返回您尝试分组的数据的查询。)

这具有不需要 SQL*Plus 功能(中断、计算)的优势。

如果您仍然只想输出第一行的 prog_name 而不使用 SQL*Plus 功能,您可以这样做:

SELECT CASE WHEN rn = 1 THEN pn END prog_name,
       st_name,
       loan,
       LOCATION,
       weeks
FROM   (SELECT prog_name pn,
               st_name,
               sum(loan) loan,
               LOCATION,
               sum(weeks) weeks,
               row_number() OVER (PARTITION BY prog_name ORDER BY st_name, weeks, LOCATION) rn
        FROM   your_results
        GROUP BY GROUPING SETS ((prog_name, st_name, loan, location, weeks), (prog_name), ()))
ORDER BY pn, st_name, weeks, LOCATION;

【讨论】:

谢谢!现在我成功对数据进行分组,但我不明白为什么结果中没有小计......我确实在分​​组集中添加了一个():分组集((...),(...),( )) 什么意思,结果中没有小计?我的结果中有小计吗? 哦!我找到了原因,在我的查询中我使用了 to_char 数周,所以它无法添加小计:P【参考方案2】:

打破是不够的——你必须真正计算某列的总和。下面是一个基于 Scott 架构的示例:

SQL> break on report on deptno
SQL> compute sum of sal on deptno
SQL> compute sum of sal on report
SQL>
SQL> select deptno, ename, job, sal
  2  from emp
  3  order by deptno;

    DEPTNO ENAME      JOB              SAL
---------- ---------- --------- ----------
        10 CLARK      MANAGER         2450
           KING       PRESIDENT      10000
           MILLER     CLERK           1300
**********                      ----------
sum                                  13750
        20 JONES      MANAGER         2975
           FORD       ANALYST         3000
           ADAMS      CLERK           1100
           SMITH      CLERK            920
           SCOTT      ANALYST         3000
**********                      ----------
sum                                  10995
        30 WARD       SALESMAN        1250
           TURNER     SALESMAN        1500
           ALLEN      SALESMAN        1600
           JAMES      CLERK            950
           BLAKE      MANAGER         2850
           MARTIN     SALESMAN        1250
**********                      ----------
sum                                   9400
                                ----------
sum                                  34145

14 rows selected.

SQL>

【讨论】:

【参考方案3】:

请从以下示例中获取参考。希望它能解决您的问题

Table: Earning

    Name    Monthly_Earning  Month
    ---------------------------------
    A       1000            January
    A       1500            January
    B       2400            Febuary
    A       1500            Febuary
    B       2100            January
    B       1100            Febuary
    A       4000            Febuary
    B       8000            January

 select Name,Monthly_Earning,Month from (
select Name,Monthly_Earning,Month from Earning e1 
union all 
select 'subtotal',sum(e2.Monthly_Earning), month from Earning e2 group by Month
union all 
select 'subtotal',sum(e2.Monthly_Earning), month from Earning e3
) e4
order by e4.Month, e4.Name

【讨论】:

那将是非常低效的;您正在访问同一张表 3 次。对于小型数据集来说这没问题(有点),但请注意数百万行......性能会受到影响。

以上是关于sql添加小计和总计的主要内容,如果未能解决你的问题,请参考以下文章

SQL SEVER分类汇总后如何让小计和总计放到下面

SQL如何查询总计和小计

oracle中如何添加小计和总计

在 Shiny 中将总计/小计添加到数据表的底部

实战 SQL:销售数据的小计/合计/总计以及数据透视表

结帐 magento 1.5 中的小计和总计加倍