SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Cross table like cartesian
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

AutumnHodge
Starting Member

USA
1 Posts

Posted - 02/22/2014 :  12:08:40  Show Profile  Reply with Quote
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.

Edited by - AutumnHodge on 02/22/2014 12:14:15

SwePeso
Patron Saint of Lost Yaks

Sweden
30102 Posts

Posted - 02/22/2014 :  14:44:57  Show Profile  Visit SwePeso's Homepage  Reply with Quote
SELECT      *
FROM        dbo.Table1 AS t1
CROSS JOIN  dbo.Table2 AS t2



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

visakh16
Very Important crosS Applying yaK Herder

India
52309 Posts

Posted - 02/23/2014 :  03:30:23  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000