从子查询
问题描述:
我想从这个子查询返回的第二个项目返回第二项:从子查询
set GrowerNumber =(select top 1 tea_no
from ktda_file
where ktda_file.fosa_acno=customer.fosa_acno)
答
SELECT *
FROM customer
OUTER APPLY (
SELECT
Item1 = MAX(CASE WHEN t.RowNum = 1 THEN t.tea_no END),
Item2 = MAX(CASE WHEN t.RowNum = 2 THEN t.tea_no END)
FROM (
SELECT tea_no, RowNum = ROW_NUMBER() OVER (ORDER BY tea_no)
FROM ktda_file
WHERE ktda_file.fosa_acno = customer.fosa_acno
) t
WHERE RowNum < 3
) t2
答
也许这将帮助你:
(select tea_no from (select row_number() over (order by tea_no asc) as rowID, tea_no from ktda_file where ktda_file.fosa_acno=customer.fosa_acno)x where rowID = 2)
答
嗨,你需要做的是这样这(如伊万所说,你没有在你的查询中订购,那么第二个是什么?)尽管如此,假设它是tea_no ...
GrowerNumber = (Select tea_no from (select row_number() over (order by tea_no) as row
from ktda_file where ktda_file.fosa_acno=customer.fosa_acno) as orderedlist
where row = 2)
答
GrowerNumber = (
select top 1
tea_no
from
(
select top 2
tea_no
from
ktda_file
where ktda_file.fosa_acno=customer.fosa_acno
) as a
order by
tea_no desc
)
答
试试这个
WITH CTE AS
(
SELECT top 2 tea_no from ktda_file
WHERE ktda_file.fosa_acno=customer.fosa_acno
ORDER BY tea_no ASC
)
SELECT TOP 1 tea_no FROM CTE ORDER BY tea_no DESC
GrowerNumber =(从ktda_file选择顶部1 tea_no其中ktda_file.fosa_acno = customer.fosa_acno) – njoromwando
你没有指定排序顺序,所以你已经有很好的机会获得第二或第3项也是如此 –