在表T1中
tag value date_time
"A" 5 "2011-02-04 11:12:52"
"A" 9 "2011-02-04 14:33:46"
"A" 1 "2011-03-22 08:55:05"
"B" 7 "2011-01-21 22:34:57"
"B" 4 "2011-04-11 04:25:43"
请问怎么才能用一条SQL查出每个tag的最大值和最大值对应的时间, 最小值和最小值对应的时间 ?
------解决方案--------------------------------------------------------
SELECT * from tth a where not exists(select 1 from tth where a.tag=tag and a.value>value)
or
not exists(select 1 from tth where a.tag=tag and a.value<value)
------解决方案--------------------------------------------------------
- SQL code
select * from t1 twhere not exists (select 1 from t1 where tag=t.tag and value<t.value)unoin allselect * from t1 twhere not exists (select 1 from t1 where tag=t.tag and value>t.value)