我想为我的SQL披萨餐厅数据库创建一个“销售报告”。当我尝试使用sum(order_amount)运行SELECT查询时,该列未格式化。我正在使用Oracle SQL。这是代码:
column order_amount heading "Order Amount" format '$9,999,999.00';
select order_id, count(order_id) as "Number of Orders", sum(pizza_quantity) as "Pizzas Sold",
sum(order_amount) as "Total Profit"
from order_details group by order_id;
这是输出:
SQL> select order_id, count(order_id) as "Number of Orders",sum(pizza_quantity) as "Pizzas Sold",
2 sum(order_amount) as "Total Profit"
3 from order_details group by order_id;
Order ID Number of Orders Pizzas Sold Total Profit
---------- ---------------- ----------- ------------
1 2 5 105
6 1 2 20
2 2 6 165
4 2 12 130
5 2 6 80
8 1 7 140
3 2 4 80
7 1 6 120
9 1 8 120
10 1 9 270
10 rows selected.
有什么方法可以显示以货币格式设置的“总利润”列?
答案 0 :(得分:2)
您可以尝试使用to_char()
。
...
to_char(sum(order_amount), '$9,999,999.00')
...
答案 1 :(得分:1)
您可以使用数字格式建模元素作为FML9,999.99
函数的第二个自变量$9G990D00
或to_char
,如以下示例所示:
SQL> alter session set nls_currency='$';
SQL> alter session set NLS_NUMERIC_CHARACTERS = '.,';
SQL> with tab(order_ID,order_amount) as
(
select 1,255.51 from dual union all
select 1,750.33 from dual union all
select 6,420.39 from dual union all
select 2,790.00 from dual union all
select 2,375.05 from dual
)
select to_char(sum(order_amount),'FML9,999.99') as "Total Profit"
from tab
group by order_ID
union all
select to_char(sum(order_amount),'$9G990D00') as "Total Profit"
from tab
group by order_ID
Total Profit
$1,005.84
$420.39
$1,065.05
$1,005.84
$420.39
$1,065.05
答案 2 :(得分:1)
如果您的问题是关于SQL * Plus列格式的,则问题是您为名为order_amount
的列定义了一种格式,但是在查询中它被称为"Total Profit"
:
column order_amount heading "Order Amount" format '$9,999,999.00'
如果更改查询以使所需的列命名为order_amount
,则将使用定义的格式:
select order_id, count(order_id) as "Number of Orders", sum(pizza_quantity) as "Pizzas Sold"
, sum(order_amount) as order_amount
from order_details
group by order_id;
(顺便说一下,除非您的成本为零,否则我怀疑订单总额是否全部为利润,在这种情况下,我想投资于您的比萨饼业务。)