I have table:
column1 column2 column3 column4 --------------------------------------------- 1 53 6527 111 2 53 6527 111 3 53 6527 111 4 53 6527 111 5 53 6527 222 6 53 6527 222 7 53 6527 333 8 53 6527 333 9 53 6527 444 10 53 6527 444 11 53 6527 444 12 53 6527 444
I need select
if exists three rows with some column4 value if exists just two then two or one. Max is three. After select
i need get result like this:
column1 column2 column3 column4 --------------------------------------------- 1 53 6527 111 2 53 6527 111 3 53 6527 111 5 53 6527 222 6 53 6527 222 7 53 6527 333 8 53 6527 333 9 53 6527 444 10 53 6527 444 11 53 6527 444
Since you are using SQL Server you can use ranking function s to get the result:
select [column1], [column2], [column3], [column4]
from
(
select [column1], [column2], [column3], [column4],
row_number() over(partition by column2, column3, column4 order by column1) rn
from yourtable
) src
where rn <= 3
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.