+ -
当前位置:首页 → 问答吧 → 高分求解~~

高分求解~~

时间:2011-12-16

来源:互联网

数据表内容为:
no sub_no sign  
1 1
2 1
3 1
4 1
5 1
6 1
7 1
8 1
9 1
9 2
10 1
10 2
10 3
10 4
11 1
11 2
11 3
12 1
13 1
13 2
14 1
14 2

要求: 当主ID下只有一条记录时, sign 显示为 空
  当主ID下有多条子ID时, 最小的子ID 的sign 显示为 "┓"
  最大的子ID 的sign 显示为 "┛"
  中间的子ID 的sign 显示为 "┃"
  一句SQL。。。高分求解~ 


作者: cuki20   发布时间: 2011-12-16


select a.no, a.sub_no, decode(sign(a.no - a.min_sub), 0, '┓', '|')
  from (select no,
  sub_no,
  MIN(sub_no) OVER(PARTITION BY no) AS min_sub,
  MAX(sub_no) OVER(PARTITION BY no) AS max_sub
  from table_test) a
 where a.no <> a.max_sub
union all
select a.no, a.sub_no, '┛'
  from (select no,
  sub_no,
  MIN(sub_no) OVER(PARTITION BY no) AS min_sub,
  MAX(sub_no) OVER(PARTITION BY no) AS max_sub
  from table_test) a
 where a.no = a.max_sub


说明: 从你给的数据看,在no只有一个的情况下应该怎么显示你并没有定义规则,我上门的语句只有一个的情况下都按照最小子id算.
另外

作者: fudaliang1999   发布时间: 2011-12-17


select a.no, a.sub_no, decode(sign(a.no - a.min_sub), 0, '┓', '|')
  from (select no,
  sub_no,
  MIN(sub_no) OVER(PARTITION BY no) AS min_sub,
  MAX(sub_no) OVER(PARTITION BY no) AS max_sub
  from table_test) a
 where a.no <> a.max_sub
union
select a.no, a.sub_no, '┛'
  from (select no,
  sub_no,
  MIN(sub_no) OVER(PARTITION BY no) AS min_sub,
  MAX(sub_no) OVER(PARTITION BY no) AS max_sub
  from table_test) a
 where a.no = a.max_sub
  and a.no <> a.min_sub
说明: 从数据上看,需求中并没有说明如果id只有一个子id 该如何显示,我假设都按照最小显示。

作者: fudaliang1999   发布时间: 2011-12-17

select no,sub_no ,(case
  when cntid = 1 then
  ' '
  else
  (case sub_no
  when maxid then
  '┛'
  when minid then
  '┓'
  else
  '|'
  end)
  end)
  from (select no,
  sub_no,
  max(sub_no) over(partition by no) maxid,
  min(sub_no) over(partition by no) minid,
  count(sub_no) over(partition by no) cntid
  from t)

作者: jdsnhan   发布时间: 2011-12-17

热门下载

更多