SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


UNION TWO TABLES WITH ONLY ONE COLUMN IN COMMON


UNION TWO TABLES WITH ONLY ONE COLUMN IN COMMON

Author
Message
santa326
santa326
SSC-Enthusiastic
SSC-Enthusiastic (163 reputation)SSC-Enthusiastic (163 reputation)SSC-Enthusiastic (163 reputation)SSC-Enthusiastic (163 reputation)SSC-Enthusiastic (163 reputation)SSC-Enthusiastic (163 reputation)SSC-Enthusiastic (163 reputation)SSC-Enthusiastic (163 reputation)

Group: General Forum Members
Points: 163 Visits: 137
Hi all,
I have two tables.
One has info about folders and another one about files.

Following are the table


DECLARE @Folders TABLE( Path VARCHAR(50), Foldername Varchar(50), FolderSize INT)

INSERT @Folders
SELECT 'C:\FOLDER1' Path, 'FOLDER1' Foldername, '3000' FolderSize UNION ALL
SELECT 'C:\FOLDER2', 'FOLDER2', '4004' UNION ALL
SELECT 'C:\FOLDER3', 'FOLDER3', '7010'


SELECT * FROM @Folders



DECLARE @Files TABLE( Path VARCHAR(50), Filename Varchar(50), FileSize INT)

INSERT @Files
SELECT 'C:\FOLDER1' Path, 'File1.xlsx' Filename, '1024' Filesize UNION ALL
SELECT 'C:\FOLDER1' , 'File2.xlsx' , '300' UNION ALL
SELECT 'C:\FOLDER1' , 'File3.xlsx' , '2000' UNION ALL
SELECT 'C:\FOLDER2' , 'File4.xlsx' , '3000' UNION ALL
SELECT 'C:\FOLDER2' , 'File5.xlsx' , '200' UNION ALL
SELECT 'C:\FOLDER2' , 'File6.xlsx' , '3145' UNION ALL
SELECT 'C:\FOLDER3' , 'File7.xlsx' , '46' UNION ALL
SELECT 'C:\FOLDER3' , 'File8.xlsx' , '30' UNION ALL
SELECT 'C:\FOLDER3' , 'File9.xlsx' , '2300'
select * from @Files




Above two table store folder and file paths .names and sizes.


i want to run a select statement which will give the following result.



DECLARE @Result TABLE( Path VARCHAR(50),Foldername Varchar(50), FolderSize varchar(50), Filename Varchar(50), FileSize varchar(50))

INSERT @Result
SELECT 'C:\FOLDER1' Path, 'FOLDER1' Foldername, '3000' FolderSize,'' Filename, '' Filesize UNION ALL
SELECT 'C:\FOLDER1' , '','','File2.xlsx' , '300' UNION ALL
SELECT 'C:\FOLDER1' , '','','File3.xlsx' , '2000' UNION ALL
SELECT 'C:\FOLDER1' , '','','File3.xlsx' , '2000' UNION ALL
SELECT 'C:\FOLDER2', 'FOLDER2', '4004','','' UNION ALL
SELECT 'C:\FOLDER2' , '','','File4.xlsx' , '3000' UNION ALL
SELECT 'C:\FOLDER2' , '','','File5.xlsx' , '200' UNION ALL
SELECT 'C:\FOLDER2' , '','','File6.xlsx' , '3145' UNION ALL
SELECT 'C:\FOLDER3', 'FOLDER3', '7010' ,'','' UNION ALL
SELECT 'C:\FOLDER3' , '','','File7.xlsx' , '46' UNION ALL
SELECT 'C:\FOLDER3' , '','','File8.xlsx' , '30' UNION ALL
SELECT 'C:\FOLDER3' , '','','File9.xlsx' , '2300'

select * from @result




I have tried UNION ALL But that dont seem to work as in real data i have different number of coloumns in each table.



Regards
Ravi T
anthony.green
anthony.green
One Orange Chip
One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)

Group: General Forum Members
Points: 25280 Visits: 6519
Based off your sample data the below will union both tables together.


select
fo.path,
fo.foldername,
CONVERT(VARCHAR,fo.foldersize) AS FolderSize,
'' AS FileName,
'' AS FileSize
FROM
@Folders fo
union
select
fi.path,
'',
'',
fi.Filename,
CONVERT(VARCHAR,fi.FileSize)
from
@Files fi
order by 1,4





Want an answer fast? Try here
How to post data/code for the best help - Jeff Moden
When a question, really isn't a question - Jeff Smith
Need a string splitter, try this - Jeff Moden
How to post performance problems - Gail Shaw
CrossTabs-Part1 & Part2 - Jeff Moden
SQL Server Backup, Integrity Check, and Index and Statistics Maintenance - Ola Hallengren
Managing Transaction Logs - Gail Shaw
Troubleshooting SQL Server: A Guide for the Accidental DBA - Jonathan Kehayias and Ted Krueger


ChrisM@Work
ChrisM@Work
SSC-Forever
SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)

Group: General Forum Members
Points: 42629 Visits: 20015
You need "placeholder" columns:

SELECT 
[Path],
Foldername,
FolderSize,
[Filename],
FileSize
FROM (
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
) d
ORDER BY [Path], Foldername DESC



“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
mickyT
mickyT
SSCrazy
SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)

Group: General Forum Members
Points: 2896 Visits: 3318
You could try the following query.

select f.Path, f.FolderName, f.FolderSize, d.FileName, d.FileSize
from @Folders f
left outer join @Files d on f.Path = d.Path
group by grouping sets ((f.Path, Foldername, FolderSize), (f.path, d.FileName, d.FileSize))
order by f.Path, d.filename



This produces the same result except there are NULLs rather than empty strings
ChrisM@Work
ChrisM@Work
SSC-Forever
SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)

Group: General Forum Members
Points: 42629 Visits: 20015
mickyT (9/3/2012)
You could try the following query.

select f.Path, f.FolderName, f.FolderSize, d.FileName, d.FileSize
from @Folders f
left outer join @Files d on f.Path = d.Path
group by grouping sets ((f.Path, Foldername, FolderSize), (f.path, d.FileName, d.FileSize))
order by f.Path, d.filename



This produces the same result except there are NULLs rather than empty strings


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

“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Mitesh Oswal
Mitesh Oswal
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1186 Visits: 653
Hey Santa,

Can you please validate the below code


SELECT Path,FolderName,FolderSize,FileName,FileSize
FROM
(
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

)E
ORDER BY PATH,ID



Regards,
Mitesh OSwal
+918698619998
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search