0

I have table which contains data as below:

DECLARE @CheckList TABLE
                   (  
                        RowNumber INT IDENTITY(1,1) INT, 
                        CheckId INT, 
                        Treat INT 
                   )  
INSERT INTO @CheckList 
VALUES (1, 1, 1), (1, 3, 1), (1, 2, 1), (1, 1, 1),
       (1, 3, 1), (1, 2, 1), (1, 1, 3), (1, 3, 3),
       (1, 2, 3);

SELECT DISTINCT Id, CheckId 
FROM @CheckList
WHERE Id = 1

Where I want to select the data from this table as

   Id   CheckId
   ------------
    1   1
    1   3
    1   2

I want the same order of the check id which is available in table. Can you help me with this?

4
  • Are you sure the second Treat set is (1,1,1)? or it is (1,1,2) ? Commented Jan 7, 2020 at 7:41
  • this is sample data but i want in same order which is available in table. i.e. 1 3 2 Commented Jan 7, 2020 at 7:41
  • @Arulkumar yes unfortunately. Commented Jan 7, 2020 at 7:42
  • 1
    Are you missing a column? Your SQL is not valid. There is no id column? How you are inserting data in identity column? Commented Jan 7, 2020 at 7:49

2 Answers 2

7

Retain original ordering by RowNumber identity column

select Id,CheckId
from(
   select distinct Id,CheckId, min(rownumber) over(partition by Id,CheckId) rn
   from @CheckList
   where Id=1
   ) t
order by rn;

Db fiddle

Sign up to request clarification or add additional context in comments.

Comments

-1

you can check this

DECLARE @CheckList TABLE
               (  Id INT ,  CheckId INT, Treat INT )  
INSERT INTO @CheckList 
VALUES (1, 1, 1), (1, 3, 1), (1, 2, 1), (1, 1, 1),(1, 3, 1), (1, 2, 1), (1, 1, 3),(1,3,3), (1, 2, 3);

SELECT DISTINCT Id, CheckId 
FROM @CheckList
WHERE Id = 1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.