我想從這個子查詢返回的第二個項目返回第二項:從子查詢
set GrowerNumber =(select top 1 tea_no
from ktda_file
where ktda_file.fosa_acno=customer.fosa_acno)
我想從這個子查詢返回的第二個項目返回第二項:從子查詢
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項也是如此 –