如何根据MAX条件检索数据 [英] How to retrieve data based on MAX condition

查看:65
本文介绍了如何根据MAX条件检索数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的桌子:

____________________________________________________________________
****** ID ****** post_title_code ****** post_eff_date ****** site **** empno
____________________________________________________________________
****** 01 ******** 1011 ************ 01/10/1999 ******* 1070 ***** 123
****** 02 ******** 1011 ************ 01/10/2000 ******* 1070 ***** 123
****** 03 ******** 1011 ************ 01/10/2001 ******* 1070 ***** 123
****** 04 ******** 1012 ************ 01/10/2002 ******* 1070 ***** 123
****** 05 ******** 1000 ************ 01/10/2003 ******* 1070 ***** 123
****** 06 ******** 1000 ************ 01/10/2000 ******* 1070 ***** 123

现在我有不同的ID,但是post_title_codes在各个地方都相同..现在,我希望结果显示记录,但post_title_code应该是不同的,因为它应该显示ID最高的记录.
例如,上述表格的输出应为"
____________________________________________________________________
****** ID ****** post_title_code ****** post_eff_date ****** site **** empno
____________________________________________________________________
****** 03 ******** 1011 ************ 01/10/2001 ******* 1070 ***** 123
****** 04 ******** 1012 ************ 01/10/2002 ******* 1070 ***** 123
****** 06 ******** 1000 ************ 01/10/2000 ******* 1070 ***** 123

在此输出中,我们可以看到post_title_codes是不同的,并且只有显示为具有最大ID的记录才与它们分离..
就像post_title_code中"1011"的记录一样,它有3条记录,因此它显示了这3条记录中ID最高的记录..

任何帮助将不胜感激..
谢谢..

I Have a Table Like This:

____________________________________________________________________
******ID******post_title_code******post_eff_date******site****empno
____________________________________________________________________
******01********1011************01/10/1999*******1070*****123
******02********1011************01/10/2000*******1070*****123
******03********1011************01/10/2001*******1070*****123
******04********1012************01/10/2002*******1070*****123
******05********1000************01/10/2003*******1070*****123
******06********1000************01/10/2000*******1070*****123

Now i have different ID''s but the post_title_codes are same in various places..Now i want the resultant to display the record but the post_title_code should be distinct and in that it should display the record with highest ID..
E.G the output of the above mentioned Table should be like"
____________________________________________________________________
******ID******post_title_code******post_eff_date******site****empno
____________________________________________________________________
******03********1011************01/10/2001*******1070*****123
******04********1012************01/10/2002*******1070*****123
******06********1000************01/10/2000*******1070*****123

Here in this output we can see that the post_title_codes are distinct and the from them only that record is displayed which had the MAXIMUM ID..
like in post_title_code for ''1011'' had three records so it displayed the one with the highest ID among these three records..

Any help would be highly appreciated..
Thank You..

推荐答案

从tbl中选择max(ID)和post_title_code,无论post_title_code是什么
select max(ID), post_title_code from tblWhatever group by post_title_code


谢谢克里斯蒂安先生. br/>
它正在创建重复项,因为我需要所有列..
就像我选择所有列一样,它要求我在分组依据"子句中使用它们,但我设法纠正了这一点.
非常感谢,您的信誉仍然存在..
Thanks Mr. Christian.

It was creating duplicates coz i had a need of all the columns..
As when i select all the columns it asked me to use them in Group By Clause but that i managed to rectify that..
Thanks a lot and the credit is still urs..


这篇关于如何根据MAX条件检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆