SQL语句写法
某视图view1(经过比较复杂的查询条件):
view1
column1 column2 column3
1 2 2
1 3 1
2 4 3
2 5 4
使用
select column1,max(column2) as column2 from view1 group by column1可以得到如下的结果
column1 column2
1 3
2 5
但要求同时显示对应的column3列的数据,即
column1 column2 column3
1 3 1
2 4 4
如果view1是一个table,则可以用
select * from view1 A
where column2 = (select max(column2) from table1 where column1 = A.column1)
但现在是视图,不支持view1 A的写法....
多谢!
问题点数:50、回复次数:7Top
1 楼txlicenhe(马可)回复于 2003-08-01 17:57:52 得分 10
Select * from view1 a join
(select column1,max(column2) as column2 from view1 group by column1) b
on a.column1 = b.column1 and a.column2 = b.column2Top
2 楼pengdali()回复于 2003-08-01 18:07:01 得分 10
select * from view1 tem where column2=(select max(column2) from view1 where column1=tem.column1)Top
3 楼zjcxc(邹建)回复于 2003-08-01 20:20:29 得分 10
select * from view1 a
where column2=(select max(column2) from view1 where column1=a.column1)Top
4 楼ldy(罗大佑)回复于 2003-08-01 20:25:02 得分 10
我觉得除了水园,就是这里星多了
呵呵Top
5 楼nmyou()回复于 2003-08-02 09:30:05 得分 0
是啊,好多星星;)
为什么这样不行呢?
select * from view1 a
where column2=(select max(column2) from a where column1=view1.column1)Top
6 楼happydreamer(www.sz.js.cn,www.gyxk.com)回复于 2003-08-02 09:40:43 得分 10
select * from view1 a
where column2=(select max(column2) from a where column1=view1.column1)
---这里的a不对 改成viewname
view1.column1 改成a.columnTop
7 楼nmyou()回复于 2003-08-02 10:57:52 得分 0
哦,多谢各位了Top




