I have a table Hub :
and second table Rates :
In this, FromHubId
and ToHubId
are foreign keys from Hub
table I wanna add some data from a file in Rates
table. What I have tried so far is: Create a temp Table and insert values in it:
CREATE TABLE #Table
(FromHub varchar(30),
ToHub varchar(30),
Rate float,
rate40 float,
)
INSERT INTO #Table values('AUCKLAND','AUCKLAND',229.157325588818,341.973239724851),
('AUCKLAND','BLENHEIM',1825.03244430479,2738.13624934331),
('AUCKLAND','CHRISTCHURCH',1977.80399469734,2966.11840915988),
('AUCKLAND','DUNEDIN',2280.99676393793,3422.08272879301),
('AUCKLAND','GREYMOUTH',2432.59314855822,3650.06488860958),
('BLENHEIM','AUCKLAND',1520.66450929195,2280.99676393793),
('BLENHEIM','BLENHEIM',229.157325588818,341.973239724851),
('CHRISTCHURCH','AUCKLAND',1748.64666910852,2622.97000366278),
('CHRISTCHURCH','DUNEDIN',911.92863926627,1367.89295889941),
('CHRISTCHURCH','GREYMOUTH',685.121645221953,1028.27005071905),
:
:
:;
Create another Temp Table and trying to insert values in it:
CREATE TABLE #Table1
(FromHubId uniqueidentifier,
ToHubId uniqueidentifier,
Rate float,
Rate40 float,
FromHub varchar(30),
ToHub varchar(30)
);
insert into #Table1
select h.HubId As FromHubId, h.HubId As ToHubId, t.Rate, t.Rate40, t.FromHub, t.ToHub
FROM #Table t JOIN Hub h ON
t.FromHub=h.Centre and t.ToHub=h.Centre;
select * from #Table1;
But, It only insert values where FromHub and ToHub are same. I want to insert all values ie for different From and To hub as well.
Any help will be appreciated. Thanks..!!
TRY THIS: you have to join separately fro both from
and to
hubId using table alias
as below:
INSERT INTO #Table1
SELECT DISTINCT h.HubId As FromHubId,
h1.HubId As ToHubId,
t.Rate,
t.Rate40,
t.FromHub,
t.ToHub
FROM #Table t
JOIN #Hub h ON t.FromHub=h.Centre
JOIN #Hub h1 ON t.ToHub=h1.Centre;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.