Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Cross table like cartesian

Author  Topic 

AutumnHodge
Starting Member

1 Post

Posted - 2014-02-22 : 12:08:40
Hi,

I am trying to cross join two tables which doesn't have any relation. Can anyone help me solve this. Here is my details.


http://www.imageurlhost.com/images/67spn200z5j1wpmzd9hz.png

Please let me know If I need to provide any details.

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2014-02-22 : 14:44:57
[code]SELECT *
FROM dbo.Table1 AS t1
CROSS JOIN dbo.Table2 AS t2[/code]


Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2014-02-23 : 03:30:23
see the exact illustration here

declare @t1 table
(
Transport varchar(100),
Type varchar(10),
Weight int,
Service varchar(100)
)

INSERT @t1
VALUES ('Road','Car',20,'OwnUse'),
('Road','Bus',30,'Management'),
('Air','USAirways',50,'USA'),
('Air','Lufthansa',25,'Germany')


declare @t2 table
(
Size varchar(10),
[Order] int,
Ratio decimal(5,1)
)
INSERT @t2
VALUES ('Large',1,1.5),
('Small',2,0.9)

SELECT Transport,[Order],Size,Service,Car,Bus,USAirways,Lufthansa,Ratio
FROM
(
SELECT t1.Transport,t2.[Order],t2.Size,t1.Service,t11.Type,t11.Weight,t2.Ratio
FROM (SELECT Transport,Service FROM @t1) t1
CROSS JOIN (SELECT Type,Weight FROM @t1) t11
CROSS JOIN @t2 t2
)t
PIVOT(SUM(Weight) FOR Type IN (Car,Bus,USAirways,Lufthansa))p


output
------------------------------------------------------------------------------------------------
Transport Order Size Service Car Bus USAirways Lufthansa Ratio
------------------------------------------------------------------------------------------------
Air 1 Large Germany 20 30 50 25 1.5
Air 1 Large USA 20 30 50 25 1.5
Air 2 Small Germany 20 30 50 25 0.9
Air 2 Small USA 20 30 50 25 0.9
Road 1 Large Management 20 30 50 25 1.5
Road 1 Large OwnUse 20 30 50 25 1.5
Road 2 Small Management 20 30 50 25 0.9
Road 2 Small OwnUse 20 30 50 25 0.9



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -