2

我有一张像

ID     ENVI              SERVER               GROUP         ACTIVE
==     ====              ======              ======         ======
1      Developent        AREGION_1            A               1
2      Developent        AREGION_2            A               1
3      Developent        AREGION_3            A               1
4      Developent        BREGION_1            B               1
5      Developent        BREGION_2            B               1
6      Developent        BREGION_3            B               1
7      Developent        CREGION_1            C               1
8      Developent        CREGION_3            C               1
9      Developent        A1REGION             A               1
10     Developent        A2REGION             A               1
11     Developent        ABCREGION            A               1

我需要编写一个带有输入参数的 sp ENVI,它将返回如下结果

ENVI                A             B              C
====                =========     =========      =========
Development         AREGION_1     BREGION_1      CREGION_1  (All servers Ending with _1)
Development         AREGION_2     BREGION_2                 (All servers Ending with _2)
Development         AREGION_3     BREGION_3      CREGION_3  (All servers Ending with _3)
Development         A1REGION     
Development         A2REGION     
Development         ABCREGION

条件是所有以数字结尾的服务器_都应该排在第一位。如果任何一列没有该行的值,则该字段应为 null 或为空。任何组下具有随机名称的任何服务器都必须放在该组下的最后。

请帮我创建sp

提前致谢

4

2 回答 2

2

您没有指定您使用的 sybase 版本,此答案假定您有一个可以访问窗口函数的版本。Sybase 没有 PIVOT 函数,因此您必须使用带有CASE表达式的聚合函数来复制它。

下面的代码应该得到你想要的结果:

select envi,
  max(case when serverGroup = 'A' then server end) as A,
  max(case when serverGroup = 'B' then server end) as B,
  max(case when serverGroup = 'C' then server end) as C
from
(
  select envi,
    server,
    serverGroup,
    case 
      when frn > rn then frn
      else rn
    end rn    
  from
  (
    select envi,
      server, 
      serverGroup,
      case 
        when charindex('_', SERVER) = 0
        then 0
        else substring(SERVER, charindex('_', SERVER)+1, len(SERVER))
      end frn, 
      row_number() over(partition by envi, serverGroup 
                        order by substring(SERVER, charindex('_', SERVER), len(SERVER)+1)) rn
    from ytable
  ) d
) x
group by envi, rn
order by rn;

请参阅SQL Fiddle with Demo。注意:演示是在 SQL Server 上。

这给出了结果:

|       ENVI |         A |         B |         C |
--------------------------------------------------
| Developent | AREGION_1 | BREGION_1 | CREGION_1 |
| Developent | AREGION_2 | BREGION_2 |    (null) |
| Developent | AREGION_3 | BREGION_3 | CREGION_3 |
| Developent |  A1REGION |    (null) |    (null) |
| Developent |  A2REGION |    (null) |    (null) |
| Developent | ABCREGION |    (null) |    (null) |
于 2013-04-04T10:47:17.213 回答
0

这是一个聚合查询,尽管您的最终结果不包括聚合列之一(修改后的服务器名称)。

select envi,
       max(case when group = 'A' then server end) as A,
       max(case when group = 'B' then server end) as B,
       max(case when group = 'C' then server end) as C
from t
group by envi,
         (case when server like '%[_]%' and server not like '%[_]%[^0-9]%'
               then left(server, charindex('_', server) - 1)
               else server
          end)

like逻辑查找具有下划线且下划线后仅包含数字的服务器名称。

于 2013-04-04T10:46:50.483 回答