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.
Author |
Topic |
sqlghost
Starting Member
23 Posts |
Posted - 2007-02-06 : 10:32:31
|
Hi all,I need to pull server names from 4 different data sources; A, B, C, and D. My objective is to determine which servers are missing from each data source. So for example, let's say I have the following data files:A.txt:Server1Server2Server3Server4B.csv:Server2Server3C.csv:Server3Server4D.xls:Server1Server2Server3I want to import this data into SQL Server and run a query that would show me that;B is missing Server1 and Server4C is missing Server1 and Server2D is missing Server4Would I import each data file into it's own 1 column database, or would I import each data file into 4 1 column tables within the same database? Also, what T-SQL query would I use to get the desired result?Thanks! |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-02-06 : 10:41:13
|
4 single column tablesdeclare @tableA table( server varchar(10))declare @tableB table( server varchar(10))declare @tableC table( server varchar(10))declare @tableD table( server varchar(10))insert into @tableAselect 'Server1' union allselect 'Server2' union allselect 'Server3' union allselect 'Server4'insert into @tableBselect 'Server2' union allselect 'Server3' insert into @tableCselect 'Server3' union allselect 'Server4'insert into @tableDselect 'Server1' union allselect 'Server2' union allselect 'Server3' select s.serverfrom @tableB b right join ( select server from @tableA union select server from @tableC union select server from @tableD ) s on b.server = s.serverwhere b.server is nullselect s.serverfrom @tableC c right join ( select server from @tableA union select server from @tableB union select server from @tableD ) s on c.server = s.serverwhere c.server is null KH |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-02-07 : 02:10:47
|
[code]-- Prepare sample datadeclare @tableA table (server varchar(10))insert @tableAselect 'Server1' union allselect 'Server2' union allselect 'Server3' union allselect 'Server4'declare @tableB table (server varchar(10))insert @tableBselect 'Server2' union allselect 'Server3' declare @tableC table (server varchar(10))insert @tableCselect 'Server3' union allselect 'Server4'declare @tableD table (server varchar(10))insert @tableDselect 'Server1' union allselect 'Server2' union allselect 'Server3' -- Stage the dataDECLARE @Stage TABLE (Source VARCHAR(1), Server VARCHAR(10))INSERT @Stage ( Source, Server )SELECT a.Source, b.ServerFROM ( SELECT 'A' AS Source UNION SELECT 'B' UNION SELECT 'C' UNION SELECT 'D' ) AS aCROSS JOIN ( SELECT Server FROM @tableA UNION SELECT Server FROM @tableB UNION SELECT Server FROM @tableC UNION SELECT Server FROM @tableD ) AS bLEFT JOIN ( SELECT 'A' AS Source, Server FROM @TableA UNION SELECT 'B', Server FROM @TableB UNION SELECT 'C', Server FROM @TableC UNION SELECT 'D', Server FROM @TableD ) AS c ON c.Source = a.Source AND c.Server = b.ServerWHERE c.Source IS NULL-- Create the outputDECLARE @Output TABLE (Message VARCHAR(8000))DECLARE @CurrSource VARCHAR(1), @MaxSource VARCHAR(1), @msg VARCHAR(8000)SELECT @CurrSource = MIN(Source), @MaxSource = MAX(Source)FROM @StageWHILE @CurrSource <= @MaxSource BEGIN SELECT @msg = NULL SELECT @msg = ISNULL(@msg + ' and ', '') + d.Server FROM ( SELECT DISTINCT TOP 100 PERCENT Server FROM @Stage WHERE Source = @CurrSource ORDER BY Server ) AS d INSERT @Output ( Message ) SELECT @CurrSource + ' is missing ' + @msg SELECT @CurrSource = MIN(Source) FROM @Stage WHERE Source > @CurrSource ENDSELECT MessageFROM @OutputORDER BY Message[/code]Peter LarssonHelsingborg, Sweden |
 |
|
sqlghost
Starting Member
23 Posts |
Posted - 2007-02-07 : 11:28:19
|
Thanks a ton!!Here's what I did to get the desired result:I created a DB called Servers and imported the data into 4 one column tables (TableA, TableB, TableC, and TableD). Next, I ran the following query:select s.ServerNamefrom TableB b right join(select ServerName from TableA unionselect ServerName from TableC unionselect ServerName from TableD) son b.ServerName = s.ServerNamewhere b.ServerName is nullThe example above, returns all of the servers not in TableB, but found in at least one of Tables other than TableB. I did the corresponding SQL query for each other Table. This achieved my desired results. However, I realized that I'd like as a secondary objective, the ability to show where the servers DO exist. So in the example above, my results would be:ServerNameServer1Server4What I'd like to show is this:ServerName A B C DServer1 x Null Null xServer4 x Null x NullI thought I would add 4 columns to each of my 4 tables; A, B, C, D and populate the appropriate column with an 'x' for each record to designate it's origin. Next, I thought the following would give me what I needed:select s.ServerName, s.A, s.B, s.C, s.Dfrom TableB b right join(select ServerName, A, B, C, D from TableA unionselect ServerName, A, B, C, D from TableC unionselect ServerName, A, B, C, D from TableD) son b.ServerName = s.ServerNamewhere b.ServerName is nullThis kinda gave me what I wanted, except for the fact that it listed duplicate servername records:ServerName A B C DServer1 x Null Null NullServer1 Null Null Null xServer4 x Null Null NullServer4 Null Null x NullIe, there cannot be two "x"'s in the same row. Is there an easy way to do this?Thanks so much for your help! |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-02-07 : 13:11:06
|
The only thing different from previous suggestion is the change from LEFT JOIN to INNER JOIN and the removal of WHERE c.Source IS NULL-- Prepare sample datadeclare @tableA table (server varchar(10))insert @tableAselect 'Server1' union allselect 'Server2' union allselect 'Server3' union allselect 'Server4'declare @tableB table (server varchar(10))insert @tableBselect 'Server2' union allselect 'Server3' declare @tableC table (server varchar(10))insert @tableCselect 'Server3' union allselect 'Server4'declare @tableD table (server varchar(10))insert @tableDselect 'Server1' union allselect 'Server2' union allselect 'Server3' -- Stage the dataDECLARE @Stage TABLE (Source VARCHAR(1), Server VARCHAR(10))INSERT @Stage ( Source, Server )SELECT a.Source, b.ServerFROM ( SELECT 'A' AS Source UNION SELECT 'B' UNION SELECT 'C' UNION SELECT 'D' ) AS aCROSS JOIN ( SELECT Server FROM @tableA UNION SELECT Server FROM @tableB UNION SELECT Server FROM @tableC UNION SELECT Server FROM @tableD ) AS bINNER JOIN ( SELECT 'A' AS Source, Server FROM @TableA UNION SELECT 'B', Server FROM @TableB UNION SELECT 'C', Server FROM @TableC UNION SELECT 'D', Server FROM @TableD ) AS c ON c.Source = a.Source AND c.Server = b.Server-- Create the outputDECLARE @Output TABLE (Message VARCHAR(8000))DECLARE @CurrSource VARCHAR(1), @MaxSource VARCHAR(1), @msg VARCHAR(8000)SELECT @CurrSource = MIN(Source), @MaxSource = MAX(Source)FROM @StageWHILE @CurrSource <= @MaxSource BEGIN SELECT @msg = NULL SELECT @msg = ISNULL(@msg + ' and ', '') + d.Server FROM ( SELECT DISTINCT TOP 100 PERCENT Server FROM @Stage WHERE Source = @CurrSource ORDER BY Server ) AS d INSERT @Output ( Message ) SELECT @CurrSource + ' are having ' + @msg SELECT @CurrSource = MIN(Source) FROM @Stage WHERE Source > @CurrSource ENDSELECT MessageFROM @OutputORDER BY Message Peter LarssonHelsingborg, Sweden |
 |
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2007-02-07 : 13:39:04
|
[code]declare @tableA table (server varchar(10))declare @tableB table (server varchar(10))declare @tableC table (server varchar(10))declare @tableD table (server varchar(10))insert @tableAselect 'Server1' union allselect 'Server2' union allselect 'Server3' union allselect 'Server4'insert @tableBselect 'Server2' union allselect 'Server3' insert @tableCselect 'Server3' union allselect 'Server4'insert @tableDselect 'Server1' union allselect 'Server2' union allselect 'Server3' select server, InTableA = max(s1), InTableB = max(s2), InTableC = max(s3), InTableD = max(s4)from ( select server, s1 = 1, s2 = 0, s3 = 0 , s4=0 from @tableA union all select server, s1 = 0, s2 = 1, s3 = 0 , s4=0 from @tableB union all select server, s1 = 0, s2 = 0, s3 = 1 , s4=0 from @tableC union all select server, s1 = 0, s2 = 0, s3 = 0 , s4=1 from @tableD ) agroup by Serverorder by Server[/code]Results:[code]server InTableA InTableB InTableC InTableD ---------- ----------- ----------- ----------- ----------- Server1 1 0 0 1Server2 1 1 0 1Server3 1 1 1 1Server4 1 0 1 0[/code]CODO ERGO SUM |
 |
|
|
|
|
|
|