订单日 sql select
order day sql select
美好的一天!
我有一个 table 填充日期时间数据类型,它存储为 nvarchar。
我需要 select 从 table 得到按天排序的结果
日期插入此形状。
>Thursday 11:30-12:30
>Sunday 08:00-09:00
>Sunday 08:00-09:00
>Sunday 08:00-09:00
>Sunday 08:00-09:00
>Monday 08:00-09:00
>Monday 09:00-10:00
>Monday 08:00-09:00
我试过用这个
SELECT TOP 1000 [Class_Time_ID] ,[Class_ID] ,[Class_Date]
FROM [School].[dbo].[Class_Time_Table]
ORDER BY CASE Class_Date WHEN 'Monday' THEN 2
WHEN 'Tuesday' THEN 3
WHEN 'Wednesday' THEN 4
WHEN 'Thursday' THEN 5
WHEN 'Sunday' THEN 1
end
一个Class_Date
(例如'Monday 09:00-10:00'
)永远不会等于 'Monday'
,但它就像'Monday%'
:
SELECT
class_time_id,
class_id,
class_date
FROM school.dbo.class_time_table
ORDER BY CASE
WHEN class_date LIKE 'Monday%' THEN 2
WHEN class_date LIKE 'Tuesday%' THEN 3
WHEN class_date LIKE 'Wednesday%' THEN 4
WHEN class_date LIKE 'Thursday%' THEN 5
WHEN class_date LIKE 'Sunday%' THEN 1
END
使用 CROSS APPLY
可能会有所帮助,因为它允许在 select
和 where
子句中使用 case expression
的别名。例如
MS SQL Server 2014 架构设置:
CREATE TABLE Class_Time_Table
([Class_Date] varchar(20), [Class_Time_ID] int ,[Class_ID] int)
;
INSERT INTO Class_Time_Table
([Class_Date])
VALUES
('Thursday 11:30-12:30'),
('Sunday 08:00-09:00'),
('Sunday 08:00-09:00'),
('Sunday 08:00-09:00'),
('Sunday 08:00-09:00'),
('Monday 08:00-09:00'),
('Monday 09:00-10:00'),
('Monday 08:00-09:00')
;
查询 1:
SELECT
ctt.*
, ca.day_of_week,ca.start_time,ca.end_time
FROM Class_Time_Table ctt
CROSS APPLY (
SELECT
CASE
WHEN class_date LIKE 'Monday%' THEN 2
WHEN class_date LIKE 'Tuesday%' THEN 3
WHEN class_date LIKE 'Wednesday%' THEN 4
WHEN class_date LIKE 'Thursday%' THEN 5
WHEN class_date LIKE 'Sunday%' THEN 1
END AS day_of_week
, SUBSTRING(RIGHT(class_date, 11), 1, 5) AS start_time
, SUBSTRING(RIGHT(class_date, 5) , 1, 5) AS end_time
) ca
order by day_of_week, start_time, end_time
| Class_Date | Class_Time_ID | Class_ID | day_of_week | start_time | end_time |
|----------------------|---------------|----------|-------------|------------|----------|
| Sunday 08:00-09:00 | (null) | (null) | 1 | 08:00 | 09:00 |
| Sunday 08:00-09:00 | (null) | (null) | 1 | 08:00 | 09:00 |
| Sunday 08:00-09:00 | (null) | (null) | 1 | 08:00 | 09:00 |
| Sunday 08:00-09:00 | (null) | (null) | 1 | 08:00 | 09:00 |
| Monday 08:00-09:00 | (null) | (null) | 2 | 08:00 | 09:00 |
| Monday 08:00-09:00 | (null) | (null) | 2 | 08:00 | 09:00 |
| Monday 09:00-10:00 | (null) | (null) | 2 | 09:00 | 10:00 |
| Thursday 11:30-12:30 | (null) | (null) | 5 | 11:30 | 12:30 |
美好的一天! 我有一个 table 填充日期时间数据类型,它存储为 nvarchar。 我需要 select 从 table 得到按天排序的结果 日期插入此形状。
>Thursday 11:30-12:30
>Sunday 08:00-09:00
>Sunday 08:00-09:00
>Sunday 08:00-09:00
>Sunday 08:00-09:00
>Monday 08:00-09:00
>Monday 09:00-10:00
>Monday 08:00-09:00
我试过用这个
SELECT TOP 1000 [Class_Time_ID] ,[Class_ID] ,[Class_Date]
FROM [School].[dbo].[Class_Time_Table]
ORDER BY CASE Class_Date WHEN 'Monday' THEN 2
WHEN 'Tuesday' THEN 3
WHEN 'Wednesday' THEN 4
WHEN 'Thursday' THEN 5
WHEN 'Sunday' THEN 1
end
一个Class_Date
(例如'Monday 09:00-10:00'
)永远不会等于 'Monday'
,但它就像'Monday%'
:
SELECT
class_time_id,
class_id,
class_date
FROM school.dbo.class_time_table
ORDER BY CASE
WHEN class_date LIKE 'Monday%' THEN 2
WHEN class_date LIKE 'Tuesday%' THEN 3
WHEN class_date LIKE 'Wednesday%' THEN 4
WHEN class_date LIKE 'Thursday%' THEN 5
WHEN class_date LIKE 'Sunday%' THEN 1
END
使用 CROSS APPLY
可能会有所帮助,因为它允许在 select
和 where
子句中使用 case expression
的别名。例如
MS SQL Server 2014 架构设置:
CREATE TABLE Class_Time_Table
([Class_Date] varchar(20), [Class_Time_ID] int ,[Class_ID] int)
;
INSERT INTO Class_Time_Table
([Class_Date])
VALUES
('Thursday 11:30-12:30'),
('Sunday 08:00-09:00'),
('Sunday 08:00-09:00'),
('Sunday 08:00-09:00'),
('Sunday 08:00-09:00'),
('Monday 08:00-09:00'),
('Monday 09:00-10:00'),
('Monday 08:00-09:00')
;
查询 1:
SELECT
ctt.*
, ca.day_of_week,ca.start_time,ca.end_time
FROM Class_Time_Table ctt
CROSS APPLY (
SELECT
CASE
WHEN class_date LIKE 'Monday%' THEN 2
WHEN class_date LIKE 'Tuesday%' THEN 3
WHEN class_date LIKE 'Wednesday%' THEN 4
WHEN class_date LIKE 'Thursday%' THEN 5
WHEN class_date LIKE 'Sunday%' THEN 1
END AS day_of_week
, SUBSTRING(RIGHT(class_date, 11), 1, 5) AS start_time
, SUBSTRING(RIGHT(class_date, 5) , 1, 5) AS end_time
) ca
order by day_of_week, start_time, end_time
| Class_Date | Class_Time_ID | Class_ID | day_of_week | start_time | end_time |
|----------------------|---------------|----------|-------------|------------|----------|
| Sunday 08:00-09:00 | (null) | (null) | 1 | 08:00 | 09:00 |
| Sunday 08:00-09:00 | (null) | (null) | 1 | 08:00 | 09:00 |
| Sunday 08:00-09:00 | (null) | (null) | 1 | 08:00 | 09:00 |
| Sunday 08:00-09:00 | (null) | (null) | 1 | 08:00 | 09:00 |
| Monday 08:00-09:00 | (null) | (null) | 2 | 08:00 | 09:00 |
| Monday 08:00-09:00 | (null) | (null) | 2 | 08:00 | 09:00 |
| Monday 09:00-10:00 | (null) | (null) | 2 | 09:00 | 10:00 |
| Thursday 11:30-12:30 | (null) | (null) | 5 | 11:30 | 12:30 |