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


Script Needed to pull the data


Script Needed to pull the data

Author
Message
DBA12345
DBA12345
Valued Member
Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)

Group: General Forum Members
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
Lowell
Lowell
One Orange Chip
One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)

Group: General Forum Members
Points: 28111 Visits: 39939
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

--
help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

T_Dot_Geek
T_Dot_Geek
Valued Member
Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)

Group: General Forum Members
Points: 60 Visits: 99
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
thermanson
thermanson
Valued Member
Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)

Group: General Forum Members
Points: 59 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)


T_Dot_Geek
T_Dot_Geek
Valued Member
Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)

Group: General Forum Members
Points: 60 Visits: 99
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
DBA12345
DBA12345
Valued Member
Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)

Group: General Forum Members
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.
T_Dot_Geek
T_Dot_Geek
Valued Member
Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)

Group: General Forum Members
Points: 60 Visits: 99
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
ChrisM@Work
ChrisM@Work
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16186 Visits: 19543
-- 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
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