T-SQL:如何将多行代码与if语句组合在一起

时间:2017-05-19 14:44:38

标签: sql-server tsql

在这个人的墙上撞了我的头。对我来说,这是一个新的诚实。我正在努力解决这个问题。显然是试图拉数据和垃圾。 concats只是拉垃圾,因为我似乎无法解析插入数据中的垃圾。我从来没有遇到过这样的问题,即我不得不从数据中提取垃圾而不仅仅是通过数据库进行分配。

表定义

create table [dbo].[sample_table](

    [id] [int] identity(1,1) not null,
    [date] [varchar](255) null,
    [hour1] [varchar](255) null,
    [hour2] [varchar](255) null,
    [hour3] [varchar](255) null,
    [hour4] [varchar](255) null,
    [hour5] [varchar](255) null,
    [hour6] [varchar](255) null,
    [hour7] [varchar](255) null,
    [hour8] [varchar](255) null,
    [hour9] [varchar](255) null,
    [hour10] [varchar](255) null,
    [hour11] [varchar](255) null,
    [hour12] [varchar](255) null,
    [hour13] [varchar](255) null,
    [hour14] [varchar](255) null,
    [hour15] [varchar](255) null,
    [hour16] [varchar](255) null,
    [hour17] [varchar](255) null,
    [hour18] [varchar](255) null,
    [hour19] [varchar](255) null,
    [hour20] [varchar](255) null,
    [hour21] [varchar](255) null,
    [hour22] [varchar](255) null,
    [hour23] [varchar](255) null,
    [hour24] [varchar](255) null)

数据已插入

insert into [dbo].[sample_table] ([date], [hour1], [hour2], [hour3], [hour4], [hour5], [hour6], [hour7], [hour8], [hour9], [hour10], [hour11], [hour12], [hour13], [hour14], [hour15], [hour16], [hour17], [hour18], [hour19], [hour20], [hour21], [hour22], [hour23], [hour24])
values 
('2015-05-25', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '-11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', null, '24'),
('2015-07-12', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10.5', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24'),
('2016-11-01', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', 'test', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24'),
('2014-05-25', '1', '2', '3', '4', '5', '6', '7', '8.954', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24')

编写一个脚本,返回id,datetime [new field],value [new field]的行数据。

例如

  

id full_date value

     

1 2015-05-25 13:00:00 13
  1 2015-05-25 14:00:00 14
  2 2015-07-12 08:00:00 8
  4 2014-05-25 08:00:00 8.954

1 个答案:

答案 0 :(得分:1)

我不知道您为什么要尝试使用if语句执行此操作。

相反,让我们使用cross apply(values )unpivot您的数据:

select 
    st.id
  , u.full_date
  , u.value
from sample_table st
  cross apply (values 
     (dateadd(hour,1,[date]),hour1)
    ,(dateadd(hour,2,[date]),hour2)
    ,(dateadd(hour,3,[date]),hour3)
    ,(dateadd(hour,4,[date]),hour4)
    ,(dateadd(hour,5,[date]),hour5)
    ,(dateadd(hour,6,[date]),hour6)
    ,(dateadd(hour,7,[date]),hour7)
    ,(dateadd(hour,8,[date]),hour8)
    ,(dateadd(hour,9,[date]),hour9)
    ,(dateadd(hour,10,[date]),hour10)
    ,(dateadd(hour,11,[date]),hour11)
    ,(dateadd(hour,12,[date]),hour12)
    ,(dateadd(hour,13,[date]),hour13)
    ,(dateadd(hour,14,[date]),hour14)
    ,(dateadd(hour,15,[date]),hour15)
    ,(dateadd(hour,16,[date]),hour16)
    ,(dateadd(hour,17,[date]),hour17)
    ,(dateadd(hour,18,[date]),hour18)
    ,(dateadd(hour,19,[date]),hour19)
    ,(dateadd(hour,20,[date]),hour20)
    ,(dateadd(hour,21,[date]),hour21)
    ,(dateadd(hour,22,[date]),hour22)
    ,(dateadd(hour,23,[date]),hour23)
    ,(dateadd(hour,24,[date]),hour24)
    ) u (full_date, value)

rextester演示:http://rextester.com/UPQH6293

返回:

+----+---------------------+-------+
| id |      full_date      | value |
+----+---------------------+-------+
|  1 | 2015-05-25 01:00:00 | 1     |
|  1 | 2015-05-25 02:00:00 | 2     |
|  1 | 2015-05-25 03:00:00 | 3     |
|  1 | 2015-05-25 04:00:00 | 4     |
|  1 | 2015-05-25 05:00:00 | 5     |
|  1 | 2015-05-25 06:00:00 | 6     |
|  1 | 2015-05-25 07:00:00 | 7     |
|  1 | 2015-05-25 08:00:00 | 8     |
|  1 | 2015-05-25 09:00:00 | 9     |
|  1 | 2015-05-25 10:00:00 | 10    |
|  1 | 2015-05-25 11:00:00 | -11   |
|  1 | 2015-05-25 12:00:00 | 12    |
|  1 | 2015-05-25 13:00:00 | 13    |
|  1 | 2015-05-25 14:00:00 | 14    |
|  1 | 2015-05-25 15:00:00 | 15    |
|  1 | 2015-05-25 16:00:00 | 16    |
|  1 | 2015-05-25 17:00:00 | 17    |
|  1 | 2015-05-25 18:00:00 | 18    |
|  1 | 2015-05-25 19:00:00 | 19    |
|  1 | 2015-05-25 20:00:00 | 20    |
|  1 | 2015-05-25 21:00:00 | 21    |
|  1 | 2015-05-25 22:00:00 | 22    |
|  1 | 2015-05-25 23:00:00 | NULL  |
|  1 | 2015-05-26 00:00:00 | 24    |
|  2 | 2015-07-12 01:00:00 | 1     |
|  2 | 2015-07-12 02:00:00 | 2     |
|  2 | 2015-07-12 03:00:00 | 3     |
|  2 | 2015-07-12 04:00:00 | 4     |
|  2 | 2015-07-12 05:00:00 | 5     |
|  2 | 2015-07-12 06:00:00 | 6     |
|  2 | 2015-07-12 07:00:00 | 7     |
|  2 | 2015-07-12 08:00:00 | 8     |
|  2 | 2015-07-12 09:00:00 | 9     |
|  2 | 2015-07-12 10:00:00 | 10.5  |
|  2 | 2015-07-12 11:00:00 | 11    |
|  2 | 2015-07-12 12:00:00 | 12    |
|  2 | 2015-07-12 13:00:00 | 13    |
|  2 | 2015-07-12 14:00:00 | 14    |
|  2 | 2015-07-12 15:00:00 | 15    |
|  2 | 2015-07-12 16:00:00 | 16    |
|  2 | 2015-07-12 17:00:00 | 17    |
|  2 | 2015-07-12 18:00:00 | 18    |
|  2 | 2015-07-12 19:00:00 | 19    |
|  2 | 2015-07-12 20:00:00 | 20    |
|  2 | 2015-07-12 21:00:00 | 21    |
|  2 | 2015-07-12 22:00:00 | 22    |
|  2 | 2015-07-12 23:00:00 | 23    |
|  2 | 2015-07-13 00:00:00 | 24    |
|  3 | 2016-11-01 01:00:00 | 1     |
|  3 | 2016-11-01 02:00:00 | 2     |
|  3 | 2016-11-01 03:00:00 | 3     |
|  3 | 2016-11-01 04:00:00 | 4     |
|  3 | 2016-11-01 05:00:00 | 5     |
|  3 | 2016-11-01 06:00:00 | 6     |
|  3 | 2016-11-01 07:00:00 | 7     |
|  3 | 2016-11-01 08:00:00 | 8     |
|  3 | 2016-11-01 09:00:00 | 9     |
|  3 | 2016-11-01 10:00:00 | 10    |
|  3 | 2016-11-01 11:00:00 | 11    |
|  3 | 2016-11-01 12:00:00 | 12    |
|  3 | 2016-11-01 13:00:00 | 13    |
|  3 | 2016-11-01 14:00:00 | test  |
|  3 | 2016-11-01 15:00:00 | 15    |
|  3 | 2016-11-01 16:00:00 | 16    |
|  3 | 2016-11-01 17:00:00 | 17    |
|  3 | 2016-11-01 18:00:00 | 18    |
|  3 | 2016-11-01 19:00:00 | 19    |
|  3 | 2016-11-01 20:00:00 | 20    |
|  3 | 2016-11-01 21:00:00 | 21    |
|  3 | 2016-11-01 22:00:00 | 22    |
|  3 | 2016-11-01 23:00:00 | 23    |
|  3 | 2016-11-02 00:00:00 | 24    |
|  4 | 2014-05-25 01:00:00 | 1     |
|  4 | 2014-05-25 02:00:00 | 2     |
|  4 | 2014-05-25 03:00:00 | 3     |
|  4 | 2014-05-25 04:00:00 | 4     |
|  4 | 2014-05-25 05:00:00 | 5     |
|  4 | 2014-05-25 06:00:00 | 6     |
|  4 | 2014-05-25 07:00:00 | 7     |
|  4 | 2014-05-25 08:00:00 | 8.954 |
|  4 | 2014-05-25 09:00:00 | 9     |
|  4 | 2014-05-25 10:00:00 | 10    |
|  4 | 2014-05-25 11:00:00 | 11    |
|  4 | 2014-05-25 12:00:00 | 12    |
|  4 | 2014-05-25 13:00:00 | 13    |
|  4 | 2014-05-25 14:00:00 | 14    |
|  4 | 2014-05-25 15:00:00 | 15    |
|  4 | 2014-05-25 16:00:00 | 16    |
|  4 | 2014-05-25 17:00:00 | 17    |
|  4 | 2014-05-25 18:00:00 | 18    |
|  4 | 2014-05-25 19:00:00 | 19    |
|  4 | 2014-05-25 20:00:00 | 20    |
|  4 | 2014-05-25 21:00:00 | 21    |
|  4 | 2014-05-25 22:00:00 | 22    |
|  4 | 2014-05-25 23:00:00 | 23    |
|  4 | 2014-05-26 00:00:00 | 24    |
+----+---------------------+-------+