【问题标题】:SYS_CONNECT_BY_PATH in SQL Server dynamical useSQL Server 动态使用中的 SYS_CONNECT_BY_PATH
【发布时间】:2014-11-24 00:18:10
【问题描述】:

我目前正在进行从 Oracle Exadata (R.I.P :( ) 到 SQL Server 2008 的迁移项目。

我在 Oracle 中有一些查询,其中包含很好的 SYS_CONNECT_BY_PATH 函数。

我的问题:我有当前的数据集

ORDER_ID   ORDER_GROUP_ID   OPERATOR_ID   GROUP_NAME    VALUE_ID    DESCRIPTION
--------------------------------------------------------------------------------------------------------------
1             10000            3          USER_ID       not null    'panel_id or msisdn_anonym of user'
2             10000            3          MISSING_FLAG     0        'data for extrapolation are not missing' 
3             10000            3          MISSING_FLAG     1        'data for extrapolation are missing'
5             10000            3          PANEL_FLAG       0        'source of user: no panel'
5             10000            3          PANEL_FLAG       1        'source of user: panel'
6             10000            3          ACTIVE_FLAG      0        'not active user'
7             10000            3          ACTIVE_FLAG      1        'active user'
1             10000            5          USER_ID      not null     'panel_id or msisdn_anonym of user'
2             10000            5          MISSING_FLAG     0        'data for extrapolation are not missing'
3             10000            5          MISSING_FLAG     1        'data for extrapolation are missing'  
5             10000            5          PANEL_FLAG       0        'source of user: no panel'
5             10000            5          PANEL_FLAG       1        'source of user: panel'
6             10000            5          ACTIVE_FLAG      0        'not active user'
7             10000            5          ACTIVE_FLAG      1        'active user'

我需要这个:

ORDER_GROUP_ID  ORDER_ID    OPERATOR_ID GROUP_NAME  VALUE_DESCRIPTION
---------------------------------------------------------------------------------------------
10000           1           3             USER_ID               [not null='panel_id or msisdn_anonym of user']
10000           3           3             MISSING_FLAG          [0='data for extrapolation are not missing'] [1='data for extrapolation are missing']
10000           5           3             PANEL_FLAG            [0='source of user: no panel'] [1='source of user: panel']
10000           7           3             ACTIVE_FLAG           [0='not active user'] [1='active user']
10000           1           5             USER_ID               [not null='panel_id or msisdn_anonym of user']
10000           3           5             MISSING_FLAG          [0='data for extrapolation are not missing'] [1='data for extrapolation are missing']
10000           5           5             PANEL_FLAG            [0='source of user: no panel'] [1='source of user: panel']
10000           7           5             ACTIVE_FLAG           [0='not active user'] [1='active user']

这是我们 Exadata 中使用的当前代码:

SELECT
  order_group_id, order_id, operator_id, group_Name, value_description
FROM 
   (SELECT
        order_id, operator_id, group_Name, level
        , seq, cnt
        , trim (REPLACE (SYS_CONNECT_BY_PATH(value_description, '#'),  '#', ' ' )) AS value_description
        , order_group_id
    FROM 
        (SELECT
            order_id, operator_id, group_Name, VALUE_ID, description, '['||VALUE_ID||'='||description||']' AS value_description
      , row_number() OVER ( PARTITION BY operator_id, group_Name ORDER BY VALUE_ID, description ) seq
            , count(*) OVER ( PARTITION BY operator_id, group_Name ) cnt
            , order_group_id
         FROM 
            NIS_MDM.EPO_FUS_USER_GROUPS_V)
    where level = cnt
    start with seq = 1
    CONNECT BY
    PRIOR operator_id = operator_id and prior group_Name = group_Name
    AND PRIOR seq = seq - 1
  )
  order by operator_id, order_id

我尝试使用 STUFF 功能甚至 Pivot,但我无法找到正确的解决方案。

如果有人能帮助我,我会很高兴。 我觉得我用不同的语言搜索了整个互联网,但没有人解决或遇到类似的问题。

【问题讨论】:

  • 查看我的答案并告诉我们...
  • 干得好 Krishnraj Rana 工作完美!竖起大拇指

标签: sql-server common-table-expression hierarchical


【解决方案1】:

嗯,你可以试试这个查询。

签入SQL Fiddle

SELECT order_group_id
    ,order_id
    ,operator_id
    ,group_Name
    ,value_description
FROM (
    (
        SELECT order_group_id
            ,order_id
            ,seq = row_number() OVER (
                ORDER BY operator_id
                    ,group_Name
                )
        FROM MyTab
        ) tab INNER JOIN (
        SELECT operator_id
            ,group_Name
            ,row_number() OVER (
                ORDER BY operator_id
                    ,group_Name
                ) seq
            ,STUFF((
                    SELECT ' ' + '[' + VALUE_ID + '=' + [description] + ']'
                    FROM MyTab v
                    WHERE v.operator_id = A.OPERATOR_ID
                        AND v.GROUP_NAME = A.GROUP_NAME
                    FOR XML PATH('')
                        ,TYPE
                    ).value('.', 'NVARCHAR(MAX)'), 1, 1, '') AS value_description
        FROM (
            SELECT order_id
                ,operator_id
                ,group_Name
                ,VALUE_ID
                ,description
                ,order_group_id
            FROM MyTab
            ) a
        GROUP BY operator_id
            ,group_Name
        ) t ON tab.seq = t.seq
    )

【讨论】:

    【解决方案2】:

    我已经通过创建数据库函数解决了这个问题

    创建一个类似 "ufn_GetParentPath" 的函数,使用它代替 "SYS_CONNECT_BY_PATH" 传递 order_id 作为参数。

    CREATE FUNCTION [dbo].[ufn_GetParentPath] ( @pCurrentNodeID    INT )
    RETURNS VARCHAR(1000)
      AS
    BEGIN
    
    DECLARE @vCurrentNodeName     VARCHAR(50)
    DECLARE @vParentID            INT
    
    IF @pCurrentNodeID IS NULL OR @pCurrentNodeID = 0
        RETURN NULL
    
    SELECT @vCurrentNodeName = [VALUE_DESCRIPTION], @vParentID = [operator_id]
    FROM [NIS_MDM].[EPO_FUS_USER_GROUPS_V]
    WHERE [order_id] = @pCurrentNodeID
    
    RETURN ISNULL([dbo].[ufn_GetParentPath] ( @vParentID ) + '/', '') + @vCurrentNodeName
    
    END
    GO
    

    【讨论】:

      猜你喜欢
      • 2012-10-09
      • 2017-10-07
      • 2015-07-25
      • 1970-01-01
      • 2021-05-13
      • 1970-01-01
      • 2023-04-09
      • 2011-07-05
      • 2018-03-11
      相关资源
      最近更新 更多