Skip to content
Advertisement

Basic SQL joining two select statements

I have two select statements that work just fine separately, but I don’t know how to put them together into one resulting table. I’ve been trying SELECT INTO or creating temporary tables, but I am just not getting anywhere. Hoping that if I post the two select statements below, it’ll take someone 30 seconds to point me in the right direction.

Advertisement

Answer

two major pieces of information is missing.

  1. what is the primary key of the table?
  2. What kind of database is this?

It’s a bit confusing on what the tables look like, but I’m going to assume that EMEQP# is a primary key in the EQPMASFL table.

if that’s the case, you can perform a join using a couple of CTEs. if that’s not the case, you have to add a field to each query that can be joined when the queries output.

you can do this:

The reason why I”m doing this in this manner is due to the different filtering criteria in each query. with the cte’s (see this article), you can use the output as data sources for follow on queries.

This comes at a cost, if you do a large query that references the cte, the cte requires each time. so use them sparingly.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement