tsql query

sa 1 Reputation point
2022-09-21T06:00:24.667+00:00

drop table if exists accounts
go
create table accounts ( seqNo Int identity(1,1), AccountId int , type varchar(122) ,clientid varchar(32))

insert into accounts (AccountId, type , clientid)

select 4563 ,'Parent' , '11' union all
select 2345 ,'parent' , '345' union all
select 1234 ,'Parent' , '45' union all
select 4564 ,'sibling' , '11' union all
select 2346 ,'sibling' , '345' union all
select 1235 ,'sibling' , '45'

select * from accounts
/*
-- expected output group by clientId order by seqno

seqno accountId Type clientId
1 4563 parent 11
4 4564 sibling 11
2 2345 parent 345
5 2346 sibling 345
3 1234 parent 45
6 1235 sibling 45

*/

Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,601 questions
{count} votes

1 answer

Sort by: Most helpful
  1. LiHongMSFT-4306 25,651 Reputation points
    2022-09-21T06:06:30.157+00:00

    Hi @sa
    You could Order By two columns to make tie breaker. Try this:

    select *  
    from accounts  
    order by clientid,seqNo  
    

    Best regards,
    LiHong


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments