Script Needed to pull the data

  • 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

  • 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!

  • 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

  • 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)

  • 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

  • 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.

  • 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

  • -- 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

Viewing 8 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic. Login to reply