sql - 结合创建的表和 Listagg 查询
问题描述
目前我有两段代码,一段用于创建表格,另一段用于列出表格。代码看起来像这样:
Create table NewTable nologging as
select distinct x as customerNumber,
y as items bought,
z as prices
from
customerNum,
numItems,
itemPrice
where (z not in ('2.00','3.00','NA'))
然后是一个 Listagg
create table formattingSection nologging as
select newTable.customerNumber, listagg(newTable.bought,',') within group (order
by bought) as boughtdesc
from newTable
group by customerNumber
是否可以将这两个部分合并为一个部分?这样我就没有2张桌子了吗?
解决方案
像这样的东西,我想:
create table formattingSection nologging as
select
customerNumber,
listagg(bought, ',') within group (order by bought) as boughtdesc
from
-- instead of "newTable" itself, use SELECT statement from "CREATE TABLE newTable"
(select distinct x as customerNumber,
y as items bought,
z as prices
from
customerNum,
numItems,
itemPrice
where (z not in ('2.00', '3.00', 'NA'))
-- and ... --> you should join those tables, somehow. Otherwise, Cartesian product
-- will be created, and that's probably not what you want.
)
group by customerNumber;
除了我在代码中写的注释之外,“z”列似乎代表“价格”,并且您似乎将它存储到一个VARCHAR2
列中。我建议你不要这样做;使用NUMBER
数据类型。为什么?因为没有什么能阻止您将价格输入为“x.%i”或“2=&3”。如果因为“NA”而做出这样的决定,没问题 - 你只需将其留空,其值为 NULL。出于显示目的,您需要DECODE
这样一个值:decode(z, null, 'NA', z)
.
推荐阅读
- python - 如何根据python中的两个条件对数组进行排序
- arrays - 适配windows编译器,感觉很可怜。问题是什么
- python - 如何设置pytest类的类实例变量
- javascript - 使用 lodash 过滤器我想用零替换 null
- spring-boot - Spring Boot 管理属性文件
- ios - 如何从 UITextField Delegate 获取当前输入的文本?
- php - 将商品添加到购物车并更改数量显示以下错误。如何修复它
- validation - 是的-密码验证
- flutter - 颤振画布:如何避免在图像区域之外绘制
- mongodb - MongoDB在哪里存储索引数据?