2011-04-30 68 views
4

选择不同的周一yyyy格式输出sorty我有与下列数据的表列datetimeSQL服务器:由降序排列

2011-03-23 
2011-04-19 
2011-04-26 
2011-05-26 

我要选择通过报告日期倒序排列不同mon-yyyy格式输出。我们需要在SQL语句中

这个SQL工作只选择一列,但我想通过ReportDate列订购

SELECT distinct SUBSTRING (convert(varchar, ReportDate, 100),1,3) +'-'+ 
     SUBSTRING (convert(varchar, ReportDate, 100),8,4) 
    FROM [EnvelopsDB].[dbo].[Envelopes] 

输出

Apr-2011 
Mar-2011 
May-2011 

这SQL提供了一个错误:

SELECT distinct SUBSTRING (convert(varchar, ReportDate, 100),1,3) +'-'+ 
     SUBSTRING (convert(varchar, ReportDate, 100),8,4) 
    FROM [EnvelopsDB].[dbo].[Envelopes] 
    order by ReportDate 

错误:

Msg 145, Level 15, State 1, Line 2
ORDER BY items must appear in the select list if SELECT DISTINCT is specified.

什么是最好的SQL查询来获取输出,我需要?

回答

5
with testdata as 
(
    select cast('2011-03-23' as datetime) as d 
union all 
    select cast('2011-04-19' as datetime) 
union all 
    select cast('2011-04-26' as datetime) 
union all 
    select cast('2011-05-26' as datetime) 
) 
SELECT DATENAME(month,d)+'-'+DATENAME(year,d) 
FROM testdata 
GROUP BY DATEPART(year,d), DATEPART(month,d), DATENAME(month,d),DATENAME(year,d) 
ORDER BY DATEPART(year,d), DATEPART(month,d) 

SELECT DATENAME(month,ReportDate)+'-'+DATENAME(year,ReportDate) 
FROM [EnvelopsDB].[dbo].[Envelopes] 
GROUP BY DATEPART(year,ReportDate), DATEPART(month,ReportDate), DATENAME(month,ReportDate),DATENAME(year,ReportDate) 
ORDER BY DATEPART(year,ReportDate), DATEPART(month,ReportDate) 
+0

这并没有给我想要的结果。我希望像这样'SELECT DISTINCT right(convert(varchar,ReportDate,106),8) FROM [EnvelopsDB]。[dbo]。[Envelopes] ORDER BY ReportDate desc' – sfgroups 2011-04-30 04:46:33

+0

您希望它按日期顺序排序吗? – Hogan 2011-04-30 04:51:07

+0

@sfgroups - 已修复。 – Hogan 2011-04-30 04:56:42

2

您可以在ReportDate说明使用GROUP BY而不是DISTINCT这样

SELECT SUBSTRING (convert(varchar, ReportDate, 100),1,3) +'-'+ 
     SUBSTRING (convert(varchar, ReportDate, 100),8,4) 
FROM [EnvelopsDB].[dbo].[Envelopes] 
GROUP BY SUBSTRING (convert(varchar, ReportDate, 100),1,3) +'-'+ 
      SUBSTRING (convert(varchar, ReportDate, 100),8,4) 

随着顺序由使用ROW_NUMBER(),而不是按组。

select substring(convert(varchar, Env.ReportDate, 100),1,3) +'-'+ 
      substring(convert(varchar, Env.ReportDate, 100),8,4) 
from (select 
     ReportDate, 
     row_number() over(partition by datepart(year, ReportDate), datepart(month, ReportDate) 
          order by (select 1)) as rn 
     from [EnvelopsDB].[dbo].[Envelopes]) as Env  
where Env.rn = 1 
order by Env.ReportDate desc 
+0

从这个SQL如何排序结果按降序排列。 – sfgroups 2011-04-30 04:43:11

1

如果你不介意在结果集中一个额外的列,那么这将正常工作。

SELECT DISTINCT 
    REPLACE(RIGHT(CONVERT(VARCHAR(11), ReportDate, 106), 8), ' ', '-') AS [Mon-YYYY], 
    RANK() OVER(ORDER BY CONVERT(VARCHAR(7), ReportDate, 120) /* [YYYY-MM]*/ DESC) AS r_order 
FROM [EnvelopsDB].[dbo].[Envelopes] 
ORDER BY r_order DESC 
如果你不想提供的MMM-YYYY列别名(然后你可以在ORDER BY使用),你不能只是做** ORDER BY 1个DESC **? SELECT DISTINCT SUBSTRING(转换(VARCHAR,ReportDate,100),1,3)+ ' - '。+ SUBSTRING(转换(VARCHAR,ReportDate,100),8,4) FROM [EnvelopsDB] [DBO] [信封] ORDER BY 1 DESC 或者只是添加列别名: SELECT DISTINCT SUBSTRING(转换(VARCHAR,ReportDate,100),1,3)+ ' - ' + SUBSTRING(转换(VARCHAR ,ReportDate,100),8,4)AS ReportDate FROM [EnvelopsDB]。[dbo]。[Envelopes] ORDER BY ReportDate DESC
+0

没有。这将排序格式化的值 – gbn 2011-04-30 08:51:32

+0

啊 - 我明白了。原来的问题并不清楚。我现在重新阅读并看到那部分内容。 – beach 2011-04-30 16:30:56

2

我想有一个类似的问题,这里最近,我现在找不到,但得到的答复是东西这样的效果:

SELECT 
    SUBSTRING(CONVERT(varchar, ReportDate, 100), 1, 3) + '-' + 
    SUBSTRING(CONVERT(varchar, ReportDate, 100), 8, 4) 
FROM [EnvelopsDB].[dbo].[Envelopes] 
GROUP BY 
    SUBSTRING(CONVERT(varchar, ReportDate, 100), 1, 3), 
    SUBSTRING(CONVERT(varchar, ReportDate, 100), 8, 4) 
ORDER BY MIN(ReportDate) 

此外,当您选择目前的输出方式格式为mmm-yyyy基本上没问题,我可能会略有不同。这里:

SELECT 
    LEFT(DATENAME(month, ReportDate), 3) + '-' + 
    DATENAME(year, ReportDate) 
FROM [EnvelopsDB].[dbo].[Envelopes] 
GROUP BY 
    DATENAME(month, ReportDate), 
    DATENAME(year, ReportDate) 
ORDER BY MIN(ReportDate)