将标题行展开为多个子行

时间:2015-07-14 09:07:08

标签: sql sql-server sql-server-2005

在我的SQL数据库中,我有一张表格,其中包含书籍[书籍]的书籍,其中书籍中的门票数量可能会有所不同。

这由两列[Books].[StartNo][Books].[BookSize]

表示

我需要实现的是一个select语句,它为表中的每个故障单重复表[Books]中的每一行,并附加一个计算列,显示该行的故障单号。

所以来自

--------+---------+----------
Book    | StartNo | BookSize 
--------+---------+----------
Book 1  |   1     |    3     
Book 2  |   4     |    4    
Book 3  |  19     |    4     

这样的事情

--------+---------+----------+----------
Book    | StartNo | BookSize | TicketNo
--------+---------+----------+----------
Book 1  |   1     |    3     |    1
Book 1  |   1     |    3     |    2
Book 1  |   1     |    3     |    3
Book 2  |   4     |    4     |    4
Book 2  |   4     |    4     |    5
Book 2  |   4     |    4     |    6
Book 2  |   4     |    4     |    7
Book 3  |  19     |    4     |   19
Book 3  |  19     |    4     |   20
Book 3  |  19     |    4     |   21
Book 3  |  19     |    4     |   22

我不太确定从哪里开始。

3 个答案:

答案 0 :(得分:4)

试试这个:

;WITH Counts AS (
    SELECT Max(StartNo + BookSize) AS TotalBookSize
    FROM t
), CTE(Tickets) AS (
    SELECT 1
    UNION ALL
    SELECT Tickets + 1
    FROM CTE
    WHERE Tickets < (SELECT TotalBookSize FROM Counts)
)
SELECT *
FROM t JOIN CTE ON CTE.Tickets BETWEEN t.StartNo AND t.StartNo + t.BookSize - 1

答案 1 :(得分:2)

使用[WebMethod] [System.Web.Script.Services.ScriptMethod(UseHttpGet = true)] public static int ItemCount() { int val = 0; Item itm = Sitecore.Context.Database.GetItem("Id will be there"); val = itm.Children.Count; return val; }

tally table

答案 2 :(得分:0)

您需要一个数字列表并将其与书籍表一起加入

select b.*, number
from Books b
join master.dbo.spt_values v on v.number between b.StartNo AND b.StartNo+b.BookSize-1