Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7010249
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:58:01+00:00 2026-05-27T21:58:01+00:00

I am using Microsoft Sql Server 2008. with this below query certain rows are

  • 0

I am using Microsoft Sql Server 2008.
with this below query certain rows are fetched with , but it is possible to arrange rows with distinct rows ordered from every table.

select id1,c1,id2,c2,id3,c3
from t1 
left join t2 on t2.fk = t1.pk
left join t3 on t3.fk = t1.pk
order by id3

enter image description here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T21:58:02+00:00Added an answer on May 27, 2026 at 9:58 pm

    You need to sort on id1, id2 and id3

    EDIT: Updated with additional sequencing field

    ;WITH data AS
    (
        select id1,c1,id2,c2,id3,c3, 
               ROW_NUMBER() OVER (PARTITION BY id1, id2 ORDER BY id2) as id2seq,
               ROW_NUMBER() OVER (PARTITION BY id1, id2, id3 ORDER BY id3) as id3seq    
        from t1   
        left join t2 on t2.fk = t1.pk  
        left join t3 on t3.fk = t1.pk  
    )
    select id1, c1, id2, c2, id3, c3
    from data
    order by id1, id2seq, id3seq
    

    EDIT: Completed query for exact results

    ;with data as( 
        select pid 
            , pname 
            , cid 
            , cname 
        from #t1 t1 
        left join #t2 t2 on t2.fk_pid = t1.pid 
    ), cte AS 
    ( 
        select pid 
              , eid 
              , ename 
        from #t1 t1 
             left join #t3 t3 on t3.fk_pid = t1.pid
    ), combine AS
    (
        select d.pid 
            , pname 
            , cid 
            , cname
            , ROW_NUMBER() over(partition by d.pid, cid order by cid) as seq 
            , eid 
            , ename 
          from data d join cte c on d.pid = c.pid 
    ) 
    select pid, pname, cid, cname, eid, ename 
    from combine 
    order by pid, seq, cid
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Microsoft SQL Server 2008 Management Studio to create a relational schema by
With SQL Server 2005 using SSMS, when I run this: SELECT * FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0','Excel
I am using T/SQL in Microsoft SQL Server 2008 I have a table CREATE
Using Microsoft SQL Server 2008, let's say there is a table1 that keeps the
I am developing one web application using asp.net 2005 and Microsoft SQL Server 2008
I am using Microsoft Sql Server 2008. I am using a view ( dbo.building_piclink
Hello I am working on a SQL database Using Microsoft SQL server 2008 R2
I am using Esri ArcMap 10.0 with Microsoft Sql Server 2008 R2 I have
Using Microsoft SQL Server 2008. I have a table of interest rates. I've manually
I am using Microsoft SQL Server 2008. I have a stored procedure. Is there

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.