如何替换功能(多个)OUTER APPLY(SELECT * FROM)

时间:2013-03-12 15:30:55

标签: sql-server sql-server-2008 pivot unpivot

适用于Microsoft SQL Server 2008 R2。

问题是

如果我们有几十个Outer Apply(30),那么他们开始工作得相当慢。在Outer Apply的中间,我有一个比简单选择更复杂的东西,一个视图。

详细

我正在编写一种分配给表的属性(在数据库中)。通常,一些表包含对属性表(键,值)的引用。

伪结构看起来像这样:

DECLARE @Lot TABLE (
LotId INT PRIMARY KEY IDENTITY, 
SomeText VARCHAR(8))

INSERT INTO @Lot
OUTPUT INSERTED.*
VALUES ('Hello'), ('World')

DECLARE @Attribute TABLE(
AttributeId INT PRIMARY KEY IDENTITY, 
LotId INT, 
Val VARCHAR(8),
Kind VARCHAR(8))

INSERT INTO @Attribute
OUTPUT INSERTED.* VALUES 
(1, 'Foo1', 'Kind1'), (1, 'Foo2', 'Kind2'), 
(2, 'Bar1', 'Kind1'), (2, 'Bar2', 'Kind2'), (2, 'Bar3', 'Kind3')

LotId       SomeText
----------- --------
1           Hello
2           World

AttributeId LotId       Val      Kind
----------- ----------- -------- --------
1           1           Foo1     Kind1
2           1           Foo2     Kind2
3           2           Bar1     Kind1
4           2           Bar2     Kind2
5           2           Bar3     Kind3

我现在可以运行查询,例如:

SELECT 
[l].[LotId]
  , [SomeText]
  , [Oa1].[AttributeId]
  , [Oa1].[LotId]
  , 'Kind1Val' = [Oa1].[Val]
  , [Oa1].[Kind]
  , [Oa2].[AttributeId]
  , [Oa2].[LotId]
  , 'Kind2Val' = [Oa2].[Val]
  , [Oa2].[Kind]
  , [Oa3].[AttributeId]
  , [Oa3].[LotId]
  , 'Kind3Val' = [Oa3].[Val]
  , [Oa3].[Kind]  
FROM @Lot AS l
OUTER APPLY(SELECT * FROM @Attribute AS la WHERE la.[LotId] = l.[LotId] AND la.[Kind] = 'Kind1') AS Oa1
OUTER APPLY(SELECT * FROM @Attribute AS la WHERE la.[LotId] = l.[LotId] AND la.[Kind] = 'Kind2') AS Oa2
OUTER APPLY(SELECT * FROM @Attribute AS la WHERE la.[LotId] = l.[LotId] AND la.[Kind] = 'Kind3') AS Oa3


LotId       SomeText AttributeId LotId       Kind1Val Kind     AttributeId LotId       Kind2Val Kind     AttributeId LotId       Kind3Val Kind
----------- -------- ----------- ----------- -------- -------- ----------- ----------- -------- -------- ----------- ----------- -------- --------
1           Hello    1           1           Foo1     Kind1    2           1           Foo2     Kind2    NULL        NULL        NULL     NULL
2           World    3           2           Bar1     Kind1    4           2           Bar2     Kind2    5           2           Bar3     Kind3

简单方式获取属性值的数据透视表以及没有属性如Kind3的Lot行的结果。 我知道微软PIVOT并不简单,不适合这里。

最后,什么会更快,并会给出相同的结果?

1 个答案:

答案 0 :(得分:5)

为了获得结果,您可以取消隐藏,然后转移数据。

有两种方法可以执行此操作。首先,您可以使用UNPIVOTPIVOT函数:

select *
from
(
    select LotId,
        SomeText,
        col+'_'+CAST(rn as varchar(10)) col,
        value
    from
    (
        select l.LotId, 
            l.SomeText,
            cast(a.AttributeId as varchar(8)) attributeid,
            cast(a.LotId as varchar(8)) a_LotId,
            a.Val,
            a.Kind,
            ROW_NUMBER() over(partition by l.lotid order by a.attributeid) rn
        from @Lot l
        left join @Attribute a
            on l.LotId = a.LotId
    ) src
    unpivot
    (
        value
        for col in (attributeid, a_Lotid, val, kind)
    ) unpiv
) d
pivot
(
    max(value)
    for col in (attributeid_1, a_LotId_1, Val_1, Kind_1,
                attributeid_2, a_LotId_2, Val_2, Kind_2,
                attributeid_3, a_LotId_3, Val_3, Kind_3)
) piv

请参阅SQL Fiddle with Demo

或者从SQL Server 2008+开始,您可以使用带有CROSS APPLY子句的VALUES来取消数据的转出:

select *
from
(
    select LotId,
        SomeText,
        col+'_'+CAST(rn as varchar(10)) col,
        value
    from
    (
        select l.LotId, 
            l.SomeText,
            cast(a.AttributeId as varchar(8)) attributeid,
            cast(a.LotId as varchar(8)) a_LotId,
            a.Val,
            a.Kind,
            ROW_NUMBER() over(partition by l.lotid order by a.attributeid) rn
        from @Lot l
        left join @Attribute a
            on l.LotId = a.LotId
    ) src
    cross apply
    (
        values ('attributeid', attributeid),('LotId', a_LotId), ('Value', Val), ('Kind', Kind)
    ) c (col, value)
) d
pivot
(
    max(value)
    for col in (attributeid_1, LotId_1, Value_1, Kind_1,
                attributeid_2, LotId_2, Value_2, Kind_2,
                attributeid_3, LotId_3, Value_3, Kind_3)
) piv

请参阅SQL Fiddle with Demo

unpivot进程为每个LotIDSomeText获取多列,并将其转换为包含结果的行:

| LOTID | SOMETEXT |           COL | VALUE |
--------------------------------------------
|     1 |    Hello | attributeid_1 |     1 |
|     1 |    Hello |       LotId_1 |     1 |
|     1 |    Hello |       Value_1 |  Foo1 |
|     1 |    Hello |        Kind_1 | Kind1 |
|     1 |    Hello | attributeid_2 |     2 |

我在内部子查询中添加了row_number(),用于创建要旋转的新列名。创建名称后,可以将数据透视表应用于新列,以显示最终结果

这也可以使用动态SQL来完成:

DECLARE @cols AS NVARCHAR(MAX),
    @query  AS NVARCHAR(MAX)

select @cols = STUFF((SELECT ',' + QUOTENAME(col+'_'+rn) 
                    from 
                    (
                      select 
                        cast(ROW_NUMBER() over(partition by l.lotid order by a.attributeid) as varchar(10)) rn
                      from Lot l
                      left join Attribute a
                          on l.LotId = a.LotId
                    ) t
                    cross apply (values ('attributeid', 1),
                                 ('LotId', 2), 
                                 ('Value', 3), 
                                 ('Kind', 4)) c (col, so)
                    group by col, rn, so
                    order by rn, so
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')


set @query = 'SELECT LotId,
                    SomeText,' + @cols + ' 
             from 
             (
                select LotId,
                    SomeText,
                    col+''_''+CAST(rn as varchar(10)) col,
                    value
                from
                (
                    select l.LotId, 
                        l.SomeText,
                        cast(a.AttributeId as varchar(8)) attributeid,
                        cast(a.LotId as varchar(8)) a_LotId,
                        a.Val,
                        a.Kind,
                        ROW_NUMBER() over(partition by l.lotid order by a.attributeid) rn
                    from Lot l
                    left join Attribute a
                        on l.LotId = a.LotId
                ) src
                cross apply
                (
                    values (''attributeid'', attributeid),(''LotId'', a_LotId), (''Value'', Val), (''Kind'', Kind)
                ) c (col, value)
            ) x
            pivot 
            (
                max(value)
                for col in (' + @cols + ')
            ) p '

execute(@query)

请参阅SQL Fiddle with Demo

所有三个版本都会给出相同的结果:

| LOTID | SOMETEXT | ATTRIBUTEID_1 | LOTID_1 | VALUE_1 | KIND_1 | ATTRIBUTEID_2 | LOTID_2 | VALUE_2 | KIND_2 | ATTRIBUTEID_3 | LOTID_3 | VALUE_3 | KIND_3 |
-----------------------------------------------------------------------------------------------------------------------------------------------------------
|     1 |    Hello |             1 |       1 |    Foo1 |  Kind1 |             2 |       1 |    Foo2 |  Kind2 |        (null) |  (null) |  (null) | (null) |
|     2 |    World |             3 |       2 |    Bar1 |  Kind1 |             4 |       2 |    Bar2 |  Kind2 |             5 |       2 |    Bar3 |  Kind3 |