看到我的另一个问题,因为这个很糟糕:
How can I group on the SAME table, order by date and limit the number of rows returned?
答案 0 :(得分:1)
SELECT id_field, fe.*
FROM User_Field uf
CROSS APPLY
(
SELECT TOP (@limit)
value, dateUsed
FROM Field_E2PValue fe
JOIN E2P_Value e
ON e.id = fe.id_e2pvalue
WHERE id_field = uf.id_field
ORDER BY
date_used DESC
) fe
WHERE id_user = @myuser