Reference a query object from another query

Tags:
SQL
I want to be able to reference a db2 query from another query. Is this possible? For example: select table_data.field1, query_data.field1, query_data.field2 from table_data, query_data where table_data.field1 = table_data.field 1 In the above example, query_data is a db2 query that I have created so I am the object owner. I seem to only be able to reference tables and not queries from other queries. Can this be done in db2?

Answer Wiki

Thanks. We'll let you know when a new response is added.

Correction:
The example should read:
select table_data.field1,
query_data.field1,
query_data.field2
from table_data,query_data
where table_data.field1 = query_data.field1

Discuss This Question: 2  Replies

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • Mdaponte
    You could create a view with the query, and then you could use it in any query: create view xxx as select ... And I guess that you could also use it as a inner query (at least in SQL Server 2000 you could), for example: select * from table1, (select t2.x, y, z from table2 t2, table3 t3 where t2.x = t3.x) as new_table where table1.x = new_table.x
    0 pointsBadges:
    report
  • SteveG44
    mdaponte reported the SQL Server 2000 inner query technique. This also works with DB2 UDB. Further, UDB provides the following construct, a common-table-expression, that makes for an even simpler to understand query... WITH query_data (field1, field2) AS (SELECT xxx, yyy FROM some_table WHERE ...) SELECT t1.field1, q1.field2 FROM table_data t1, query_data q1 WHERE t1.field1 = q1.field1 This example also uses correlated coefficients for ease of coding JOIN conditions. Note that it isn't necessary to retrieve q1.field1 since it will always have the same value as t1.field1. Note that the complete query_data result set is not available outside the full select statement so this technique is valid only if you only want the end result.
    5 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following