【问题标题】:Oracle query to pivot rows info into columnsOracle 查询将行信息转换为列
【发布时间】:2013-10-11 15:30:54
【问题描述】:

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

地点 每周开放时间 ---------- ------------ ----------- ---------- 奥斯汀太阳公开赛 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,等等)

谢谢!

【问题讨论】:

    标签: sql oracle


    【解决方案1】:

    我想,这会解决你的问题

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

    LIVE SQLFIDDLE DEMO

    【讨论】:

      【解决方案2】:

      试试这个:

      select location,dayofweek,
      case when openorclose = 'Open' then time end as OpenTime,
      case when openorclose = 'CLOSE' then time end as CloseTime
      from TableName;
      

      【讨论】:

        猜你喜欢
        • 2019-07-29
        • 2018-11-01
        • 2013-07-10
        • 1970-01-01
        • 1970-01-01
        • 2018-06-09
        • 1970-01-01
        • 2013-09-06
        • 1970-01-01
        相关资源
        最近更新 更多