0

我想找到特定 id 的最低访问值和整个表的最低值。在一个查询中。

我的标签

ID  VISITED
101 2009
102 2010
103 2011
104 2012
105 2013

我可以在一个查询中完成吗?

现在我确实喜欢:

select
  min(visited)
from
  mytab
where
  id = 100;

然后我做第二个查询。

select
  min(visited)
from
  mytab;

我想要的是下面的东西(但可以让它更简单吗?)

select 
  min( a.visited ),
  min( b.visited )
from
  (select visited from mytab where id=100) as a,
  (select visited from mytab) as b;

Query run for e.g. id 103 and 100 would be:
id 103 will give 2011,2009
id 100 will give null,2009 

谢谢

4

2 回答 2

1
select  min(visited) as GlobalMin
,       min(case when id = 100 then visited end) as MinForId100
from    mytab
于 2013-05-06T12:58:26.507 回答
1

您可以MIN使用CASE

select 
    min(case when id = 100 then visited end) minbyid,
    min(visited) minoverall
from mytab

SQL 小提琴演示

于 2013-05-06T12:59:16.110 回答