0

我有一个表,其中存储有关分支的打开和关闭时间的信息,如下所示:

地点 每周开放时间
---------- ------------ ----------- ----------
奥斯汀太阳公开赛 1730
奥斯汀周一收盘 200
奥斯汀星期一公开赛 830
奥斯汀周二收盘 200
奥斯汀周二公开赛 830
奥斯汀周三收盘 200
奥斯汀周三公开赛 830
奥斯汀周四收盘 200
奥斯汀周四公开赛 830
奥斯汀周五收盘 200
奥斯汀星期五公开赛 830
奥斯汀周五收盘 1730

如您所见,并非所有日子都一定有开/关日,一天可以有多个开/关时间,原因是分行可能在前一天营业,上夜班,然后在第二天关闭。在此示例中,奥斯汀分行于周日下午 5:30 开门,周一凌晨 2:00 关门,然后在 8:30 重新营业,一直工作到第二天凌晨 2:00。周五,他们在下午 5:30 关闭,周六休息,然后在周日再次开放以重复相同的周期。

我的问题是,是否可以单独使用 select 语句来返回看起来像这样的行:

地点 DAYOFTHEEK OPENTIME CLOSETIME
---------- ------------ ---------- ----------
奥斯汀太阳 1730 空    
奥斯汀星期一 null 200
奥斯汀星期一 830 空
奥斯汀星期二 null 200
奥斯汀星期二 830 空
奥斯汀星期三 null 200
奥斯汀星期三 830 空
奥斯汀周四 null 200
奥斯汀周四 830 空
奥斯汀星期五 null 200
奥斯汀星期五 830 1730

null 只是一个占位符,可以是像 -1 这样的任何值,或者也可以使用任何值。在这种特殊情况下,时间是一个 int 值,表示时间等价(凌晨 2:00 为 200,17:30 为 1730,等等)

谢谢!

4

2 回答 2

0

尝试这个:

select location,dayofweek,
case when openorclose = 'Open' then time end as OpenTime,
case when openorclose = 'CLOSE' then time end as CloseTime
from TableName;
于 2013-10-12T07:28:55.037 回答
0

我想,这会解决你的问题

select location,dayoftheweek,
       case openorclose when 'Open' then time end "OPENSTATUS",
       case openorclose when 'Close' then time end "CLOSESTATUS" from YOUR_TABLE;

现场 SQLFIDDLE 演示

于 2013-10-11T16:26:37.600 回答