user1990383
user1990383

Reputation: 125

How to order a table based on other table in SQL Server

I have one table1

c1  c2
a   1
a   2
a   3
b   1
b   2
b   3

I have one table2

c3  c4
A   I
B   II
C   III

If Cross Join Table1 & Table2 into Table3

c1  c2  c3  c4
a   1   A   I
a   2   A   I
a   3   A   I
b   1   A   I
b   2   A   I
b   3   A   I
a   1   B   II
a   2   B   II
a   3   B   II
b   1   B   II
b   2   B   II
b   3   B   II
a   1   C   III
a   2   C   III
a   3   C   III
b   1   C   III
b   2   C   III
b   3   C   III

If you add one column in Table1 and cross join with Table2 and insert into Table3 the order of Table3 is missing.

So my question is, how to maintain the order based on the Table1?

Upvotes: 0

Views: 254

Answers (1)

pyrospade
pyrospade

Reputation: 8078

Don't worry about the order of inserts. You should include whichever field you want to order by in the insert and use order by when selecting the data.

Try this -

select *
from table3
order by c1, c2

That's as good as your going to get with your current schema.

Upvotes: 1

Related Questions