从单行返回多行

时间:2009-06-30 14:45:33

标签: sql-server-2005

这可能是不可能的,但我想我会把它扔出去:

鉴于下表:

ID,开始,结束
123,1,N

其中N是整数,写一个查询以返回以下结果集:

ID,开始,结束
123,1,1 123,1,2 123,1,3 。


123,1,N

我们使用的平台是SQL Server 2005,但是如果你能用另一种SQL来做这件事,我仍然会对这个解决方案感兴趣。

3 个答案:

答案 0 :(得分:1)

给定一些(理论上无限,但你可以预先填充)包含所有整数的表整数,答案相当简单:

SELECT ID, Begin, I FROM YourTable, Integers
WHERE I <= Begin AND I >= End

使用Integers.I上的聚簇索引,这应该非常快。您可以在存储过程中预先填充整数(基于SELECT max(End) FROM YourTable的结果)。

答案 1 :(得分:1)

这最多可以达到99,999,您可以轻松修改它以添加更多数字。它不需要预先存在的数字表,也不需要存储过程,并且仍然非常快。至少适用于SQL Server 2000及以上版本,并且可以轻松移植到其他版本的SQL:

select MyTable.ID, MyTable.[Begin], n.N
from (
    select 123 as ID, 1 as [Begin], 9 as [End]
) MyTable
cross join (
    select a.a + (10 * b.a) + (100 * c.a) + (1000 * d.a) + (10000 * e.a) as N
    from (select 0 as a union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) as a
    cross join (select 0 as a union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) as b
    cross join (select 0 as a union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) as c
    cross join (select 0 as a union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) as d
    cross join (select 0 as a union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) as e
) n
where n.N > 0
    and n.N <= MyTable.[End]
order by n.N

答案 2 :(得分:1)

试试这个:

create table #smalltable (id int, [begin] int, [end] int)
insert into #smalltable values (123,1,4)
insert into #smalltable values (124,1,12)
insert into #smalltable values (125,1,7)

;WITH digits (d) AS (
    SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION
    SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION
    SELECT 7 UNION SELECT 8 UNION SELECT 9 UNION
    SELECT 0)
SELECT
    s.id, s.[begin], n.Number AS [End]
    FROM (SELECT i.d + ii.d * 10 + iii.d * 100 + iv.d * 1000 +
              v.d * 10000 + vi.d * 100000 AS Number
              FROM digits            i
                  CROSS JOIN digits  ii
                  CROSS JOIN digits  iii
                  CROSS JOIN digits  iv
                  CROSS JOIN digits  v
                  CROSS JOIN digits  vi
         ) AS N
        INNER JOIN #smalltable                                    s  ON 1=1
        INNER JOIN (SELECT MAX([end]) AS MaxEnd FROM #smalltable) dt ON 1=1
   WHERE n.Number > 0 AND n.Number<=dt.MaxEnd
    AND n.Number<=s.[end]
   ORDER BY s.id,n.Number

评论

  • 不要将列命名为保留字:“开始”和“结束”,有一天你会感谢我。
  • 如果您计划多次在生产中运行此项,create a Numbers table
    并改为使用此查询:

在此之前必须有一个表格数字(见上面的链接)

SELECT
    s.id,s.[begin],n.Number AS [End]
    FROM Numbers                n
        INNER JOIN #smalltable  s ON 1=1
   WHERE  n.Number > 0 AND n.Number<=s.[end]
   ORDER BY s.id,number

它会更好。