有人可以帮助我将记录分成多行。
我的记录看起来像这样
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
答案 0 :(得分: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
Demo (based on previuos reply data)
*编辑:“ROWS”不是表格的好名字
答案 1 :(得分: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