Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Table

S.no Number Stage
1 A-1 1
2 A-2 2
3 A-3 3
4 B-1 1
5 B-2 2
6 c-1 1
7 C-2 2
8 C-3 3
9 D-1 1
10 D-2 2
11 D-3 3
12 D-4 4

I need query for following

3 A-3 3
5 B-2 2
8 C-3 3
12 D-4 4
Posted
Comments
Richard Deeming 24-Sep-15 10:54am    
This is the same question you posted yesterday, with less detail than the previous version.

1 solution

SQL
Select S.no, Number, Stage from table
where S.no in (3,5,8,12)


See what you get when you don't explain yourself?

This answer is correct as it does answer your question. Because I am nice I will interpret your intent:

SQL
--CTE to enumerate the results
with MyCTE as (
    Select 
        S.no, 
        Number, 
        Stage, 
        -- Row_Number will add an incremental number
        Row_Number () OVER (
            -- Split the numbering by first Char of Number ('A', 'B', ...)
            Partition By substring(Number,1,1)  
            -- Order by stage descending so the highest stage will be 1
            Order By Stage desc) as Num
    from table
)
--now select from the CTE:
Select no, Number, Stage
From MyCTE 
Where num = 1
 
Share this answer
 

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