On Thu, 2 Sep 1999 21:02:43 +0200, "Anton Ekermans" <ek...@inetcom.co.za>
wrote:
Quote
>To elaborate on the query...
>Say I want to have a calculated value based on an aggregate in the detail
>and a value in the master:
>(If this in right...)
> SELECT Code, SUM(Amount) SubTotal,(Sum(Amount)*Master.Factor) Total
> FROM Detail,Master
> GROUP BY Code
>How will the SQL parser know if I mean Detail.Code or Master.Code in
>select/group by...?
>Can I use values from the master table like this?
In this case, it is not going to matter whether it uses the Code field from
the Master or the Detail table. If the Detail table has ten records with a
given Code value, the Amount field would still be totalled to produce one
aggregate record for that Code value. But if you really need to specify
which table a field comes from, use a table correlation name (explained in
the local SQL help). In the statement below, "M" and "D" are table
correlation names.
SELECT M.Code, SUM(D.Amount) SubTotal,
(SUM(D.Amount) * M.Factor) Total
FROM Detail D, Master M
WHERE (D.Code = M.Code)
GROUP BY M.Code
Quote
>But if I scroll in the master table, the detail aggregate are not
>updated. I'm using TQuery for both grids(master/detail).
Maybe you need a TQuery Master-Detail link. The Detail TQuery component's
SQL would look like that below:
SELECT M.Code, SUM(D.Amount) SubTotal,
(SUM(D.Amount) * M.Factor) Total
FROM Detail D, Master M
WHERE (D.Code = M.Code) AND (M.Code = :Code)
GROUP BY M.Code
Then, set the DataSource property of the Detail TQuery component to the
TDataSource for the Master TQuery. This will cause the Detail TQuery
component to only produce one total, for the records with the same Code
value as in the current record of the Master TQuery. Scroll the Master
TQuery and the Detail TQuery is automatically requeried to produce a new
total for the new Code value.
==========================================================================
Steve Koterski "Computers are useless. They can only give
Technical Publications you answers."
Borland -- Pablo Picasso (1881-1973)
http://www.borland.com/techpubs/delphi