|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Wednesday, May 08, 2013 3:42 PM
Points: 37,
Visits: 142
|
|
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
|
|
|
|
|
SSChampion
        
Group: General Forum Members
Last Login: Today @ 10:50 AM
Points: 11,605,
Visits: 27,647
|
|
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
|
|
|
|
|
Valued Member
      
Group: General Forum Members
Last Login: Thursday, November 08, 2012 9:15 AM
Points: 54,
Visits: 82
|
|
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
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Tuesday, May 14, 2013 7:48 AM
Points: 40,
Visits: 263
|
|
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)
|
|
|
|
|
Valued Member
      
Group: General Forum Members
Last Login: Thursday, November 08, 2012 9:15 AM
Points: 54,
Visits: 82
|
|
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
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Wednesday, May 08, 2013 3:42 PM
Points: 37,
Visits: 142
|
|
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.
|
|
|
|
|
Valued Member
      
Group: General Forum Members
Last Login: Thursday, November 08, 2012 9:15 AM
Points: 54,
Visits: 82
|
|
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
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: 2 days ago @ 8:21 AM
Points: 5,602,
Visits: 10,950
|
|
-- 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
|
|
|
|