marioway
asked on
SUM problem in SQL
Hi, I have a problem with a query and don't know what I'm doing wrong.
I have two Paradox tables: Factuurartikel.db and Temp1.db
I want to get the sum of Factuurartikel.Aantal between the dates Temp1.Datum1 and Temp1.Datum2, where Factuurartikel.Omschrijvin g = Temp1.Omschrijving.
When i run the query and want to get the result I get 2 (The first result of the query) instead of 39 (Total of Aantal between the given dates.
Here's the SQL text:
SELECT Factuurartikel.Code, SUM(Factuurartikel.Aantal) AS som1, Temp1.Datum1, Temp1.Datum2, Temp1.Omschrijving, Temp1.ArtikelID, Factuurartikel.Omschrijvin g, Factuurartikel.ArtikelID, Factuurartikel.Artikelgroe pID, Factuurartikel.Datumverkoo p
FROM "factuurartikel.DB" Factuurartikel, "temp1.DB" Temp1
WHERE (Factuurartikel.Omschrijvi ng = Temp1.Omschrijving )
AND Factuurartikel.Datumverkoo p BETWEEN Temp1.Datum1 AND Temp1.Datum2
GROUP BY Temp1.Datum1, Factuurartikel.Artikelgroe pID, Factuurartikel.Code, Temp1.Datum2, Temp1.Omschrijving, Temp1.ArtikelID, Factuurartikel.ArtikelID, FactuurArtikel.Omschrijvin g, FactuurArtikel.Datumverkoo p
Please help ASAP
Thx
Marioway
I have two Paradox tables: Factuurartikel.db and Temp1.db
I want to get the sum of Factuurartikel.Aantal between the dates Temp1.Datum1 and Temp1.Datum2, where Factuurartikel.Omschrijvin
When i run the query and want to get the result I get 2 (The first result of the query) instead of 39 (Total of Aantal between the given dates.
Here's the SQL text:
SELECT Factuurartikel.Code, SUM(Factuurartikel.Aantal)
FROM "factuurartikel.DB" Factuurartikel, "temp1.DB" Temp1
WHERE (Factuurartikel.Omschrijvi
AND Factuurartikel.Datumverkoo
GROUP BY Temp1.Datum1, Factuurartikel.Artikelgroe
Please help ASAP
Thx
Marioway
ASKER
Hi BlackTigerX, I tried it, but the result is the same. There are no relationships between the two tables. I read through the questions concerning SUM and SQL --> nested query should be used ?
Regards
Mario
Regards
Mario
Try this (I think the GROUP BY clause must contain only fields from Factuurartikel table):
SELECT Factuurartikel.Code, SUM(Factuurartikel.Aantal) AS som1,
Temp1.Datum1, Temp1.Datum2, Temp1.Omschrijving, Temp1.ArtikelID,
Factuurartikel.Omschrijvin g, Factuurartikel.ArtikelID, Factuurartikel.Artikelgroe pID,
Factuurartikel.Datumverkoo p
FROM "factuurartikel.DB" Factuurartikel, "temp1.DB" Temp1
WHERE (Factuurartikel.Omschrijvi ng = Temp1.Omschrijving )
AND Factuurartikel.Datumverkoo p BETWEEN Temp1.Datum1 AND Temp1.Datum2
GROUP BY Factuurartikel.Code, Factuurartikel.Omschrijvin g, Factuurartikel.ArtikelID,
Factuurartikel.Artikelgroe pID, Factuurartikel.Datumverkoo p
SELECT Factuurartikel.Code, SUM(Factuurartikel.Aantal)
Temp1.Datum1, Temp1.Datum2, Temp1.Omschrijving, Temp1.ArtikelID,
Factuurartikel.Omschrijvin
Factuurartikel.Datumverkoo
FROM "factuurartikel.DB" Factuurartikel, "temp1.DB" Temp1
WHERE (Factuurartikel.Omschrijvi
AND Factuurartikel.Datumverkoo
GROUP BY Factuurartikel.Code, Factuurartikel.Omschrijvin
Factuurartikel.Artikelgroe
ASKER
Hi esoftbg, I get an error ("every simple fieled must ....") .
Regards
Mario
Regards
Mario
To get the SUM You must have
SELECT SUM(Factuurartikel.Aantal) AS som1
FROM "factuurartikel.DB" Factuurartikel, "temp1.DB" Temp1
WHERE (Factuurartikel.Omschrijvi ng = Temp1.Omschrijving ) AND Factuurartikel.Datumverkoo p BETWEEN Temp1.Datum1 AND Temp1.Datum2
If You want other fields You must decide from which row it should be
SELECT SUM(Factuurartikel.Aantal)
FROM "factuurartikel.DB" Factuurartikel, "temp1.DB" Temp1
WHERE (Factuurartikel.Omschrijvi
If You want other fields You must decide from which row it should be
ASKER
Hi evrybody, I think I solved this thing another way. The problem was that I always got the first result of the query, and not the total sum of all the records of the query. I did this this way:
i1:= query17Som1.Value;
for j:= 1 to query17.RecordCount - 1 do
begin
i1:= query17Som1.Value;
query17.Next;
i2:= query17som1.Value;
i1:= i1 + i2;
end;
The final result of this loop is i1 which is the total of all the records in the query, and this is what I wanted.
If anyone can show me how to do this by using just one query, I would appreciate it.
Regards
Mario
i1:= query17Som1.Value;
for j:= 1 to query17.RecordCount - 1 do
begin
i1:= query17Som1.Value;
query17.Next;
i2:= query17som1.Value;
i1:= i1 + i2;
end;
The final result of this loop is i1 which is the total of all the records in the query, and this is what I wanted.
If anyone can show me how to do this by using just one query, I would appreciate it.
Regards
Mario
Sorry Mario, obviously I am not on a right track ....
Best Regards
Emil
May be you need to use two Query components instead of one Query (The code below is only a suggestion - it is not tested):
QueryTemp1.SQL.Text :=
'SELECT Datum1, Datum2, Omschrijving, ArtikelID
FROM "temp1.DB"';
QueryFactuurArtikel.SQL.Te xt :=
'SELECT Code, SUM(Aantal) AS som1, Omschrijving, ArtikelID, ArtikelgroepID, Datumverkoop
FROM "factuurartikel.DB"
WHERE (Omschrijving = :Omschrijving ) AND Datumverkoop BETWEEN :Datum1 AND :Datum2
GROUP BY Code, SUM(Aantal) AS som1, Omschrijving, ArtikelID, ArtikelgroepID, Datumverkoop';
And use the event:
procedure Form1.QueryFactuurArtikelB eforeOpen( ....);
begin
QueryFactuurArtikel.ParamB yName('Oms chrijving' ).AsString := QueryTemp1.FieldByName('Om schrijving ').AsStrin g;
QueryFactuurArtikel.ParamB yName('Dat um1').AsSt ring := QueryTemp1.FieldByName('Da tum1').AsS tring;
QueryFactuurArtikel.ParamB yName('Dat um2').AsSt ring := QueryTemp1.FieldByName('Da tum2').AsS tring;
end;
Best Regards
Emil
May be you need to use two Query components instead of one Query (The code below is only a suggestion - it is not tested):
QueryTemp1.SQL.Text :=
'SELECT Datum1, Datum2, Omschrijving, ArtikelID
FROM "temp1.DB"';
QueryFactuurArtikel.SQL.Te
'SELECT Code, SUM(Aantal) AS som1, Omschrijving, ArtikelID, ArtikelgroepID, Datumverkoop
FROM "factuurartikel.DB"
WHERE (Omschrijving = :Omschrijving ) AND Datumverkoop BETWEEN :Datum1 AND :Datum2
GROUP BY Code, SUM(Aantal) AS som1, Omschrijving, ArtikelID, ArtikelgroepID, Datumverkoop';
And use the event:
procedure Form1.QueryFactuurArtikelB
begin
QueryFactuurArtikel.ParamB
QueryFactuurArtikel.ParamB
QueryFactuurArtikel.ParamB
end;
ASKER
Emil, As I mentioned above, I ran the this this query:
SELECT Factuurartikel.Code, Temp1.Datum1, Temp1.Datum2, Temp1.Omschrijving, Temp1.ArtikelID, Factuurartikel.Omschrijvin g, Factuurartikel.ArtikelID, Factuurartikel.Artikelgroe pID, Factuurartikel.Datumverkoo p, SUM(Factuurartikel.Aantal) AS som1
FROM "factuurartikel.DB" Factuurartikel, "temp1.DB" Temp1
WHERE (Factuurartikel.Omschrijvi ng = Temp1.Omschrijving )
AND Factuurartikel.Datumverkoo p BETWEEN Temp1.Datum1 AND Temp1.Datum2
GROUP BY Factuurartikel.Code, Temp1.Datum1, Temp1.Datum2, Temp1.Omschrijving, Temp1.ArtikelID, Factuurartikel.Omschrijvin g, Factuurartikel.ArtikelID, Factuurartikel.Artikelgroe pID, Factuurartikel.Datumverkoo p
and on the result of the query : two variables i1 and i2
i1:= query17Som1.Value;
for j:= 1 to query17.RecordCount - 1 do
begin
i1:= query17Som1.Value;
query17.Next;
i2:= query17som1.Value;
i1:= i1 + i2;
end;
i1 now gives me the result I want (= the sum of som1).
Perhaps a nested query could give the same result ?
Regards
Mario
SELECT Factuurartikel.Code, Temp1.Datum1, Temp1.Datum2, Temp1.Omschrijving, Temp1.ArtikelID, Factuurartikel.Omschrijvin
FROM "factuurartikel.DB" Factuurartikel, "temp1.DB" Temp1
WHERE (Factuurartikel.Omschrijvi
AND Factuurartikel.Datumverkoo
GROUP BY Factuurartikel.Code, Temp1.Datum1, Temp1.Datum2, Temp1.Omschrijving, Temp1.ArtikelID, Factuurartikel.Omschrijvin
and on the result of the query : two variables i1 and i2
i1:= query17Som1.Value;
for j:= 1 to query17.RecordCount - 1 do
begin
i1:= query17Som1.Value;
query17.Next;
i2:= query17som1.Value;
i1:= i1 + i2;
end;
i1 now gives me the result I want (= the sum of som1).
Perhaps a nested query could give the same result ?
Regards
Mario
Mario,
Could you send me the "factuurartikel" and "temp1" paradox tables to make some test ?
Emil
Could you send me the "factuurartikel" and "temp1" paradox tables to make some test ?
Emil
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Try this:
SELECT
F.Code, F.Omschrijving, F.ArtikelID, F.ArtikelgroepID, F.Datumverkoop, SUM(F.Aantal) AS som1
FROM "factuurartikel.DB" F
FULL OUTER JOIN "temp1.DB" T ON F.Omschrijving = T.Omschrijving
WHERE
Factuurartikel.Datumverkoo p BETWEEN T.Datum1 AND T.Datum2
GROUP BY
F.Code, F.Omschrijving, F.ArtikelID, F.ArtikelgroepID, F.Datumverkoop
Regards
Pierre
SELECT
F.Code, F.Omschrijving, F.ArtikelID, F.ArtikelgroepID, F.Datumverkoop, SUM(F.Aantal) AS som1
FROM "factuurartikel.DB" F
FULL OUTER JOIN "temp1.DB" T ON F.Omschrijving = T.Omschrijving
WHERE
Factuurartikel.Datumverkoo
GROUP BY
F.Code, F.Omschrijving, F.ArtikelID, F.ArtikelgroepID, F.Datumverkoop
Regards
Pierre
ASKER
Emil, the solution you sent me is perfect. Query now is:
SELECT SUM(Aantal) AS som1, Omschrijving, Code, ArtikelgroepID
FROM "factuurartikel.DB"
WHERE (Omschrijving = :Omschrijving )
AND (Datumverkoop >= :Datum1 )
AND (Datumverkoop <= :Datum2 )
GROUP BY Omschrijving, Code, ArtikelgroepID
thank you very much
Mario
SELECT SUM(Aantal) AS som1, Omschrijving, Code, ArtikelgroepID
FROM "factuurartikel.DB"
WHERE (Omschrijving = :Omschrijving )
AND (Datumverkoop >= :Datum1 )
AND (Datumverkoop <= :Datum2 )
GROUP BY Omschrijving, Code, ArtikelgroepID
thank you very much
Mario
Mario, you are welcome
Emil
Emil
first of all try grouping by the same as your select:
SELECT Factuurartikel.Code, Temp1.Datum1, Temp1.Datum2, Temp1.Omschrijving, Temp1.ArtikelID, Factuurartikel.Omschrijvin
FROM "factuurartikel.DB" Factuurartikel, "temp1.DB" Temp1
WHERE (Factuurartikel.Omschrijvi
AND Factuurartikel.Datumverkoo
GROUP BY Factuurartikel.Code, Temp1.Datum1, Temp1.Datum2, Temp1.Omschrijving, Temp1.ArtikelID, Factuurartikel.Omschrijvin
other thing that might be messing the query up, is if this is a 1 to many relationship between these 2 tables