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 8759055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:42:19+00:00 2026-06-13T14:42:19+00:00

I created this sql DEMO : I have #tbl1 : ___cola__ 1 2 3

  • 0

I created this sql DEMO :

I have #tbl1 :

___cola__
  1
  2
  3
  4

and #tbl2 :

_colb_
  a
  b
  c
  d

I want this :

_colb____|__cola____
  a           1
  b           2
  c           3
  d           4

I have found a solution ( bad IMHO)

SELECT table1.cola, table2.colb FROM
(SELECT cola, ROW_NUMBER() OVER (ORDER BY cola) AS rn1 FROM #tbl1) table1,
(SELECT colb, ROW_NUMBER() OVER (ORDER BY colb) AS rn2 FROM #tbl2) table2
WHERE table1.rn1 = table2.rn2

For knowledge ,
How ELSE can I do it ?

I started with :

SELECT cola , s.f FROM #tbl1 cross apply(select colb as f from #tbl2) s

But there is a problem at the right section.

  • 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-06-13T14:42:21+00:00Added an answer on June 13, 2026 at 2:42 pm

    Here’s an alternative:

    SELECT cola, colb
    FROM (
      SELECT a.cola, b.colb,
             rna=row_number() over (partition by colb
                                    order by cola),
             rnb=row_number() over (partition by cola
                                    order by colb)
      FROM #tbl1 a
      CROSS JOIN #tbl2 b
      ) X
    WHERE rna=rnb;
    

    And another:

    ;WITH
     a1 AS (SELECT cola, ROW_NUMBER() OVER (ORDER BY cola) AS rn1 FROM #tbl1),
     a2 AS (SELECT colb, ROW_NUMBER() OVER (ORDER BY colb) AS rn2 FROM #tbl2)
    SELECT a1.cola, a2.colb
    FROM a1
    JOIN a2 on a1.rn1=a2.rn2;
    

    But to be honest, the 2nd one is just rearranging the parts of the query – the execution plan is exactly the same as what you had. It works, and is the most efficient plan for such a zip-up query.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created my database by this sql query: (CREATE DATABASE + DBName +
I have a script task that executes a dynamically created SQL restore statement. This
I have created this table with a sql query that looks like this $result
I am facing one critical issue in SQL Azure database, i have created this
I have this sql statement: CREATE TABLE [dbo].[User]( [UserId] [int] IDENTITY(1,1) NOT NULL, [FirstName]
I want to create this table in Oracle. This is only the table SQL
Hi there I have an SQL table that looks like this: CREATE TABLE IF
I have been playing with the demo code from this msdn article by Jeffrey
With a table created using this SQL Create Table X ( ID varchar(4) Not
I created this account registration activation script of my own, I have checked it

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.