0

I have the following two queries that produce the results I need. Now the final output I truly need I would usually use python for after the results are returned, but unfortunately only SQL can be used.

Query A:

SELECT * 
FROM openquery(PROD, 'SELECT `status`, computer_name, device_type 
                      FROM assets 
                      WHERE (device_type="SERVER") 
                        AND (status="ACTIVE")')

Query B:

SELECT * 
FROM openquery(AppMap, 'SELECT `t1`.`uaid` AS `uaid`, `t3`.`computer_name`,
                        FROM ((`applications` `t1` 
                        JOIN `app_infrastructure` `t2` ON (((`t1`.`uaid` = `t2`.`uaid`)))) 
                        JOIN `infrastructure` `t3` ON ((`t2`.`infrastructure_id` = `t3`.`infrastructure_id`)));')

How I would want to process the results:

if a computer_name is in both A and B:
   final_row = ['computer_name', 1]
elseif a computer_name is in A but not B:
   final_row = ['computer_name', 0]
elseif a computer_name is in B but not A:
   final_row = ['computer_name', 2]

So my final query results need to look like those rows, does that make sense?

3
  • Seems like a job for a FULL JOIN between both queries Commented Mar 5, 2018 at 18:17
  • genius! will give it a try. Commented Mar 5, 2018 at 18:21
  • That is not tsql Commented Mar 5, 2018 at 19:52

1 Answer 1

1

In a stored procedure, use both queries to load table variables.

Then do a FULL OUTER JOIN query, joining the two table variables on computer_name, and use a CASE expression to get your final_row value for each computer name.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.