我下面有这个 sql 查询,它有 2 个按我的日期排序的
2016年1月2日 - 2017年1月3日
2011年2月3日 - 2016年7月7日
2010年12月22日 - 2016年7月7日
2016年2月1日 - 2016年2月1日
2010年12月22日 - 2013年7月7日
这是我使用下面的查询时的结果。但问题是它不符合我的预期。我想首先按降序顺序订购 DateEnd,然后订购 DateStarted。
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 ,dbo.VoluntaryWork.DateStarted desc
最佳答案
您好,使用主查询外部的另一个 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
关于mysql - sql 两个 order by 给出不同的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41436204/