Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 如何使用ORDERBY case when子句对列表进行排序(SQL Server)_Sql Server_Sql Order By - Fatal编程技术网

Sql server 如何使用ORDERBY case when子句对列表进行排序(SQL Server)

Sql server 如何使用ORDERBY case when子句对列表进行排序(SQL Server),sql-server,sql-order-by,Sql Server,Sql Order By,我试图根据month和id对列表进行排序,这样,如果是某个month和id将在顶部排序,否则将在下面排序 我希望使用order by with case when子句,但我得到以下错误: ORDER BY items must appear in the select list if SELECT DISTINCT is specified. 查询 declare @month int = month(getDate()) select distinct [description], [i

我试图根据
month
id
对列表进行排序,这样,如果是某个
month
id
将在顶部排序,否则将在下面排序

我希望使用order by with case when子句,但我得到以下错误:

ORDER BY items must appear in the select list if SELECT DISTINCT is specified.
查询

declare @month int = month(getDate())

select distinct 
[description],
[item],
[meta]
order by case
when @month = 1 and item = 'A00' then 12
when @month = 2 and item = 'B00' then 11
when @month = 3 and item = 'C00' then 10
when @month = 4 and item = 'D00' then 9
when @month = 5 and item = 'E00' then 8
when @month = 6 and item = 'F00' then 7
when @month = 7 and item = 'G00' then 6
when @month = 8 and item = 'H00' then 5
when @month = 9 and item = 'I00' then 4
when @month = 10 and item = 'J00' then 3
when @month = 11 and item = 'K00' then 2
when @month = 12 and item = 'L00' then 1
else 0 end desc

如何根据两个参数对它们进行排序?

尝试将DISTICNT替换为GROUP BY