【发布时间】:2020-01-09 00:46:14
【问题描述】:
在第一个 SELECT 语句中,报告获取所有具有发货活动的产品的 inv 详细信息。然后有一个 UNION 连接另一个 SELECT 语句以获取上一个日历年没有活动的产品。
但是,在第二个 SELECT 语句中返回的记录有多个 header_id,因此是多行……而不是像第一个 SELECT 语句那样单行。你知道如何在第二个 SELECT 语句中只提取每条记录的第一个 header_id 吗?
代码和示例结果集如下。在数据中,产品 #7 应仅列出 header_id 1372288 的行,这是输入数据库的最后一个 ID。
select 3 sort_key, header_Id,location_id,nlasinv.product,
start_inv,produced produced_inv,stored,from_stock,shipped,
(start_inv + produced + stored) - (from_stock + shipped) end_inv,nlas_ops_mtd_prodsize(111,nlasinv.product,'31-DEC-19'), nlas_ops_mtd_shipsize(111,nlasinv.product,'31-DEC-19'),nlas_ops_ytd_prodsize(111,nlasinv.product,'31-DEC-19'), nlas_ops_ytd_shipsize(111,nlasinv.product,'31-DEC-19')
from nlas_header inv,
nlas_inventory nlasinv
where nlasinv.header_id = 1372168
and inv.id = nlasinv.header_id
union
select distinct
3 sort_key,header_Id,location_id,nlasinv.product,
start_inv,produced produced_inv,stored,from_stock,shipped,
(start_inv + produced + stored) - (from_stock + shipped) end_inv,nlas_ops_mtd_prodsize(111,nlasinv.product,'31-DEC-19'),
nlas_ops_mtd_shipsize(111,nlasinv.product,'31-DEC-19'),nlas_ops_ytd_prodsize(111,nlasinv.product,'31-DEC-19'),
nlas_ops_ytd_shipsize(111,nlasinv.product,'31-DEC-19')
from
nlas_inventory nlasinv,
nlas_header hdr
where
nlasinv.header_id = hdr.id
and hdr.location_id = 409
and hdr.observation_date >= trunc(to_date('31-DEC-19','dd-mon-rr'),'year')
and nlasinv.product not in
(select distinct product from
nlas_header h,
nlas_inventory i
where i.header_id = 1372168)
order by product, header_id des
c
【问题讨论】:
-
如何定义“第一行”?最小日期、值、其他?
-
“第一行”将是具有最高 header_id 的行...相当于最近的记录。
-
那么戈登的回答是正确的。在他的回答中,
t代表您拥有的整个查询(除了应该放在解决方案之后(最后)的最后一个ORDER BY子句。