2

这是我在这里的第一篇文章。我正在处理支票,我在 Oracle 11g 数据库中有这个

 WITH cheques AS (
    SELECT 1 ch_no,'U' ch_status FROM dual UNION ALL
    SELECT 2 ch_no,'U' ch_status FROM dual UNION ALL
    SELECT 3 ch_no,'T' ch_status FROM dual UNION ALL
    SELECT 4 ch_no,'T' ch_status FROM dual UNION ALL
    SELECT 5 ch_no,'U' ch_status FROM dual UNION ALL
    SELECT 6 ch_no,'U' ch_status FROM dual UNION ALL
    SELECT 7 ch_no,'C' ch_status FROM dual UNION ALL
    SELECT 8 ch_no,'U' ch_status FROM dual UNION ALL
    SELECT 9 ch_no,'U' ch_status FROM dual UNION ALL
    SELECT 10 ch_no,'C' ch_status FROM dual UNION ALL
    SELECT 11 ch_no,'U' ch_status FROM dual UNION ALL
    SELECT 12 ch_no,'U' ch_status FROM dual UNION ALL
    SELECT 13 ch_no,'X' ch_status FROM dual UNION ALL
    SELECT 14 ch_no,'X' ch_status FROM dual UNION ALL
    SELECT 15 ch_no,'T' ch_status FROM dual UNION ALL
    SELECT 16 ch_no,'U' ch_status FROM dual UNION ALL
    SELECT 17 ch_no,'U' ch_status FROM dual UNION ALL
    SELECT 18 ch_no,'I' ch_status FROM dual UNION ALL
    SELECT 19 ch_no,'I' ch_status FROM dual UNION ALL
    SELECT 20 ch_no,'U' ch_status FROM dual
 )

我希望以这种方式获得它们:

Status  Min Max
U   1   2
U   5   6
U   8   9
U   11  12
U   16  17
U   20  20
C   7   7
C   10  10
T   3   4
T   15  15
X   13  14
I   18  19

因此,在 Reporting Services 2008 中,我将能够像这样显示它们:

U       C       T       X       I   
Min Max Min Max Min Max Min Max Min Max
1   2   7   7   3   4   13  14  18  19
5   6   10  10  15  15              
8   9                               
11  12                              
16  17                              
20  20      

这里的问题是如何获得这个,如何根据他们的状态获得每张支票的最小值和最大值?我已经搜索过,但我找不到任何东西。

对不起我的英语不好。

谢谢!

4

1 回答 1

4

这不是一个简单的问题,需要许多步骤。

  1. 用于lag在每个连续块的开头设置一个“标志ch_noch_status
  2. 用作sum分析order by(将默认窗口更改为unbounded preceeding)为每个连续块提供唯一标识符
  3. 最后使用正则group by计算每个块的min&max

查询

WITH cheques AS (
    SELECT 1 ch_no,'U' ch_status FROM dual UNION ALL
    SELECT 2 ch_no,'U' ch_status FROM dual UNION ALL
    SELECT 3 ch_no,'T' ch_status FROM dual UNION ALL
    SELECT 4 ch_no,'T' ch_status FROM dual UNION ALL
    SELECT 5 ch_no,'U' ch_status FROM dual UNION ALL
    SELECT 6 ch_no,'U' ch_status FROM dual UNION ALL
    SELECT 7 ch_no,'C' ch_status FROM dual UNION ALL
    SELECT 8 ch_no,'U' ch_status FROM dual UNION ALL
    SELECT 9 ch_no,'U' ch_status FROM dual UNION ALL
    SELECT 10 ch_no,'C' ch_status FROM dual UNION ALL
    SELECT 11 ch_no,'U' ch_status FROM dual UNION ALL
    SELECT 12 ch_no,'U' ch_status FROM dual UNION ALL
    SELECT 13 ch_no,'X' ch_status FROM dual UNION ALL
    SELECT 14 ch_no,'X' ch_status FROM dual UNION ALL
    SELECT 15 ch_no,'T' ch_status FROM dual UNION ALL
    SELECT 16 ch_no,'U' ch_status FROM dual UNION ALL
    SELECT 17 ch_no,'U' ch_status FROM dual UNION ALL
    SELECT 18 ch_no,'I' ch_status FROM dual UNION ALL
    SELECT 19 ch_no,'I' ch_status FROM dual UNION ALL
    SELECT 20 ch_no,'U' ch_status FROM dual
 )
select ch_status, min(ch_no), max(ch_no)
from( select ch_no, ch_status, sum(changed_flag) over (order by ch_no) as grp
      from( select ch_no, ch_status, 
                   decode(ch_status,lag(ch_status) over(order by ch_no),0,1) 
                     as changed_flag
            from cheques ) )
group by ch_status, grp
order by ch_status, min(ch_no)

结果

| CH_STATUS | MIN(CH_NO) | MAX(CH_NO) |
---------------------------------------
|         C |          7 |          7 |
|         C |         10 |         10 |
|         I |         18 |         19 |
|         T |          3 |          4 |
|         T |         15 |         15 |
|         U |          1 |          2 |
|         U |          5 |          6 |
|         U |          8 |          9 |
|         U |         11 |         12 |
|         U |         16 |         17 |
|         U |         20 |         20 |
|         X |         13 |         14 |

SQL小提琴在这里

于 2012-12-14T18:08:42.853 回答