﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 2008 / T-SQL (SS2K8)  / UNION TWO TABLES WITH ONLY ONE COLUMN IN COMMON / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Fri, 24 May 2013 21:55:29 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: UNION TWO TABLES WITH ONLY ONE COLUMN IN COMMON</title><link>http://www.sqlservercentral.com/Forums/Topic1353466-392-1.aspx</link><description>Hey Santa,Can you please validate the below code[code="sql"]SELECT Path,FolderName,FolderSize,FileName,FileSizeFROM(	SELECT PATH,FolderName,FolderSize,'' AS FileName,'' AS FileSize, 1 AS ID FROM @Folders	UNION	select PATH,'' AS FolderName,'' as FolderSize,FileName, FileSize,2 AS ID from @Files	)EORDER BY PATH,ID[/code]</description><pubDate>Tue, 04 Sep 2012 09:18:20 GMT</pubDate><dc:creator>Mitesh Oswal</dc:creator></item><item><title>RE: UNION TWO TABLES WITH ONLY ONE COLUMN IN COMMON</title><link>http://www.sqlservercentral.com/Forums/Topic1353466-392-1.aspx</link><description>[quote][b]mickyT (9/3/2012)[/b][hr]You could try the following query.[code="sql"]select f.Path, f.FolderName, f.FolderSize, d.FileName, d.FileSizefrom @Folders f 	left outer join @Files d on f.Path = d.Pathgroup by grouping sets ((f.Path, Foldername, FolderSize), (f.path, d.FileName, d.FileSize))order by f.Path, d.filename[/code]This produces the same result except there are NULLs rather than empty strings[/quote]It does, but at a cost - each table is scanned twice and there are two sorts. The UNION version scans each table once followed by a single sort. Kudos for lateral thinking though :cool:</description><pubDate>Tue, 04 Sep 2012 01:49:22 GMT</pubDate><dc:creator>ChrisM@Work</dc:creator></item><item><title>RE: UNION TWO TABLES WITH ONLY ONE COLUMN IN COMMON</title><link>http://www.sqlservercentral.com/Forums/Topic1353466-392-1.aspx</link><description>You could try the following query.[code="sql"]select f.Path, f.FolderName, f.FolderSize, d.FileName, d.FileSizefrom @Folders f 	left outer join @Files d on f.Path = d.Pathgroup by grouping sets ((f.Path, Foldername, FolderSize), (f.path, d.FileName, d.FileSize))order by f.Path, d.filename[/code]This produces the same result except there are NULLs rather than empty strings</description><pubDate>Mon, 03 Sep 2012 14:50:49 GMT</pubDate><dc:creator>mickyT</dc:creator></item><item><title>RE: UNION TWO TABLES WITH ONLY ONE COLUMN IN COMMON</title><link>http://www.sqlservercentral.com/Forums/Topic1353466-392-1.aspx</link><description>You need "placeholder" columns:[code="sql"]SELECT 	[Path], 	Foldername, 	FolderSize,	[Filename], 	FileSizeFROM (	SELECT 		[Path], 		[Filename] = CAST(NULL AS Varchar(50)), 		FileSize = CAST(NULL AS INT),		Foldername, 		FolderSize 	FROM @Folders	UNION ALL	SELECT 		[Path], 		[Filename], 		FileSize, 		Foldername = CAST(NULL AS Varchar(50)), 		FolderSize = CAST(NULL AS INT)	FROM @Files) dORDER BY [Path], Foldername DESC[/code]</description><pubDate>Mon, 03 Sep 2012 06:00:48 GMT</pubDate><dc:creator>ChrisM@Work</dc:creator></item><item><title>RE: UNION TWO TABLES WITH ONLY ONE COLUMN IN COMMON</title><link>http://www.sqlservercentral.com/Forums/Topic1353466-392-1.aspx</link><description>Based off your sample data the below will union both tables together.[code="sql"]select 	fo.path,	fo.foldername,	CONVERT(VARCHAR,fo.foldersize) AS FolderSize,	'' AS FileName,	'' AS FileSizeFROM	@Folders founionselect	fi.path,	'',	'',	fi.Filename,	CONVERT(VARCHAR,fi.FileSize)from	@Files fiorder by 1,4[/code]</description><pubDate>Mon, 03 Sep 2012 05:57:14 GMT</pubDate><dc:creator>anthony.green</dc:creator></item><item><title>UNION TWO TABLES WITH ONLY ONE COLUMN IN COMMON</title><link>http://www.sqlservercentral.com/Forums/Topic1353466-392-1.aspx</link><description>Hi all,     I have two tables. One has info about folders and another one about files.Following are the table[code="sql"]DECLARE	@Folders TABLE( Path VARCHAR(50), Foldername Varchar(50), FolderSize INT)INSERT	@FoldersSELECT	'C:\FOLDER1' Path, 'FOLDER1' Foldername, '3000' FolderSize  UNION ALLSELECT	'C:\FOLDER2', 'FOLDER2', '4004' UNION ALLSELECT	'C:\FOLDER3', 'FOLDER3', '7010' SELECT * FROM @FoldersDECLARE	@Files TABLE( Path VARCHAR(50), Filename Varchar(50), FileSize INT)INSERT	@FilesSELECT	'C:\FOLDER1' Path, 'File1.xlsx' Filename, '1024' Filesize  UNION ALLSELECT	'C:\FOLDER1' , 'File2.xlsx' , '300'  UNION ALLSELECT	'C:\FOLDER1' , 'File3.xlsx' , '2000'  UNION ALLSELECT	'C:\FOLDER2' , 'File4.xlsx' , '3000'  UNION ALLSELECT	'C:\FOLDER2' , 'File5.xlsx' , '200'  UNION ALLSELECT	'C:\FOLDER2' , 'File6.xlsx' , '3145'  UNION ALLSELECT	'C:\FOLDER3' , 'File7.xlsx' , '46'  UNION ALLSELECT	'C:\FOLDER3' , 'File8.xlsx' , '30'  UNION ALLSELECT	'C:\FOLDER3' , 'File9.xlsx' , '2300'select * from @Files[/code]Above two table store folder and file paths .names and sizes.i want to run a select statement which will give the following result.[code="sql"]DECLARE	@Result TABLE( Path VARCHAR(50),Foldername Varchar(50), FolderSize varchar(50), Filename Varchar(50), FileSize varchar(50))INSERT	@ResultSELECT	'C:\FOLDER1' Path, 'FOLDER1' Foldername, '3000' FolderSize,'' Filename, '' Filesize  UNION ALLSELECT	'C:\FOLDER1' , '','','File2.xlsx' , '300'  UNION ALLSELECT	'C:\FOLDER1' , '','','File3.xlsx' , '2000'  UNION ALLSELECT	'C:\FOLDER1' , '','','File3.xlsx' , '2000'  UNION ALLSELECT	'C:\FOLDER2', 'FOLDER2', '4004','','' UNION ALLSELECT	'C:\FOLDER2' , '','','File4.xlsx' , '3000'  UNION ALLSELECT	'C:\FOLDER2' , '','','File5.xlsx' , '200'  UNION ALLSELECT	'C:\FOLDER2' , '','','File6.xlsx' , '3145'  UNION ALLSELECT	'C:\FOLDER3', 'FOLDER3', '7010' ,'','' UNION ALLSELECT	'C:\FOLDER3' , '','','File7.xlsx' , '46'  UNION ALLSELECT	'C:\FOLDER3' , '','','File8.xlsx' , '30'  UNION ALLSELECT	'C:\FOLDER3' , '','','File9.xlsx' , '2300'select * from @result[/code]I have tried UNION ALL But that dont seem to work as in real data i have different number of coloumns in each table.RegardsRavi T</description><pubDate>Mon, 03 Sep 2012 05:44:00 GMT</pubDate><dc:creator>santa326</dc:creator></item></channel></rss>