我在Oracle中生成一个脚手架视图,用于我需要的其他工作。我一般都是在Oracle和SQL中沾沾自喜。
我想得到这个:
GridView
由此:
DATE_ I II III
13-DEC-16 CATEGORY SUBCATEGORY SITE_NAME_A
13-DEC-16 CATEGORY SUBCATEGORY SITE_NAME_B
13-DEC-16 CATEGORY SUBCATEGORY SITE_NAME_C
14-DEC-16 CATEGORY SUBCATEGORY SITE_NAME_A
14-DEC-16 CATEGORY SUBCATEGORY SITE_NAME_B
14-DEC-16 CATEGORY SUBCATEGORY SITE_NAME_C
...
这当然有效,如果子查询返回单个记录,但会抛出太多记录"什么时候没有错误。我怎么能解决这个问题?我想获得额外的记录,因为它们是新的网站名称,但不知道如何在视图中插入它们。
谢谢!
编辑:为了澄清,供应商可能有多个vendor_site,当太多记录错误合并时。 还注意到输出示例是错误的,纠正了它。
答案 0 :(得分:0)
可能您正在寻找那个(SELECT UNIQUE VENDOR_SITE_CODE FROM AP_INVOICES_V WHERE VENDOR_NAME = 'SOMETHING') AS III
答案 1 :(得分:0)
如果您需要新的站点名称作为附加行,那么 而不是使其成为子查询,将其与主表连接并使用distinct来避免重复记录。根据需要添加或删除WHERE子句
SELECT distinct TRUNC (SYSDATE - (3) + (LEVEL)) AS DATE_,
'CATEGORY' AS I,
'SUBCATEGORY' AS II,
VENDOR_SITE_CODE AS III,
VENDOR_NAME,
LEVEL
FROM DUAL,AP_INVOICES_V WHERE VENDOR_NAME = 'SOMETHING'
CONNECT BY LEVEL <= (SYSDATE - (SYSDATE - (3)))
如果要在现有行中添加新站点名称,请使用Oracle分析函数LISTAGG和VENDOR_NAME分区
SELECT TRUNC (SYSDATE - (3) + (LEVEL)) AS DATE_,
'CATEGORY' AS I,
'SUBCATEGORY' AS II,
( SELECT DISTINCT LISTAGG(VENDOR_SITE_CODE,',') WITHIN GROUP (ORDER BY VENDOR_SITE_CODE) OVER (PARTITION BY VENDOR_NAME) FROM AP_INVOICES_V WHERE VENDOR_NAME='VENDOR1') AS III
FROM DUAL
CONNECT BY LEVEL <= (SYSDATE - (SYSDATE - (3)))