Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Script Needed to pull the data Expand / Collapse
Author
Message
Posted Monday, October 29, 2012 1:33 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Thursday, April 24, 2014 8:19 PM
Points: 56, Visits: 240

I need to pull the data from two tables called Table1 & Table2.

Table1 has following columns:
ServerName ApplicationName ApplicationID
Table2 has following columns:
ServerName ApplicationName ApplicationID

From these I need Servername, ApplicationID and one extra coulmn called 'Appserver'. Which is not present in both the tables.

This column should be appeared as

Table1 if 'Servername', 'ApplicationID' pulled only from Tabe1
or
Table2 if 'Servername', 'ApplicationID' pulled only from Table2
or
Combine if 'Servername', 'ApplicationID' pulled from only both tables (table1& Table2)

Thanks
Post #1378440
Posted Monday, October 29, 2012 1:49 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 6:44 AM
Points: 12,905, Visits: 32,158
sounds like a case statement or a union all, but i'm having trouble figuring out where your data is
soemthing like this?
SELECT Servername, ApplicationID, 'Table1'  AS Appserver FROM Table1 UNION ALL
SELECT Servername, ApplicationID, 'Table2' AS Appserver FROM Table2 UNION ALL
SELECT Servername, ApplicationID, 'Combine' AS Appserver FROM Combine

--or is it like this?

SELECT Servername, ApplicationID, CASE
WHEN Table1.SomeValue IS NULL
Then 'Table2'
WHEN Table2.SomeValue IS NULL
Then 'Table1'
ELSE 'Combine'
END AS Appserver
FROM CentralTable




Lowell

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1378444
Posted Monday, October 29, 2012 1:51 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Tuesday, May 6, 2014 3:55 PM
Points: 54, Visits: 83
Select T1.ServerName, T1.ApplicationName, T1.ApplicationID, AppServer = 'Combined'
from Table1 T1
inner join Table2 T2 ON T1.ServerName = T2.ServerName
and T1.ApplicationID = T2.ApplicationID

UNION All

Select T1.ServerName, T1.ApplicationName, T1.ApplicationID, AppServer = 'Table1'
from Table1 T1
Left join Table2 T2 ON T1.ServerName = T2.ServerName
and T1.ApplicationID = T2.ApplicationID

UNION ALL

Select T1.ServerName, T1.ApplicationName, T1.ApplicationID, AppServer = 'Table2'
from Table1 T1
Right join Table2 T2 ON T1.ServerName = T2.ServerName
and T1.ApplicationID = T2.ApplicationID
Post #1378445
Posted Monday, October 29, 2012 1:56 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, January 7, 2014 1:16 PM
Points: 43, Visits: 287
Maybe:

Select IsNull(t1.ServerName,t2.ServerName) as ServerName,
IsNull(t1.ApplicationName,t2.ApplicationName) as ApplicationName,
Case
When t1.ApplicationName is null then 'Table2'
When t2.ApplicationName is null then 'Table1'
Else 'Combine'
End AppServer
From Table1 t1
Full Outer Join Table2 t2 on
(t1.ServerName = t2.ServerName) and (t1.ApplicationName = t2.ApplicationName)

Post #1378448
Posted Monday, October 29, 2012 2:09 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Tuesday, May 6, 2014 3:55 PM
Points: 54, Visits: 83
SSCRookie,
Your logic is perfect. It is a good strategy to save some coding by using Full Outer Join and CASE istead of lousy Left/Right/Inner join and Union. Thanks for sharing good work.
Regards,
T_Dot_Geek
Post #1378458
Posted Monday, October 29, 2012 5:58 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Thursday, April 24, 2014 8:19 PM
Points: 56, Visits: 240
Thank you ver much...I am looking to modify the query as below


select distinct t1.ServerName, t1.[Application ID], t1.[Application Name], t2.VP1, T2.VP2, T2.SVP, T2.[DC2015 Move Group], T1.[System Type],
T3.Line, AppServerSource = (Select IsNull(t1.ServerName,t2.ServerName) as ServerName,
IsNull(t1.[Application Name],t2.[Application Name]) as [Application Name],
Case
When t1.[Application Name] is null then 'Check'
When t2.[Application Name] is null then 'Payorder'
Else 'Both'
End AppServer)
from Table1 t1 INNER JOIN Table2 t2
on t1.[Application ID]= t2.[Application ID] and t1.Servername = t2.ServerName JOIN BIM T3
ON t1.[Application Name]= t3.[Application Name]
order by Servername

After executing the above query I am getting the follwoing error.

Msg 116, Level 16, State 1, Line 1
Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.
Post #1378526
Posted Monday, October 29, 2012 8:12 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Tuesday, May 6, 2014 3:55 PM
Points: 54, Visits: 83
Please Look at below statement where There is anInvalid use of bracket and Select statement.
Your code is little bit confusing.

AppServerSource = (Select IsNull(t1.ServerName,t2.ServerName) as ServerName

I tried to fix as below. Please try this one :

select distinct
t1.ServerName,
t1.[Application ID],
t1.[Application Name],
t2.VP1,
T2.VP2,
T2.SVP,
T2.[DC2015 Move Group],
T1.[System Type],
T3.Line,
IsNull(t1.ServerName,t2.ServerName) as ServerName,
IsNull(t1.[Application Name],t2.[Application Name]) as [Application Name],
Case
When t1.[Application Name] is null then 'Check'
When t2.[Application Name] is null then 'Payorder'
Else 'Both'
End AppServer
from Table1 t1 INNER JOIN Table2 t2
on t1.[Application ID]= t2.[Application ID] and t1.Servername = t2.ServerName JOIN BIM T3
ON t1.[Application Name]= t3.[Application Name]
order by Servername
Post #1378543
Posted Tuesday, October 30, 2012 3:54 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 7:59 AM
Points: 6,780, Visits: 13,980
-- formatted original
select distinct
t1.ServerName,
t1.[Application ID],
t1.[Application Name],
t2.VP1,
T2.VP2,
T2.SVP,
T2.[DC2015 Move Group],
T1.[System Type],
T3.Line,

-- defines 1 column
AppServerSource = (
Select -- returns 3 columns
IsNull(t1.ServerName,t2.ServerName) as ServerName,
IsNull(t1.[Application Name],t2.[Application Name]) as [Application Name],
Case
When t1.[Application Name] is null then 'Check'
When t2.[Application Name] is null then 'Payorder'
Else 'Both'
End AppServer)
from Table1 t1
INNER JOIN Table2 t2
on t1.[Application ID]= t2.[Application ID]
and t1.Servername = t2.ServerName
JOIN BIM T3
ON t1.[Application Name]= t3.[Application Name]
order by Servername -- this is ambiguous and will error - use a table alias

-- alternative
select distinct
t1.ServerName,
t1.[Application ID],
t1.[Application Name],
t2.VP1,
T2.VP2,
T2.SVP,
T2.[DC2015 Move Group],
T1.[System Type],
T3.Line,
ServerName = IsNull(t1.ServerName,t2.ServerName),
[Application Name] = IsNull(t1.[Application Name],t2.[Application Name]),
AppServer = Case
When t1.[Application Name] is null then 'Check'
When t2.[Application Name] is null then 'Payorder'
Else 'Both'
End
from Table1 t1
INNER JOIN Table2 t2
on t1.[Application ID]= t2.[Application ID]
and t1.Servername = t2.ServerName
INNER JOIN BIM T3
ON t1.[Application Name]= t3.[Application Name]
order by t1.Servername






“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
Post #1378646
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse