查询语句问题,高手来帮帮忙

来源:百度知道 编辑:UC知道 时间:2024/09/23 04:38:15
一张表tab,有A,B,C,D,E,F等字段,现在
现在根据A字段过筛选,按B字段的组,根据C字段取了最大值。
语句如下:
SELECT B ,max(c) FROM tab where STATUS='XX' GROUP BY B
问题:
现在显示出来的表只有只有分组后的 B字段 和 C字段 的最大值,但是我想要的是这些记录对应的所有字段(即整条记录)都显示出来,而不只是显示两个字段,请问该如何写?
语句没有该好,应该是:
SELECT B ,max(C) FROM tab where A='XX' GROUP BY B

好像都不对a,为什么通不过.....只是多显示几个字段 而已,就这么难吗??.......

--按name分组取val最大的值所在行的数据。
--方法1:
select a.* from tb a where val = (select max(val) from tb where name = a.name) order by a.name
--方法2:
select a.* from tb a where not exists(select 1 from tb where name = a.name and val > a.val)
--方法3:
select a.* from tb a,(select name,max(val) val from tb group by name) b where a.name = b.name and a.val = b.val order by a.name
--方法4:
select a.* from tb a inner join (select name , max(val) val from tb group by name) b on a.name = b.name and a.val = b.val order by a.name
--方法5
select a.* from tb a where 1 > (select count(*) from tb where name = a.name and val > a.val ) order by a.name

上面的答案已经可以了

select a.* from tab a inner join (SELECT B ,max(c) as c FROM tab where STATUS='XX' GROUP BY B) b on a.b=b.b and a.c=b.c

select * from tab dd where A='XX' and exists(SELECT B,max(C) FROM tab where A='XX' GROUP BY B having dd.B=B and dd.C=max(C))

PS:如