多行值到同一行和不同列

时间:2018-04-24 19:26:52

标签: sql sql-server sql-server-2008

我有一个包含4个字段Person_idStore_idstartdateenddate的表格。对于person_id的特定值,可以有许多具有不同开始日期和结束日期的记录。在这里,我需要将多行值更新为相同的行和不同的列。

示例1:

Person_id        Store_ID         Startdate       enddate
10000351067      10000232561      2016-09-09      2016-09-16
10000351067      10000232561      2016-09-16      2016-10-03
10000351067      10000232561      2016-10-03      2016-10-07
10000351067      10000232561      2016-10-07      2017-01-17
10000351067      10000232561      2017-01-17      2018-04-05
10000351067      10000232561      2018-04-05      NULL

示例2:

10000193858      10000225875      2016-07-13          2016-08-03
10000193858      10000225875      2016-08-03          2017-05-17
10000193858      10000225875      2017-05-17          2017-06-05
10000193858      10000225875      2017-05-31          2017-06-05
10000193858      10000225875      2017-06-05          2017-06-13
10000193858      10000225875      2017-06-13          2017-08-16
10000193858      10000225875      2017-08-07          2017-08-16
10000193858      10000225875      2017-08-16          2017-08-18
10000193858      10000225875      2017-08-18          2017-08-31
10000193858      10000225875      2017-08-31          2018-01-05
10000193858      10000225875      2018-01-05          NULL

结果集:

例如:

Person_id   store_id      Start_date_1 Ended_at_1  Started_at_2 Ended_at_2...
10000351067 10000232561   2016-09-09   2016-09-16  2016-09-16   2016-10-03...

例如:

Person_id    store_id     Start_date_1 Ended_at_1  Started_at_2 Ended_at_2...
10000193858  10000225875  2016-07-13   2016-08-03  2016-08-03   2017-05-17....

2 个答案:

答案 0 :(得分:3)

这是一个有点复杂的问题,但可以使用CROSS APPLY解析数据,然后使用PIVOT以您需要的格式将其恢复。它在SQL 2017上进行了测试,但我相信PIVOT和CTE是在2005年添加的,而ROW_NUMBER()是在2008年添加的,因此应该在您的SQL版本中使用。在主查询中,我对列进行了别名,以便您可以看到数据的来源。既然你说你可以有20个start/endDate列,我就添加了这些列,但如果需要,你可以使用动态SQL来创建一个更动态的列表。

SQL Fiddle

MS SQL Server 2017架构设置

CREATE TABLE t1 ( Person_id bigint, Store_ID bigint, Startdate date, enddate date ) ;
INSERT INTO t1 (Person_id,Store_ID,Startdate,enddate)
VALUES 
    ( 10000351067,10000232561,'2016-09-09','2016-09-16')
  , ( 10000351067,10000232561,'2016-09-16','2016-10-03')
  , ( 10000351067,10000232561,'2016-10-03','2016-10-07')
  , ( 10000351067,10000232561,'2016-10-07','2017-01-17')
  , ( 10000351067,10000232561,'2017-01-17','2018-04-05')
  , ( 10000351067,10000232561,'2018-04-05',NULL)
  , ( 10000193858,10000225875,'2016-07-13','2016-08-03')
  , ( 10000193858,10000225875,'2016-08-03','2017-05-17')
  , ( 10000193858,10000225875,'2017-05-17','2017-06-05')
  , ( 10000193858,10000225875,'2017-05-31','2017-06-05')
  , ( 10000193858,10000225875,'2017-06-05','2017-06-13')
  , ( 10000193858,10000225875,'2017-06-13','2017-08-16')
  , ( 10000193858,10000225875,'2017-08-07','2017-08-16')
  , ( 10000193858,10000225875,'2017-08-16','2017-08-18')
  , ( 10000193858,10000225875,'2017-08-18','2017-08-31')
  , ( 10000193858,10000225875,'2017-08-31','2018-01-05')
  , ( 10000193858,10000225875,'2018-01-05',NULL)
;

查询1

/* Start with a CTE to get the base data, plus the row number for the pivot colName */
; WITH cte_details AS (
  SELECT t1.Person_ID
    , t1.Store_ID
    , t1.StartDate
    , t1.EndDate
    , ROW_NUMBER() OVER ( PARTITION BY t1.Person_ID, t1.Store_ID ORDER BY t1.StartDate ) AS rn
  FROM t1
)
SELECT pvt.* 
FROM (
  /* Unpivot values, using a CROSS APPLY */
  SELECT cd.Person_ID, cd.Store_ID
    , crs.c + CAST(cd.rn AS varchar(5)) AS colName
    , crs.v AS colValue
  FROM cte_details cd
  CROSS APPLY (
    SELECT 'startDate', startDate UNION ALL
    SELECT 'endDate', endDate  
  ) crs (c, v)
) s1
/* Now PIVOT those back to get the results. */
PIVOT (
  max(s1.colValue) 
  FOR s1.colName IN (
        startDate1, endDate1, startDate2, endDate2, startDate3, endDate3
      , startDate4, endDate4, startDate5, endDate5, startDate6, endDate6
      , startDate7, endDate7, startDate8, endDate8, startDate9, endDate9
      , startDate10, endDate10, startDate11, endDate11, startDate12, endDate12
      , startDate13, endDate13, startDate14, endDate14, startDate15, endDate15
      , startDate16, endDate16, startDate17, endDate17, startDate18, endDate18
      , startDate19, endDate19, startDate20, endDate20      
   )
) pvt

<强> Results

|   Person_ID |    Store_ID | startDate1 |   endDate1 | startDate2 |   endDate2 | startDate3 |   endDate3 | startDate4 |   endDate4 | startDate5 |   endDate5 | startDate6 |   endDate6 | startDate7 |   endDate7 | startDate8 |   endDate8 | startDate9 |   endDate9 | startDate10 |  endDate10 | startDate11 | endDate11 | startDate12 | endDate12 | startDate13 | endDate13 | startDate14 | endDate14 | startDate15 | endDate15 | startDate16 | endDate16 | startDate17 | endDate17 | startDate18 | endDate18 | startDate19 | endDate19 | startDate20 | endDate20 |
|-------------|-------------|------------|------------|------------|------------|------------|------------|------------|------------|------------|------------|------------|------------|------------|------------|------------|------------|------------|------------|-------------|------------|-------------|-----------|-------------|-----------|-------------|-----------|-------------|-----------|-------------|-----------|-------------|-----------|-------------|-----------|-------------|-----------|-------------|-----------|-------------|-----------|
| 10000193858 | 10000225875 | 2016-07-13 | 2016-08-03 | 2016-08-03 | 2017-05-17 | 2017-05-17 | 2017-06-05 | 2017-05-31 | 2017-06-05 | 2017-06-05 | 2017-06-13 | 2017-06-13 | 2017-08-16 | 2017-08-07 | 2017-08-16 | 2017-08-16 | 2017-08-18 | 2017-08-18 | 2017-08-31 |  2017-08-31 | 2018-01-05 |  2018-01-05 |    (null) |      (null) |    (null) |      (null) |    (null) |      (null) |    (null) |      (null) |    (null) |      (null) |    (null) |      (null) |    (null) |      (null) |    (null) |      (null) |    (null) |      (null) |    (null) |
| 10000351067 | 10000232561 | 2016-09-09 | 2016-09-16 | 2016-09-16 | 2016-10-03 | 2016-10-03 | 2016-10-07 | 2016-10-07 | 2017-01-17 | 2017-01-17 | 2018-04-05 | 2018-04-05 |     (null) |     (null) |     (null) |     (null) |     (null) |     (null) |     (null) |      (null) |     (null) |      (null) |    (null) |      (null) |    (null) |      (null) |    (null) |      (null) |    (null) |      (null) |    (null) |      (null) |    (null) |      (null) |    (null) |      (null) |    (null) |      (null) |    (null) |      (null) |    (null) |

https://docs.microsoft.com/en-us/previous-versions/sql/sql-server-2008-r2/ms177410(v=sql.105)

答案 1 :(得分:1)

如果我理解正确,您希望UPDATE某些字段WHERE某些条件成立

UPDATE 
SET Store_ID=10000232561, Startdate=2016-09-09, enddate=2016-09-16
WHERE Person_id = 10000351067

微软的T-SQL文档非常好,请参阅the update statement docs

相关问题