[英]How to select the earliest date and data that corresponds to it
I have a very simple question that's kicking my ass. 我有一个非常简单的问题就是踢我的屁股。
CUSTNMBR | first_date | SOPNUMBE
----------------------------------------
3344771005 | 2012-05-03 | 334471961748
3344771005 | 2012-04-04 | 334476873726
In the above table i want to return the earliest date along with the custnumbr
and sopnumbe
so it'll look like this 在上表中,我希望将最早的日期与
custnumbr
和sopnumbe
一起返回,所以它看起来像这样
3344771005 | 2012-04-04 | 334476873726
I used this 我用过这个
Select a.CUSTNMBR, min(a.Tax_Date) as first_date, a.SOPNUMBE
from SOP30200 as a
where a.CUSTNMBR = '3344771005'
Group by a.CUSTNMBR, a.SOPNUMBE
but it returns all variables and if I knock off the a.sopnumbe
in group it errors out. 但是它会返回所有变量,如果我在组中敲掉
a.sopnumbe
就会出错。
Try this: 尝试这个:
Select top 1 a.CUSTNMBR, a.Tax_Date as first_date, a.SOPNUMBE
from SOP30200 as a
where a.CUSTNMBR = '3344771005'
order by a.Tax_Date asc
Try 尝试
Select TOP 1 a.CUSTNMBR, min(a.Tax_Date)as first_date, a.SOPNUMBE
from SOP30200 as a
where a.CUSTNMBR = '3344771005'
Group by a.CUSTNMBR, a.SOPNUMBE'
ORDER BY 2 ASC
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.