ORACLE查询返回数据集的第一行

时间:2016-08-09 14:02:46

标签: sql oracle analytics

我正在编写一个查询,该查询将返回一组分组数据的第一行。我尝试使用OVER PARTITION BY子句,但不知怎的,我没有得到理想的结果:

select  row_number() OVER(PARTITION BY leafv , value_group , l1d ,l2d  ,l3d  ,l4d  ,l5d  ,l6d ,l7d  
order by leafv , value_group , l1d ,l2d  ,l3d  ,l4d  ,l5d  ,l6d ,l7d  
) MYROWNUM ,
S.* 
from
(SELECT 
  NVL(CASE
    WHEN (SELECT parameter_value
      FROM V_CONFIG_PARAMETERS
      WHERE rownum       = 1
      AND parameter_name = 'MOMENT_ITEM_NUMBER_TYPE') = 'S'
    THEN sku
    WHEN (SELECT parameter_value
      FROM V_CONFIG_PARAMETERS
      WHERE rownum       = 1
      AND parameter_name = 'MOMENT_ITEM_NUMBER_TYPE') = 'U'
   THEN upc
    WHEN (SELECT parameter_value
      FROM V_CONFIG_PARAMETERS
      WHERE rownum       = 1
      AND parameter_name = 'MOMENT_ITEM_NUMBER_TYPE') = 'C'
    THEN CUSTOM_PRODUCT_CODE
  END,'NULLVALUE') leafv,
  nvl(chain_name,'NULLVALUE') value_group,
  nvl(level_10_description ,'NULLVALUE') l10d,
  nvl(level_1_description,'NULLVALUE') l1d,
  nvl(level_2_description,'NULLVALUE') l2d,
  nvl(level_3_description,'NULLVALUE') l3d,
  nvl(level_4_description,'NULLVALUE') l4d,
  nvl(level_5_description,'NULLVALUE') l5d,
  nvl(level_6_description,'NULLVALUE') l6d,
  nvl(level_7_description,'NULLVALUE') l7d,
  nvl(level_8_description,'NULLVALUE') l8d,
  nvl(level_9_description,'NULLVALUE') l9d
  FROM t_product_catalog_flat
  order by leafv , value_group , l1d ,l2d  ,l3d  ,l4d  ,l5d  ,l6d ,l7d  ,l8d  ,l9d  ,l10d ) S
  order by MYROWNUM

预期成果: 每组数据的1行,具有相同的列组值(leafv,value_group,l1d,l2d,l3d,l4d,l5d,l6d,l7d,l8d,l9d,l10d)

但是我得到了一些奇怪的结果,这与我的期望并不相符。 请建议可能的解决方案

2 个答案:

答案 0 :(得分:0)

此查询超过DBA_TAB_COLS,并按表名称和数据类型分组。它会拉回第一个分组的行。

希望这对于你想要完成的事情并不过分简单化。

select *
from (
  select  t.TABLE_NAME, t.DATA_TYPE
  from dba_tab_cols t
  where t.OWNER = 'SYS'
  group by  t.TABLE_NAME, t.DATA_TYPE
  order by t.TABLE_NAME, t.DATA_TYPE
)
where rownum = 1

答案 1 :(得分:0)

此查询返回预期结果:

SELECT * FROM (SELECT row_number()OVER(由leafv分区,value_group,l1d,l2d,l3d,l4d,l5d,l6d,l7d顺序由leafv,value_group,l1d,l2d,l3d,l4d,l5d,l6d,l7d)MYROWNUM, S. * 从 (SELECT NVL(SKU,'NULLVALUE')leafv, NVL(chain_name,'NULLVALUE')value_group, NVL(level_10_description,'NULLVALUE')l10d, NVL(level_1_description,'NULLVALUE')l1d, NVL(level_2_description,'NULLVALUE')l2d, NVL(level_3_description,'NULLVALUE')l3d, NVL(level_4_description,'NULLVALUE')l4d, NVL(level_5_description,'NULLVALUE')l5d, NVL(level_6_description,'NULLVALUE')l6d, NVL(level_7_description,'NULLVALUE')l7d, NVL(level_8_description,'NULLVALUE')l8d, NVL(level_9_description,'NULLVALUE')l9d FROM test_partition 按顺序排序, value_group, l1d, l2d, l3d, l4d, l5d, l6d, l7d, l8d, l9d, l10d )S ) MYROWNUM = 1