• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 165
  • 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
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

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