5

我有一个很容易理解的问题,但对我来说实现起来并不那么简单。

我有一个名为Time的表:

-----------------------------------
 DAY     TIME1     TIME2
-----------------------------------
 1       08.00     09.40
 1       09.40     10.00
 1       10.00     11.40
 2       08.00     08.50
 2       08.50     10.40
 ----------------------------------

我想要得到的是:

-------------------
 DAY     TIME     
-------------------
 1       08.00     
 1       09.40     
 1       10.00     
 1       11.00     
 2       08.00     
 2       08.50     
 2       10.40     
 ------------------

我试过这段代码:

SELECT DISTINCT sub.val FROM (
  SELECT Time1 AS val FROM Time
  UNION ALL
  SELECT Time2 AS val FROM Time
) AS sub

它只返回时间列。我不知道加入“日”专栏。当我试图在“DISTINCT sub.val”之后添加“Day”时,它出错了。怎么做?

非常感谢你。

4

4 回答 4

5
select distinct day, time1 from mytab
union
select distinct day, time2 from mytab
于 2012-04-05T13:40:41.443 回答
3
SELECT DISTINCT sub.Day, sub.val as Time FROM (
  SELECT Day, Time1 AS val FROM Time
  UNION ALL
  SELECT Day, Time2 AS val FROM Time
) AS sub
于 2012-04-05T13:40:36.400 回答
3

试试这个

SELECT DISTINCT *
FROM (
    SELECT day, time1 FROM Time
    UNION ALL SELECT day, time2 FROM Time
) AS x
ORDER BY x.day
于 2012-04-05T13:42:43.450 回答
2

这可能会有所帮助:

SELECT
    DAY,
    TIME1
FROM
    YourTable AS T
UNION
SELECT
    DAY,
    TIME2
FROM
    YourTable AS T
ORDER BY DAY,TIME1
于 2012-04-05T13:44:07.787 回答