2011-11-25 64 views
0

我是SQL服务器中的新手。编写一个具有挑战性的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 

我不知道我是否需要写一个存储过程(T-SQL)或有是SQL中的任何内置关键字,使用它我可以得到我想要的输出。我不能创建一个新表,但视图,我的现有模式允许sprocs。

请帮帮我。

TIA

+0

每行有'id = 1'?除了包含“星期一”的“dayoftheweek”列之外,是否有任何将“星期一”行组合在一起的内容? –

回答

2

表结构有可怕的,我愿意做任何事情,你可能要远离它。这就是说mssql有一些你可以使用的子字符串函数。

select dayoftheweek, timeoftheday 'open' 
from uglytable 
where right(dayofweek,4) = 'Open' 

这会给你所有打开的记录。我们会做同样的事情,在一秒钟内完成所有关闭的记录。这将通过阅读开放与关闭的挑战获得

要解决的第二个问题是从该文本中挑选'day'列。每天都以y结尾,并且不包含其他'y',因此我们可以使用charindex

lil免责声明...我没有可用于测试的mssql数据库,所以我可能会将char这里和那里。

select charindex('y',dayoftheweek) 

这将返回'y'字符串的位置。

Mondayopen对第6个字符

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' 

这个现在应该没有了“开放”或开放时间沿着“关闭”返回星期几。把它变成一个子查询,它加入到关闭子查询放在一起:

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 

希望我没有语法错误在那里,没有什么上测试它,并可能有一个错字^^

+0

我应该不应该使用像打开表的别名这样的术语吗? – Twelfth

+0

不,你不应该:-)但是感谢你的惊人想法。我确实把它投入生产。 – BoCode

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