EDIT: I have written it a bit wrong gill change my Q
I’m a newbie with SQL and I have a Q..
I made 2 Temp. Tables.
Each has 25 Rows.(DateValue)
I want to combine this 2 tables in a third table..
First Table is [From]
Second Table is [To]…
Both tables have different values
I want to get it like this:
From| To |
1111|2222
2222|3333
3333|4444
etc..
I use this simple Query
Create Table #T3
(
[From] Datetime
,[To] Datetime
)
INSERT Into #T3
SELECT Distinct #T1.[From], #T2.[To]
From #T1,#T2
Where #T1.[From] is not null
And #T2.[To] is not null
Select * from #T3
Drop Table #T3
Drop Table #T2
Drop Table #T1
But my results are like this
From| To |
1111|1111
1111|2222
1111|3333
2222|1111
2222|2222
2222|3333
It multiplies the first field with the second wich gives me alot more records back..
Any help ?
THANKS !
After the OP’s edit
This may work as you want (which is not entirely clear):
Using
results in all combinations or rows of
T1andT2. It’s called a cross product and (properly) used withCROSS JOIN, like this:When you want to join the two tables based on a condition (and not get the cross product), you use a
JOINorINNER JOIN(these two are same thing):will get you all rows combinations where
T1.FrommatchesT2.To(on equality). I suppose you wanted to match every row ofT1with the row ofT2whereT2.Towas just larger thanT1.Fromso I used the “smaller than”<operator instead of the “equality”=operator.The
GROUP BYandMIN()were added to get only the one with smallestT2.Tofrom those rows.