50分求条sql语句。

来源:百度知道 编辑:UC知道 时间:2024/06/06 17:56:07
表结构如下:
+---------------------+---------+---------+-----------+---------+--------+------------------+--------+----------+
| item_date | item_no | deposit | withdrawl | balance | source | balance_adjusted | reason | trans_id |
+---------------------+---------+---------+-----------+---------+--------+------------------+--------+----------+
| 2008-08-05 00:00:00 | 24234 | 50 | 0 | 50 | ch | 0 | NULL | NULL |
| 2008-08-10 00:00:00 | 24234 | 30 | 0 | 80 | ch | 0 | NULL | NULL |
| 2008-08-12 00:00:00 | 24234 | 20 | 0 | 100 | ch | 0 | NULL | NULL |
| 2008-08-20 00:00:00 | 24234 | 100 | 0 | 200 | ch | 0 | NULL | NULL |
| 2008-09-08 00:00:00 | 24234 | 100 | 0 | 300 | ch | 0 | NULL | NULL |
| 2008-09-22 00:47:21 | 24234 | 50 | 0 | 350 | ch | 0 | NULL | NULL |
+---------------------+---------+---------+-----------+---------+--------+------------------+--------+----------+

大家只要看item_date,item_no,balance 3列就可以了。

结果出来:2008-09-22

select a.* from table a,(select item_no,max(item_date) item_date from table
group by item_no) b where a.item_no=b.item_no and a.item_date=b.item_date

select item_no,max(item_date) from table where item_no=24234
group by item_no

如果不确认,那么把where item_no=24234 去掉就可以了
select item_no,max(item_date) from table
group by item_no

select * from from table
where item_date=(select max(item_date)from table )

SELECT * FROM 表名
WHERE item_date IN(SELECT Max(item_date) FROM 表名)

是不是分组求最大值的问题呀.!!