Skip to content

Instantly share code, notes, and snippets.

@Brutt
Created April 17, 2019 13:41
Show Gist options
  • Save Brutt/6bbb373e113be2f54cc068ce7a4a7b0d to your computer and use it in GitHub Desktop.
Save Brutt/6bbb373e113be2f54cc068ce7a4a7b0d to your computer and use it in GitHub Desktop.
unpivot3
WITH calendar as (
SELECT 1 as shop_id, 0 as mon, 0 as tue, 1 as wed, 1 as thur, 1 as fri, 0 as sat, 0 as sun FROM dual
UNION ALL
SELECT 2 as shop_id, 1 as mon, 1 as tue, 1 as wed, 1 as thur, 0 as fri, 0 as sat, 0 as sun FROM dual
)
, seq as (SELECT level as lvl FROM dual CONNECT BY level <= 7)
SELECT shop_id,
case when lvl = 1 then 'mon'
when lvl = 2 then 'tue'
when lvl = 3 then 'wed'
when lvl = 4 then 'thur'
when lvl = 5 then 'fri'
when lvl = 6 then 'sat'
when lvl = 7 then 'sun'
end as dayOfWeek,
case when lvl = 1 then mon
when lvl = 2 then tue
when lvl = 3 then wed
when lvl = 4 then thur
when lvl = 5 then fri
when lvl = 6 then sat
when lvl = 7 then sun
end as isWorkDay
FROM calendar, seq
WHERE shop_id = 1
ORDER BY shop_id, lvl
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment