sql两个命令通过给出不同的结果

时间:2017-01-03 03:58:15

标签: mysql sql-server

我在下面有这个sql查询,我的日期有2个订单

02/01/2016 - 03/01/2017

02/03/2011 - 07/07/2016

12/22/2010 - 07/07/2016

02/01/2016 - 02/01/2016

12/22/2010 - 07/07/2013

这是我使用下面的查询时的结果。但问题是它不符合我的预期。我想先以desc顺序订购DateEnd,然后再订购DateStarted。

@sources

1 个答案:

答案 0 :(得分:0)

您可以在主查询之外使用另一个select子句,然后像这样应用order by子句。我希望这会有效。

SELECT * from (
SELECT  top 5  case when dbo.VoluntaryWork.Organization + ' - ' + dbo.VoluntaryWork.OrganizationAddress = ' - ' then 'N/A' 
    else dbo.VoluntaryWork.Organization + ' - ' + dbo.VoluntaryWork.OrganizationAddress end AS OrgAddress, 
    case when isnull(CONVERT(varchar(10), dbo.VoluntaryWork.DateStarted, 101),'') = '' or isnull(CONVERT(varchar(10), dbo.VoluntaryWork.DateStarted, 101),'') = '01/01/1900' then 'N/A' else isnull(CONVERT(varchar(10), dbo.VoluntaryWork.DateStarted, 101),'') end AS DateStarted, 
    case when isnull(CONVERT(varchar(10), dbo.VoluntaryWork.DateEnded, 101),'') = '' or isnull(CONVERT(varchar(10), dbo.VoluntaryWork.DateEnded, 101),'') = '01/01/1900' then 'N/A' else isnull(CONVERT(varchar(10), dbo.VoluntaryWork.DateEnded, 101),'') end AS DateEnded, 
    FROM         dbo.PersonVoluntaryWork INNER JOIN
                      dbo.VoluntaryWork ON dbo.PersonVoluntaryWork.VoluntaryWorksId = dbo.VoluntaryWork.VoluntaryWorksId
WHERE     (dbo.PersonVoluntaryWork.PersonId = @PersonId)
    ORDER BY dbo.VoluntaryWork.DateEnded DESC
    ) A ORDER BY A.DateStarted