Click here to Skip to main content
15,896,497 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am using the following sql query..


C#
string query = "select * from (select ROW_NUMBER() OVER (ORDER BY style_no) rn ,* from add_product) a where rn BETWEEN(@PageIndex -1) * @PageSize + 1 AND(((@PageIndex -1) * @PageSize + 1) + @PageSize) - 1";



it is giving records something like this...

C#
Style_no               Text             Age
JC001                  Text1            15
JC001                  Text2            18
JC001                  Text5            19
JC002                  Text1            80
JC002                  Text8            45
JC002                  Text9            60


But i want results something like this :

C#
JC001                   Text1              15
JC002                   Text1              80



Each row with same style_no column must be selected only once. i hope you understand what i am saying.

i want to modify above query in such a way that it should work like as it is.. only change i want is that it should select the records only once where style_no is more then once.
Posted
Updated 23-Apr-15 20:19pm
v3
Comments
Tomas Takac 24-Apr-15 2:36am    
What should be then in Text and Age columns? What' the rule? Select rows where Text = Text1?
Maciej Los 24-Apr-15 3:16am    
My virtual 5!

1 solution

SQL
SELECT * FROM  (
    SELECT ROW_NUMBER() OVER (ORDER BY style_no) rn, style_no, text, age FROM 
    (
-- take everything and rank it by style_no
        SELECT ROW_NUMBER() OVER (ORDER BY style_no) rank, style_no, text, age
        FROM add_product
    ) ranking
    WHERE rank = 1 -- take only one of those (you didn't specify any ordering)
) paging
where rn BETWEEN(@PageIndex -1) * @PageSize + 1 AND(((@PageIndex -1) * @PageSize + 1) + @PageSize) - 1;


Or you could play with SELECT TOP 1 * FROM ....but I think the above is cleaner.
 
Share this answer
 
Comments
Maciej Los 24-Apr-15 3:13am    
It was bit complicated what OP wanted to achieve, because there's no duplicates (text, age!). Getting first resolves OP issue, so my 5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900