由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Database版 - 如何寫此 SQL 查詢?
相关主题
一个sql问题:怎样实现 (((a1*10)+a2)*10+a3)*10 ... (转载)how to make this query
急问: 请问SQL适合做这种计算吗?问Jackrun, Beijing等大侠performance问题
老印给我的一个Challenge谁有电子版的SQL Server2005书,能否共享一下
better solution for cross table query in sql?请推荐一本自学SQL的书吧
怎么写个query 把输出变成横排.发包子,每周一题
问题:行换列, pivot tableSQL 2008 Create Index vs Rebuild Index (Alter Index)
求教一条MS SQL语句question about MS SQL mirroring...
help: SQL Server数据同步复制open cursor for collection?
相关话题的讨论汇总
话题: 00话题: 06话题: day话题: 08
进入Database版参与讨论
1 (共1页)
i****a
发帖数: 36252
1
Day Open Close
1 08:00 06:00
2 08:00 06:00
3 08:00 06:00
4 08:00 06:00
5 08:00 06:00
6 09:00 05:00
7
so this is the hours of a shop
The goal is to get this into the following format:
Mon - Fri 08:00-06:00, Sat 09:00-05:00
How do I do this??
c**t
发帖数: 2744
2
google pivot query

【在 i****a 的大作中提到】
: Day Open Close
: 1 08:00 06:00
: 2 08:00 06:00
: 3 08:00 06:00
: 4 08:00 06:00
: 5 08:00 06:00
: 6 09:00 05:00
: 7
: so this is the hours of a shop
: The goal is to get this into the following format:

i****a
发帖数: 36252
3
it's not just a pivot
need to combine the dates. if M and T are the same, T-F same, S and S
same, for example, then show:
Mon - Tue 07:00-06:00, Tue - Fri 08:00-06:00, Sat - Sun 10:00-02:00
I couple pivot it into individual columns for MonOpen, MonClose etc and
use a whole bunch of case statements but that's ugly...

【在 c**t 的大作中提到】
: google pivot query
a9
发帖数: 21638
4
用cursor应该比较方便了吧?

【在 i****a 的大作中提到】
: Day Open Close
: 1 08:00 06:00
: 2 08:00 06:00
: 3 08:00 06:00
: 4 08:00 06:00
: 5 08:00 06:00
: 6 09:00 05:00
: 7
: so this is the hours of a shop
: The goal is to get this into the following format:

i****a
发帖数: 36252
5
trying to avoid cursor and loops...

【在 a9 的大作中提到】
: 用cursor应该比较方便了吧?
j*****n
发帖数: 1781
6
check "GROUPING function" in BOL see if it can work in your case.
sorry, no time to think it in deep yet...
b******g
发帖数: 81
7
I don't think SQL is the best option for this functionality.
Are you sure SQL is the only way/tool provided?
a9
发帖数: 21638
8
cursor and loops exists for a reason

【在 i****a 的大作中提到】
: trying to avoid cursor and loops...
f*******h
发帖数: 53
9
Try this one, in sqlserver 2008:
;with cte as (
select 1 as GroupID,DAY,opentime,closetime
from OfficeHours
where DAY=1

union all

select cte.GroupID,o.Day,o.OpenTime,o.CloseTime
from cte,
OfficeHours as o
where o.Day=cte.Day+1
and cte.CloseTime=o.CloseTime
and cte.OpenTime=o.OpenTime

union all

select cte.GroupID+1,o.Day,o.OpenTime,o.CloseTime
from cte,
OfficeHours as o
where o.Day=cte.Day+1
f*******h
发帖数: 53
10
If you are using Oracle 11g, just use "within group" method
i****a
发帖数: 36252
11
oh, did I not list the system I am using? It's SQL 2005. Thanks freshfish. I
'll look at the code you provided.
i****a
发帖数: 36252
12
Excellent, freshfish's code works! Thanks a bunch
I just need to do a row concatenation to get it into 1 line.
f*******h
发帖数: 53
13
Thanks, you just made me rich:)
1 (共1页)
进入Database版参与讨论
相关主题
open cursor for collection?怎么写个query 把输出变成横排.
谁能帮我看看这个oracle function有什么错?问题:行换列, pivot table
SQL Server -- 大家一起来做题 (10个包包)求教一条MS SQL语句
请问sql 有条件性的select columnshelp: SQL Server数据同步复制
一个sql问题:怎样实现 (((a1*10)+a2)*10+a3)*10 ... (转载)how to make this query
急问: 请问SQL适合做这种计算吗?问Jackrun, Beijing等大侠performance问题
老印给我的一个Challenge谁有电子版的SQL Server2005书,能否共享一下
better solution for cross table query in sql?请推荐一本自学SQL的书吧
相关话题的讨论汇总
话题: 00话题: 06话题: day话题: 08