We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Query from a query

Whytboy
Whytboy asked
on
Medium Priority
171 Views
Last Modified: 2010-04-06
Is it possible to use the result from a query in another TQuery object.  For instance, Have a query that returns a data set.  Then have another query that returns the sum of one of the columns in the first dataset?  

I'm trying to avoid having the same query in multiple places!

Roger Webb
Comment
Watch Question

Commented:
Sure, why not?

Author

Commented:
And that would be accomplished .... how??


Commented:
Ooops, sorry misread your question, don't know if that's possible.
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Problem with that is that if you include a where clause so make query1 a subset of data.. you would have to include the where clause in query2 as well...

Commented:
that's true.

one solution would be to copy all text from query1 to query2 replacing just fields between 'select ' and ' from'.

actually it was meant to be used that way especially if query is joining more tables.



Commented:
so it would look like this

query2.sql.text:='select sum('+query1.fields[1].fieldname+') '+
  copy(query1.sql.text, pos('from',query1.sql.text),
    length(query1.sql.text)-pos('from',query1.sql.text));

check syntax, i wrote it know, didn't checked

Author

Commented:
Adjusted points to 150
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.