我在Postgres有一张桌子,平日和时间(24小时模式)。像:
weekday | time
---------------
sun | 04
wed | 23
现在我需要提前5个小时。我怎么做到:
weekday | time
---------------
sat | 23
wed | 18
答案 0 :(得分:2)
您可以使用CASE语句来获得所需的结果。它只是有点难看,但它并不难以管理。
private void gradeBox_Loaded(object sender, RoutedEventArgs e)
{
var textBox = gradeBox.Template.FindName("PART_TextBox", gradeBox) as Xceed.Wpf.Toolkit.WatermarkTextBox;
if (textBox != null)
{
textBox.IsReadOnly = true;
}
}
你可以使用CTE,这样你只需要在一个地方改变“5”。还可以添加正数或负数并处理一天前滚的功能:
SELECT
CASE WHEN time - 5 < 0 THEN
CASE
WHEN weekday = 'Sun' THEN 'Sat'
WHEN weekday = 'Sat' THEN 'Fri'
WHEN weekday = 'Fri' THEN 'Thu'
WHEN weekday = 'Thu' THEN 'Wed'
WHEN weekday = 'Wed' THEN 'Tue'
WHEN weekday = 'Tue' THEN 'Mon'
WHEN weekday = 'Mon' THEN 'Sun'
END
ELSE weekday
END as weekday,
CASE WHEN time - 5 < 0 THEN 24 - (time-5) ELSE time-5 END as time
FROM yourtable;
答案 1 :(得分:2)
另一种不那么丑陋的方式:
t=# with a as (
with
i as (select 5 h)
, a as (
select to_char(g, 'dy'),g delta
from generate_series('2017.05.15','2017.05.21','1 day'::interval) g
)
select s145.*,delta + (time||' hours')::interval - concat(h,' hours')::interval nt
from s145
join i on true
join a on a.to_char = weekday
)
select to_char(nt, 'dy'), to_char(nt,'HH24')
from a;
to_char | to_char
---------+---------
sat | 23
wed | 18
(2 rows)
,例如2小时:
t=# with a as (
with
i as (select 2 h)
, a as (
select to_char(g, 'dy'),g delta
from generate_series('2017.05.15','2017.05.21','1 day'::interval) g
)
select s145.*,delta + (time||' hours')::interval - concat(h,' hours')::interval nt
from s145
join i on true
join a on a.to_char = weekday
)
select to_char(nt, 'dy'), to_char(nt,'HH24')
from a;
to_char | to_char
---------+---------
sun | 02
wed | 21
(2 rows)
我拿了'2017.05.15'
,因为我知道这是星期一。它会尊重时间切换,可能不是op想要的。当然很容易避免将时区设置为'utc'。但我不想让已经丑陋的代码更复杂化