简体   繁体   中英

Insert cyclical result from query into a table

I need to insert the result of a query into a table Employee .

The Employee table is created in this way:

CREATE TABLE Employee(
                        Name1    NVARCHAR(100)
                      , Date1    DATETIME
                      , Name2    NVARCHAR(100)
                      , Date2    DATETIME
                      , Name3    NVARCHAR(100)
                      , Date3    DATETIME
                      , Name4    NVARCHAR(100)
                      , Date4    DATETIME
                      , Name5    NVARCHAR(100)
                      , Date5    DATETIME
                      , Name6    NVARCHAR(100)
                      , Date6    DATETIME
                      , Name7    NVARCHAR(100)
                      , Date7    DATETIME
                      )

For the sake of simplicity let's say that my query is:

SELECT d.name, d.date
FROM data d
WHERE d.type = 'n'
ORDER BY d.name

This query returns something like this:

A        DateA
B        DateB
C        DateC
D        DateD
E        DateE
F        DateF
G        DateG
H        DateH
I        DateI
L        DateL
M        DateM
N        DateN
O        DateO
P        DateP
Q        DateQ
QQ       DateQQ
...
...    

The tricky part is that I should insert each row inside the Employee table in this way:

  • first name of the query (A) inside Name1, first date (DateA) inside Date1
  • second name (B) inside Name2, and second date (DateB) inside Date2
  • third row inside Name3, Date3
  • ....
  • seventh row inside Name7, Day7
  • eighth row inside Name1 and Day1
  • ninth row inside Name2 and Day2
  • ....

This means that after every row_num % 7 = 0 I need to cycle again (somehow) starting from Name1 and Day1.

The expected result (inside my Employee table) should be:

A,DateA,   B,DateB,   C,DateC,   D,DateD,   E,DateE,  F,DateF,   G,DateG 
H,DateH,   I,DateI,   L,DateL,   M,DateM,   N,DateN,  O,DateO,   P,DateP 
Q,DateQ,   QQ,DateQQ, R,DateR,   S,DateS,   T,DateT,  U,DateU,   V,DateV  
W,DateW,   X,DateX,   Y,DateY,   YY,DateYY, Z,DateZ,  ZZ,DateZZ, ZZZ,DateZZZ
....

I don't have any idea how to handle this thing, but I need that output inside that table (or inside 7 different table if it can be more helpful).

I hope that I express myself clear.

You can spend about 30 minutes thinking your way through a straight set based approach or create a simple loop in 5 minutes.

DECLARE @Loops INT=(SELECT (COUNT(*) / 7)+CASE WHEN(COUNT(*) % 7)>0 THEN 1 ELSE 0 END  FROM MyOtherTable)  
DECLARE @LoopCount INT=0

WHILE( @LoopCount <= @Loops )BEGIN
    SET @LoopCount=@LoopCount+1

    INSERT Employee(name,date)
    SELECT 
        name,date 
    FROM
    (
        SELECT 
            name,
            date,
            InsertLoop=NTILE(@Loops) OVER(ORDER BY MyTableID)           
        FROM 
            MyOtherTable
    )AS X
    WHERE X.InsertLoop=@LoopCount
END

In one statement (I think that's what Lucky had in mind):

With Src As ( --< Source query with RowNum and ColNum calculated through ROW_NUMBER()
    SELECT e.name, e.date
       ,       (ROW_NUMBER() Over (Order By Name, Date) - 1) % 7 + 1 As ColNum
       , FLOOR((ROW_NUMBER() Over (Order By Name, Date) - 1) / 7)    As RowNum
    FROM employee e
    WHERE e.type = 'n'
)
Select
    Src1.Name As Name1, Src1.Date As Date1
  , Src2.Name As Name2, Src2.Date As Date2
  , Src3.Name As Name3, Src3.Date As Date3
  , Src4.Name As Name4, Src4.Date As Date4
  , Src5.Name As Name5, Src5.Date As Date5
  , Src6.Name As Name6, Src6.Date As Date6
  , Src7.Name As Name7, Src7.Date As Date7
From Src Src1
Left Join Src Src2 On Src1.RowNum = Src2.RowNum And Src2.ColNum = 2
Left Join Src Src3 On Src1.RowNum = Src3.RowNum And Src3.ColNum = 3
Left Join Src Src4 On Src1.RowNum = Src4.RowNum And Src4.ColNum = 4
Left Join Src Src5 On Src1.RowNum = Src5.RowNum And Src5.ColNum = 5
Left Join Src Src6 On Src1.RowNum = Src6.RowNum And Src6.ColNum = 6
Left Join Src Src7 On Src1.RowNum = Src7.RowNum And Src7.ColNum = 7
Where Src1.ColNum = 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM