[英]Running Order by on column alias
I am trying to run following SQL query on northwind database : 我试图在Northwind数据库上运行以下SQL查询:
SELECT * FROM (
SELECT DISTINCT ROW_NUMBER() OVER (ORDER BY Joinning DESC) rownum,
LastName, Country, HireDate AS Joinning
FROM Employees
WHERE Region IS NOT NULL
) r
It's giving me the error : 这给了我错误:
Invalid column name 'Joinning'.
The 'rownumber' is required for pagination. 分页需要“行号”。
Can anybody please suggest how I can sort on the Joining alias with the rownumber generated ? 有人可以建议我如何根据生成的行号对Joining别名进行排序吗?
--A possible work around -可能的解决方法
Just figured out a work around; 只是想出一个解决办法; please suggest if anything is wrong or need changes : 请提出任何错误或需要更改的建议:
SELECT ROW_NUMBER() OVER (ORDER BY Joinning DESC) rownum,* FROM (
SELECT
LastName, Country, HireDate AS Joinning
FROM Employees
WHERE Region IS NOT NULL
) r
--To put further where clause on row number(what I wanted to do for pagination): -在行号上进一步放置where子句(我想为分页做些什么):
With myres as(
SELECT ROW_NUMBER() OVER (ORDER BY Joinning DESC) rownum,* FROM (
SELECT
LastName, Country, HireDate AS Joinning
FROM Employees
WHERE Region IS NOT NULL
) a
) Select * from myres where myres.rownum > 0 and myres.rownum < = 0+20
Try 尝试
SELECT * FROM (
SELECT DISTINCT ROW_NUMBER() OVER (ORDER BY HireDate DESC) rownum,
LastName, Country, HireDate AS Joinning
FROM Employees
WHERE Region IS NOT NULL
) r
Hope you ahve joinning in your table. 希望你能加入你的餐桌。 The order by clause is usually given at the last of the query like this : 通常在查询的最后给出order by子句,如下所示:
SELECT * FROM ( SELECT DISTINCT ROW_NUMBER() rownum, LastName, Country, HireDate AS Joinning) FROM Employees WHERE Region IS NOT NULL ORDER BY Joinning DESC) SELECT * FROM(SELECT DISTINCT ROW_NUMBER()rownum,LastName,Country,HireDate AS Joining)从员工所在区域不为空的ORDER BY BY DESC
Hope this helps you! 希望这对您有所帮助!
Use Original Name of the field, That will work just fine HireDate
使用该字段的原始名称,将可以正常使用 HireDate
SELECT * FROM (
SELECT DISTINCT ROW_NUMBER() OVER (ORDER BY HireDate DESC) rownum,
LastName, Country, HireDate AS Joinning
FROM Employees
WHERE Region IS NOT NULL
) r
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.