-1

我有这张桌子:

 pedido data
 314    2013-04-05 00:00:00.000
 318    2013-04-05 00:00:00.000
 319    2013-04-05 00:00:00.000          
 26025  2013-07-01 00:00:00.000
 26026  2013-07-01 00:00:00.000 
 26026  2013-07-01 00:00:00.000
 26026  2013-07-01 00:00:00.000
 26026  2013-07-01 00:00:00.000
 26027  2013-07-01 00:00:00.000
 26029  2013-07-01 00:00:00.000
 26029  2013-07-01 00:00:00.000

我想要这个结果:

 314        2013-04-05
 318        2013-04-05
 319        2013-04-05
26025       2013-07-01
26026       2013-07-01
26027       2013-07-01
26029       2013-07-01

我尝试过这个但不工作:

   select pedido,data from alteraca  group by data,pedido order by pedido  

还有这个 :

  select pedido, (CONVERT varchar(10),data,101)  as fecha1  from alteraca 

这段代码对我有用:

      select pedido, MIN (data) as data from alteraca 
      group by pedido order by pedido

一个小细节我如何只显示日期部分

  14        2013-04-05
  318       2013-04-05

不是

  14        2013-04-05 00:00:00.000 
  318       2013-04-05 00:00:00.000
4

4 回答 4

2

你已经找到了group by。请检查文档以了解确切的语法。假设您想要最低日期,您可以这样做:

select 
  pedido,
  min(data) as data 
from alteraca  
group by pedido 
order by pedido 

如果您只想删除重复项:

select distinct
  pedido,
  data
from alteraca  
order by pedido 

无论哪种情况,如果您想转换为 char,您都可以date与之交换。(CONVERT varchar(10), data) as data

于 2013-07-30T20:06:58.083 回答
0

试试这个:

 select Distinct pedido, CONVERT(varchar(10),data)  as fecha1  from alteraca
于 2013-07-30T20:07:12.027 回答
0

您的查询已经对了!

select pedido,data from alteraca  group by data,pedido order by pedido

看看这个SQL Fiddle 演示

输出(来自小提琴):

PEDIDO  DATA
314         April, 05 2013 00:00:00+0000
318         April, 05 2013 00:00:00+0000
319         April, 05 2013 00:00:00+0000
26025   July, 01 2013 00:00:00+0000
26026   July, 01 2013 00:00:00+0000
26027   July, 01 2013 00:00:00+0000
26029   July, 01 2013 00:00:00+0000
于 2013-07-30T20:14:11.503 回答
0

因为您使用的是 mssql 2008,所以可以使用 cast(data as date):

SELECT pedido, cast(min(data) as date) as data
FROM table
GROUP BY pedido
ORDER BY pedido

如果您想转换为 char(10) 或使用早期版本的 sqlserver,您可以使用此语法,这里的优点是您将始终在输出中获得相同的格式 - 与您的问题中描述的完全一样:

SELECT pedido, convert(char(10), min(data), 120) as data
FROM table
GROUP BY pedido
ORDER BY pedido
于 2013-07-30T21:03:45.853 回答