-1

有人可以帮我将记录分成多行。

我的记录看起来像这样

321517  2013    SEPTEMBER   3   30  286787  321517-2013
321517  2013    SEPTEMBER   2   42  286787  321517-2013

我希望他们看起来像这样

321517  2013    SEPTEMBER   1   30  286787  321517-2013
321517  2013    SEPTEMBER   1   30  286787  321517-2013
321517  2013    SEPTEMBER   1   30  286787  321517-2013
321517  2013    SEPTEMBER   1   42  286787  321517-2013
321517  2013    SEPTEMBER   1   42  286787  321517-2013
4

2 回答 2

1

您可以获得最大可能值,然后进行递归 CTE 以生成行。

;WITH MAX_VALUE AS (
   SELECT MAX(C4) AS VAL FROM Table1
),
TMP_ROWS AS (
    SELECT 1 AS PARENT, 0 AS LVL, 1 AS ID

    UNION ALL

    SELECT 
        CHILD.PARENT,
        TMP_ROWS.LVL + 1 AS LVL,
        TMP_ROWS.ID
    FROM (SELECT 1 AS PARENT, 1 AS ID, 0 AS NIVEL) AS CHILD
        INNER JOIN TMP_ROWS ON CHILD.PARENT = TMP_ROWS.ID
    WHERE TMP_ROWS.LVL < (SELECT VAL FROM MAX_VALUE)
)
select C1, C2, C3, 1 C4, C5, C6, C7
from Table1 join TMP_ROWS on C4 > TMP_ROWS.LVL
order by C1, C2, C3, C5, C6, C7

演示(基于之前的回复数据)

*编辑:“ROWS”不是表的好名字

于 2013-10-28T19:29:34.943 回答
0

你可以尝试这样的事情。请注意,此查询假定maximum value of your 4th Column is 10. more rows to the CTE using a cross join如果您有更高的值,您可以添加。

;WITH CTE AS (
  select Digit 
  from ( values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9)) AS t(Digit)
)
select C1, C2, C3, 1 C4, C5, C6, C7
from Table1 join CTE on C4 > Digit
order by C1

sql server 2008 上的小提琴演示

于 2013-10-28T18:54:15.040 回答