我有以下信息,但只有时间没有日期(时间连续),但我有日期基点。
time T1 T2 T3 T4
---------------------------------------------------------------------
14:36:11.000 24 28 23 35
20:46:20.000 27 28 27 32
00:07:33.000 24 27 23 38
01:36:20.000 94 28 27 32
03:46:50.000 24 28 23 37
07:58:20.000 64 58 27 32
15:29:25.000 24 23 27 32
01:15:32.000 44 28 27 39
如果我现在第一个Row的日期时间是'2013-06-03 14:36:11.000'。我怎样才能获得这些数据:
Datetime T1 T2 T3 T4
---------------------------------------------------------------------
2013-06-03 14:36:11.000 24 28 23 35
2013-06-03 20:46:20.000 27 28 27 32
2013-06-04 00:07:33.000 24 27 23 38
2013-06-04 01:36:20.000 94 28 27 32
2013-06-04 03:46:50.000 24 28 23 37
2013-06-04 07:58:20.000 64 58 27 32
2013-06-04 15:29:25.000 24 23 27 32
2013-06-05 01:15:32.000 44 28 27 39
答案 0 :(得分:4)
此解决方案适用于MySql:
SELECT (@var:=if(time(@var)<=b.time,timestamp(date(@var),b.time),
timestamp(date(date_add(@var, interval 1 day)),b.time))) as Datetime ,
b.T1,b.T2,b.T3,b.T4
FROM test.YOURTABLE b, (select @var:='2013-06-03 00:00:00') a
答案 1 :(得分:2)
试试这个 -
<强>查询:强>
DECLARE @temp TABLE
(
[time] VARCHAR(12)
, T1 INT
, T2 INT
, T3 INT
, T4 INT
)
INSERT INTO @temp ([time], T1, T2, T3, T4)
VALUES
('14:36:11.000', 24, 28, 23, 35),
('20:46:20.000', 27, 28, 27, 32),
('00:07:33.000', 24, 27, 23, 38),
('01:36:20.000', 94, 28, 27, 32),
('03:46:50.000', 24, 28, 23, 37),
('07:58:20.000', 64, 58, 27, 32),
('15:29:25.000', 24, 23, 27, 32),
('01:15:32.000', 44, 28, 27, 39)
DECLARE @Date DATETIME = '2013-06-03'
;WITH cte AS
(
SELECT
tt = [time]
, rn = ROW_NUMBER() OVER (ORDER BY (SELECT 1))
, T1, T2, T3, T4
FROM @temp t
),
cte2 AS
(
SELECT t1.*, [shift] = 0
FROM cte t1
WHERE t1.rn = 1
UNION ALL
SELECT cte.*, [shift] =
CASE WHEN cte2.tt > cte.tt
THEN [shift] + 1
ELSE [shift]
END
FROM cte2
JOIN cte ON cte2.rn = cte.rn - 1
)
SELECT [time] = DATEADD(DAY, shift, @Date + ' ' + tt), T1, T2, T3, T4
FROM cte2
<强>结果:强>
time T1 T2 T3 T4
----------------------- ----------- ----------- ----------- -----------
2013-06-03 14:36:11.000 24 28 23 35
2013-06-03 20:46:20.000 27 28 27 32
2013-06-04 00:07:33.000 24 27 23 38
2013-06-04 01:36:20.000 94 28 27 32
2013-06-04 03:46:50.000 24 28 23 37
2013-06-04 07:58:20.000 64 58 27 32
2013-06-04 15:29:25.000 24 23 27 32
2013-06-05 01:15:32.000 44 28 27 39