如何将Cross Apply用于多行?

时间:2015-05-19 11:48:54

标签: sql-server cross-apply

我有这张桌子:

;WITH cte AS (
    SELECT Name='john' , Times=1    
    UNION ALL
    SELECT 'paul' ,2
    UNION ALL
    SELECT 'george' , 3
    UNION ALL
    SELECT 'ringo' , 1
)

enter image description here

我想显示每一行Times次:

John 1
Paul 2
Paul 2
george 3
george 3
george 3
ringo 1

所以我知道如果我写Cross apply就像:

SELECT *
FROM   cte
       CROSS APPLY(
        SELECT 1 AS ca
        UNION 
        SELECT 2
       ) y

然后每行显示2次。

enter image description here

但我不想要2次。我想要Times

问题

如何增强查询功能?

nb:

我想到的一个非智能解决方案是创建一个为Times参数创建n行的udf - 然后在Cross Apply我只做:select * from udf_toTable(Times)

4 个答案:

答案 0 :(得分:6)

;WITH cte AS (
    SELECT Name='john' , Times=1    
    UNION ALL
    SELECT 'paul' , Times=2
    UNION ALL
    SELECT 'george' , Times=3
    UNION ALL
    SELECT 'ringo' , Times=1
),
multi as
(
    select 
        Name, Times, Times as num
    from cte
    union all
    select 
        Name, Times, num - 1
    from multi 
    where num > 1
)
select Name, Times from multi
order by Name

<强>更新

没有递归

;WITH cte AS (
    SELECT Name='john' , Times=1    
    UNION ALL
    SELECT 'paul' , Times=2
    UNION ALL
    SELECT 'george' , Times=3
    UNION ALL
    SELECT 'ringo' , Times=1
)
select cte.*
from cte join 
    -- generate sequence of numbers 1,2 ... MAX(Times)
    (select top (select MAX(Times) from cte) ROW_NUMBER() over (order by object_id) rowNum from sys.objects) t
on cte.Times >= t.rowNum 
order by name

答案 1 :(得分:1)

您不需要使用交叉申请。 使用相当递归的CTE:

;WITH cte AS (
    SELECT Name='john' , Times=1    
    UNION ALL
    SELECT 'paul' , Times=2
    UNION ALL
    SELECT 'george' , Times=3
    UNION ALL
    SELECT 'ringo' , Times=1
)
, res as (
select Name, 1 RowNum
from cte
union all
select cte.Name, res.RowNum+1
from cte
  join res on cte.Name=res.Name
where res.RowNum+1<=cte.Times
)
select res.*, cte.Times
from res
  join cte on cte.Name=res.Name
order by 1, 2

<强>更新 另一个动态最大

;WITH cte AS (
    SELECT Name='john' , Times=1    
    UNION ALL
    SELECT 'paul' , Times=2
    UNION ALL
    SELECT 'george' , Times=3
    UNION ALL
    SELECT 'ringo' , Times=1
), times
AS
(
  select 1 n, MAX(cte.Times) Times
  from cte
  union all
  select t.n+1, t.Times
  from times t
  where t.n+1<=t.Times
)
SELECT 
  c.*
FROM CTE AS c
INNER JOIN times AS t ON c.Times >= t.n
order by 1, 2

答案 2 :(得分:0)

我设法做到了这一点:(因为sys.objects仍然有悲伤的表情)

;WITH cte AS (
    SELECT Name='john' , Times=1    
    UNION ALL
    SELECT 'paul' ,2
    UNION ALL
    SELECT 'george' , 3
    UNION ALL
    SELECT 'ringo' , 1
)  

SELECT *
FROM   cte
       CROSS APPLY(
                select top (cte.Times) 'bla'=1 from sys.objects
       ) y
在查看答案之后

更新:这是一个使用CROSS APPLY:

的解决方案
;WITH cte AS (
    SELECT Name='john' , Times=1    
    UNION ALL
    SELECT 'paul' ,2
    UNION ALL
    SELECT 'george' , 3
    UNION ALL
    SELECT 'ringo' , 1
)   ,  times
AS
(
  select 1 n, MAX(cte.Times) Times
  from cte
  union all
  select t.n+1, t.Times
  from times t
  where t.n+1<=t.Times
)

SELECT *
FROM   cte
       CROSS APPLY(  
                select top (cte.Times) n from times
       ) y

答案 3 :(得分:0)

SELECT A.Name, A.Times
FROM (VALUES
    ('John', 1)
  , ('Paul', 2)
  , ('George', 3)
  , ('Ringo', 1)
) A (Name, Times)
CROSS APPLY(VALUES
    (1), (2), (3)
) B (n)
WHERE A.Times >= B.n
ORDER BY A.Name;

我唯一能想到的缺点就是你必须手动输入数字,但可以通过数字表/ TVF轻松解决:

SELECT A.Name, A.Times
FROM (VALUES
    ('John', 1)
  , ('Paul', 2)
  , ('George', 3)
  , ('Ringo', 1)
) A (Name, Times)
CROSS APPLY dbo.RangeSmallInt(1, A.Times) B;

我在评论中也注意到你遇到了sys.objects的限制,有人发布了生成序列的精彩链接。我的示例中使用的RangeSmallInt函数基于该帖子,并且性能非常高。这是代码:

-- Generate a range of up to 65,536 contiguous BIGINTS
CREATE FUNCTION dbo.RangeSmallInt (
    @n1 BIGINT = NULL
  , @n2 BIGINT = NULL
)
RETURNS TABLE
AS
RETURN (
    WITH Numbers AS (
        SELECT N FROM(VALUES
            (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1) -- 16
          , (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1) -- 32
          , (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1) -- 48
          , (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1) -- 64
          , (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1) -- 80
          , (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1) -- 96
          , (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1) -- 112
          , (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1) -- 128
          , (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1) -- 144
          , (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1) -- 160
          , (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1) -- 176
          , (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1) -- 192
          , (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1) -- 208
          , (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1) -- 224
          , (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1) -- 240
          , (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1), (1) -- 256
        ) V (N)
    )    
    SELECT TOP (
               CASE
                   WHEN @n1 IS NOT NULL AND @n2 IS NOT NULL THEN ABS(@n2 - @n1) + 1
                   ELSE 0
               END
           )
           N = ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) - 1 + CASE WHEN @n1 <= @n2 THEN @n1 ELSE @n2 END
    FROM Numbers A, Numbers B
    WHERE ABS(@n2 - @n1) + 1 < 65537
);

将其扩展为支持INT大小:

-- Generate a range of up to 4,294,967,296 contiguous BIGINTS
CREATE FUNCTION dbo.RangeInt (
    @num1 BIGINT = NULL
  , @num2 BIGINT = NULL
)
RETURNS TABLE
AS
RETURN (
    WITH Numbers(N) AS (
        SELECT N
        FROM dbo.RangeSmallInt(0, 65535)
    )
    SELECT TOP (
               CASE
                   WHEN @num1 IS NOT NULL AND @num2 IS NOT NULL THEN ABS(@num1 - @num2) + 1
                   ELSE 0
               END
           )
           N = ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) + CASE WHEN @num1 <= @num2 THEN @num1 ELSE @num2 END - 1
    FROM Numbers A
       , Numbers B 
    WHERE ABS(@num1 - @num2) + 1 < 4294967297
);