-1

使用 Microsoft SQL Server 2012,我正在尝试创建一个新视图,该视图将在描述匹配时返回一行的真实开始和结束。

我有一张看起来像这样的桌子

+----+-------+-------+-----+
| ID | desc  | start | end | 
+----+-------+-------+-----+
|  1 | line1 |     0 | 100 |
|  2 | line2 |     0 | 100 |
|  3 | line2 |   101 | 200 | 
+----+-------+-------+-----+

我想再添加 2 列,这样当 desc 匹配时,它会返回来自 ID 2 的 0 和来自 ID 3 的 200,如下所示:

+----+-------+-------+-----+------------+----------+
| ID | desc  | start | end | real_start | real_end |
+----+-------+-------+-----+------------+----------+
|  1 | line1 |     0 | 100 |          0 |      100 |
|  2 | line2 |     0 | 100 |          0 |      200 |
|  3 | line2 |   101 | 200 |          0 |      200 |
+----+-------+-------+-----+------------+----------+

到目前为止我的语法:

CREATE VIEW tableview1 
AS 
     SELECT 
         ID, desc,
         start, end,
         (???) as real_start,
         (???) as real_end 
     FROM 
         linetable 
     WHERE 
         condition;

当描述匹配时,我不知道如何对这两行进行分组和比较,并选择两者中最低的。我会自己玩并更新线程。

非常感谢!

4

2 回答 2

0

我认为这会起作用,有人可以确认吗?

    CREATE VIEW tableview1 
AS 
     select 
     t1.ID, t1.desc,
     t1.start, t1.end,
     (select min(start)
        from linetable z
        where z.desc = t1.desc) as real_start,
     (select min(end)
        from linetable z
        where z.desc = t1.desc) as real_end
     from    
     (SELECT 
         ID, desc,
         start, end,
     FROM 
         linetable 
     WHERE 
         condition) as t1
于 2018-11-07T21:29:12.350 回答
0

您的子选择可能只是 MIN()/MAX()desc

于 2018-11-07T19:05:33.167 回答