SQL中带有DISTINCT的ORDER BY
我有如下疑问:SQL中带有DISTINCT的ORDER BY,sql,Sql,我有如下疑问: SELECT DISTINCT Format(partycaseassociatedparty.assigneddate, 'MM/dd/yyyy') AS [*Assigned Date], Format(partycaseassociatedparty.releaseddate, 'MM/dd/yyyy') AS [*Released Date] FROM dbo.partycaseassociatedparty AS PartyCaseA
SELECT DISTINCT Format(partycaseassociatedparty.assigneddate, 'MM/dd/yyyy') AS [*Assigned Date],
Format(partycaseassociatedparty.releaseddate, 'MM/dd/yyyy') AS [*Released Date]
FROM dbo.partycaseassociatedparty AS PartyCaseAssociatedParty
WHERE partycaseassociatedparty.programid = 1
ORDER BY [*assigned date] DESC
此处[*指定日期]为MM/dd/yyyyy格式,因此在排序时(按顺序排序),它将基于MM/dd/yyyy进行排序,但我需要基于yyyy/MM/dd进行排序。如何在此处实现此目的?使用
分组方式,而不是使用不同的:
ORDER BY FORMAT(PartyCaseAssociatedParty.AssignedDate,'yyyy/MM/dd') DESC
SELECT Format(pcap.assigneddate, 'MM/dd/yyyy') AS [*Assigned Date],
Format(pcap.releaseddate, 'MM/dd/yyyy') AS [*Released Date]
FROM dbo.partycaseassociatedparty AS pcap
WHERE pcap.programid = 1
GROUP BY Format(pcap.assigneddate, 'MM/dd/yyyy'), Format(pcap.releaseddate, 'MM/dd/yyyy')
ORDER BY max(pcap.assigneddate) DESC;
使用分组依据
时,可以在order by
子句中使用聚合函数。order by PartyCaseAssociatedParty.AssignedDate这看起来像是MS Access或SQL Server查询。为什么它被标记为“mysql”?它显示错误,如“如果指定了select DISTINCT,则ORDER BY items必须出现在选择列表中”。它显示错误,如“如果指定select DISTINCT,则ORDER BY items必须出现在选择列表中”。查询中需要DISTINCT。@Saritha.S.R。groupby
也做了同样的事情。这是正确的,但我会自动生成查询,因此将出现不同的查询。有没有办法用distinct做同样的事情?@Saritha.S.R。我不这么认为。您可以通过向输出中添加第三列并按该列排序来实现这一点。