Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 162
  • Last Modified:

Query from a query

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
0
Whytboy
Asked:
Whytboy
  • 3
  • 3
  • 2
1 Solution
 
ZifNabCommented:
Sure, why not?
0
 
WhytboyAuthor Commented:
And that would be accomplished .... how??


0
 
ZifNabCommented:
Ooops, sorry misread your question, don't know if that's possible.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
yunicCommented:
 Query1.SQL.Text:='select attr_op, attr_dolzina from meta_sif_attr';
  Query1.Open;
  Query2.SQL.Text:='select sum('+query1.fields[1].fieldname+') from meta_sif_attr';
  query2.Open;
  writeln(Query2.fields[0].AsString);


0
 
WhytboyAuthor 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...
0
 
yunicCommented:
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.



0
 
yunicCommented:
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
0
 
WhytboyAuthor Commented:
Adjusted points to 150
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now