我是SQL服务器的新手。
我有下表:
id dayoftheweek timeoftheday
1 mondayOpen 0:00
1 mondayClose 23:59
1 tuesdayOpen 0:00
1 tuesdayClose 23:59
1 wednesdayOpen 0:00
1 wednesdayClose 23:59
1 thursdayOpen 0:00
1 thursdayClose 23:59
1 fridayOpen 0:00
1 fridayClose 23:59
1 saturdayOpen 0:00
1 saturdayClose 23:59
1 sundayOpen 0:00
1 sundayClose 23:59
我想要以下内容:
id day open close
1 monday 0:00 23:59
2 tuesday 0:00 23:59
我不知道是否需要编写一个sproc(T-SQL)或者SQL中是否有任何内置关键字,使用它我可以得到我想要的输出。我无法创建新表,但在我现有的架构中允许使用视图。
请帮帮我。
TIA
答案 0 :(得分:2)
表格结构很可怕,我会做任何你能做的事情来摆脱它。那就是说mssql有一些你可以使用的子串函数。
select dayoftheweek, timeoftheday 'open'
from uglytable
where right(dayofweek,4) = 'Open'
这将为您提供所有已打开的记录。我们会做同样的事情,以便在一秒钟内获得所有已关闭的记录。这将是阅读开放与封闭
的挑战要解决的第二个问题是从该文本中选择“日期”列。每天都以y结尾,其中不包含其他'y',所以我们可以使用charindex
lil免责声明......我没有可用于测试的mssql数据库,所以我可能会在这里和那里成为一个字符。
select charindex('y',dayoftheweek)
这将返回'y'字符的字符串中的位置。
Mondayopen在第6个角色上有一个y
select left('mondayopen',6)
这将返回mondayOpen或mondayClose的左侧6个字符。把两者放在一起:
select left(dayoftheweek,charindex('y',dayoftheweek))
希望我做对了......它应该是mondayopen和mondayclose到星期一。把两者放在一起:
select left(dayoftheweek,charindex('y',dayoftheweek)) dayofweek, timeoftheday 'open'
from uglytable
where right(dayofweek,4) = 'Open'
现在应该在没有'开放'或'关闭'以及开放时间的情况下返回每周一天。将其转换为子查询,将其连接到close子查询并放在一起:
select open.dayofweek, open.open,close.close
from
(select left(dayoftheweek,charindex('y',dayoftheweek)) dayofweek, timeoftheday 'open'
from uglytable
where right(dayofweek,4) = 'Open') open
inner join
(select left(dayoftheweek,charindex('y',dayoftheweek)) dayofweek, timeoftheday 'close'
from uglytable
where right(dayofweek,5) = 'Close') close
on open.dayofweek = close.dayofweek
希望我没有语法错误,没有什么可以测试它,可能会有错误^^
答案 1 :(得分:0)
当然,这是非常糟糕的数据库设计。用于存储您需要在DATE and TIME类型上使用的日期。
试试这个:
declare @tbl table(id int, dayoftheweek varchar(50), timeoftheday varchar(10))
insert into @tbl values (1, 'mondayOpen', '0:00')
insert into @tbl values (1, 'mondayClose', ' 23:59')
insert into @tbl values (1, 'tuesdayOpen', ' 0:00')
insert into @tbl values (1, 'tuesdayClose', ' 23:59')
insert into @tbl values (1, 'wednesdayOpen', '0:00')
insert into @tbl values (1, 'wednesdayClose', '23:59')
insert into @tbl values (1, 'thursdayOpen', ' 0:00')
insert into @tbl values (1, 'thursdayClose', '23:59')
insert into @tbl values (1, 'fridayOpen', '0:00')
insert into @tbl values (1, 'fridayClose', '23:59')
insert into @tbl values (1, 'saturdayOpen', ' 0:00')
insert into @tbl values (1, 'saturdayClose', '23:59')
insert into @tbl values (1, 'sundayOpen', '0:00')
insert into @tbl values (1, 'sundayClose', '23:59')
insert into @tbl values (2, 'mondayOpen', '0:00')
insert into @tbl values (2, 'mondayClose', ' 23:59')
insert into @tbl values (2, 'tuesdayOpen', ' 0:00')
insert into @tbl values (2, 'tuesdayClose', ' 23:59')
insert into @tbl values (2, 'wednesdayOpen', '0:00')
insert into @tbl values (2, 'wednesdayClose', '23:59')
insert into @tbl values (2, 'thursdayOpen', ' 0:00')
SELECT tOpen.id, tOpen.DayOfTheWeek, tOpen.timeoftheday OpenTime, tClose.timeoftheday CloseTime
FROM (SELECT REPLACE(dayoftheweek,'open','') DayOfTheWeek, timeoftheday,id
FROM @tbl
WHERE LEN(REPLACE(dayoftheweek,'open',''))<LEN(dayoftheweek)) tOpen
JOIN
(SELECT REPLACE(dayoftheweek,'close','') DayOfTheWeek, timeoftheday,id
FROM @tbl
WHERE LEN(REPLACE(dayoftheweek,'close',''))<LEN(dayoftheweek)) tClose ON tOpen.DayOfTheWeek=tClose.DayOfTheWeek AND tOpen.id=tClose.id