Connecting multiple Query to one Grid

Posted on 2006-10-19
Last Modified: 2010-04-05

Backend - Progress Database
Front End - Delphi

I have lots of Restriction using SQL commands with Progress Database, since it doesn't support all of them.  For example the below Query doesn't work.

SELECT t1.tr_effdate, COUNT(t1.tr_effdate) AS DayCount, (SELECT COUNT(*) FROM tr_hist t2
    WHERE t2.tr_effdate BETWEEN '1 Jun 2006' AND t1.tr_effdate) AS MonthToDate
FROM tr_hist t1
GROUP BY t1.tr_effdate
ORDER BY t1.tr_effdate

My Questions now,

I want to used multiple Queries and display the data on one TDBGrid. I will have a key field in each query .

Is this possible , if so can someone explan with a small example.

Let me know if you need any other details.

Question by:saroren
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 17

Expert Comment

ID: 17770442
Do you wish to have write access to the dbgrid's data for these multiple queries, or will it be read only?

Expert Comment

ID: 17771686
I don't think it is possible to have multiple queries linked to 1 dbgrid at the same time.

The only way I see is to make use of a UNION-Clause in your Query to combine the results of several queries.
Only condition is that all queries have same output-fields I guess.

Example :
SELECT 'Customer' AS PersonType, Firstname, LastName, Address
FROM Customers
SELECT 'Supplier' AS PersonType, FirstName, LastName, Address
FROM Suppliers
ORDER BY PersonType, LastName, FirstName

By use of such a query I can see all suppliers AND all Customers in 1 grid. But in fact I still use 1 SQL-Statement to retrieve these data.
But that will be the closest you can get I guess.

Best regards,

The Mayor.

Expert Comment

ID: 17777527
how about feeding the data to a ClientDataset?

Drop a TClientDataSet
Add 3 fields to it one to hold the date, and the other for DayCount and MonthToDate
once the fields are created, call the CreateDataset method of the TClientDataset. or at design time Right-Click select Create Dataset
iterate through each of the queries and assign the values of the ClientDataSet fields to the query values

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 17777601
Hi TheRealLoki   - I want the grid to be read only.

Hi wimmeyvaert  - My query is too complex and too many record .

Hi cobi100  - i will try this, as i am new to Delphi, if iam not able to figure this i will let you know.

Thanks for the help!!!!

Expert Comment

ID: 17777643
well on my post, probably the most complicated part would be to iterate through the records, try it like this:;
while not(query1.eof) do
  ClientDataset1.FieldbyName('DayCount').AsInteger := query1.FieldByName('DayCount').AsInteger;;

Author Comment

ID: 17822347
Hi Cobi100,

Sorry for the dealy.

I tried your Query but it works only for one Query. But i want to display data's from two queries in a single grid.

If i right click on the Tclient dataset and choose create Dataset, then i choose Assign Local Data, at thsi point i am able to choose only one query .  

is there a way i can choose multiple query and display the data on a singel Grid.


Accepted Solution

cobi100 earned 500 total points
ID: 17845266
yes, there is, don't assign local data instead use the code I posted in my previous post, maybe on form activate or something like that, you have to use that code for each of the queries you want to add to the client dataset, like this:;
while not(query1.eof) do
  ClientDataset1.FieldbyName('DayCount').AsInteger := query1.FieldByName('DayCount').AsInteger;

this is for the first query, and for the second one you want to do this:;
while not(query2.eof) do
  ClientDataset1.FieldbyName('MonthToDate').AsInteger := query1.FieldByName('MonthToDate').AsInteger;

hmmmm, now that I'm seeing it, this will only work if both of the queries return the same number of records and in the same order, if that's not the case then you need to locate the right record to do the update, I think you can use ClientDataset1.Locate

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question