使用SQL * Plus从以前的SELECT中选择总计

时间:2013-11-06 13:24:45

标签: sql sqlplus

使用SQL * Plus生成通过电子邮件发送给客户的列表,例如:

SET MARKUP HTML ON
SPOOL spool.html
SELECT order_number, entry_date, delivery_date, customer_order_number, order_totals_quantity, TRUNC(order_totals_sqm,2), order_totals_net_value FROM orders WHERE entry_date = SYSDATE;

如何创建一行显示列出的order_totals字段的总数并使其与这些字段保持一致?

即。如果我为这些字段单独SELECT COUNT(),那么当它们真正需要位于原始SELECT之下时,它会将它们列在前3个字段下。

更新:如果可能的话,这就是我要找的。

other columns ... order_totals_quantity | TRUNC(order_totals_sqm,2) | order_totals_net_value
--------------------------------------------------------------------------------------------
                  Total                 | Total                     | Total

3 个答案:

答案 0 :(得分:0)

...也许 取决于您想要的汇总和什么表示唯一记录,以免错误地汇总数量。

SELECT order_number, entry_date, delivery_date, customer_order_number, 
  sum(order_totals_quantity), sum(TRUNC(order_totals_sqm,2)), sum(order_totals_net_value)
FROM orders 
WHERE entry_date = SYSDATE;
GROUP BY GROUPING SETS ((order_number, entry_date, delivery_date, customer_order_number),
         ())

找到的示例:http://www.oracle-base.com/articles/misc/rollup-cube-grouping-functions-and-grouping-sets.php

答案 1 :(得分:0)

试试这个[假设您正在使用oracle]:

SELECT order_number, entry_date, delivery_date, customer_order_number, order_totals_quantity, TRUNC(order_totals_sqm,2), order_totals_net_value,tot.a, tot.b 
FROM orders, (select sum(order_totals_quantity) a, sum(order_totals_net_value ) b from orders WHERE entry_date = SYSDATE) tot 
WHERE entry_date = SYSDATE;

答案 2 :(得分:0)

当您使用SQL * Plus时,使用计算有一种更简单的方法。这样做的好处是服务器上不运行额外的SQL。以下是您可以根据查询进行调整的示例:

    BREAK ON report
    COMPUTE SUM LABEL total OF a ON report

    SELECT 1 AS a FROM dual UNION ALL
    SELECT 2 AS a FROM dual UNION ALL
    SELECT 3 AS a FROM dual;

                A
    -------------
                1
                2
                3
    -------------
                6

    3 rows selected.

您也可以使用其他聚合。以下是完整文档的链接:COMPUTE