sql 添加小计和总计
sql add subtotal and grand total
我正在尝试通过程序添加贷款小计和总周数,并在最后一行添加所有贷款和所有周的总计。
我试过分组集和汇总,但结果没有改变或错误...
这里是 table:
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
仅仅 break 是不够的 - 你必须实际 计算 某些列的总和。这是一个基于 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>
请参考下面的例子。希望能解决你的问题
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
问题是 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 子查询替换为 returns 您尝试分组的数据的查询。)
这样做的好处是不需要 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;
我正在尝试通过程序添加贷款小计和总周数,并在最后一行添加所有贷款和所有周的总计。
我试过分组集和汇总,但结果没有改变或错误...
这里是 table:
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
仅仅 break 是不够的 - 你必须实际 计算 某些列的总和。这是一个基于 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>
请参考下面的例子。希望能解决你的问题
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
问题是 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 子查询替换为 returns 您尝试分组的数据的查询。)
这样做的好处是不需要 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;