Icon View Incident Report

Serious Serious
Reported By: Robert Wachtel
Reported On: 7/12/2002
For: Version 3.12 Build 1
# 1156 Single Row Aggregate SQL Statements with Joins Results in Improper Results

Given are two table (Payments and PaymentTypes):

Payment

Amount TypeID
100,00 1
50,00 2
150,00 3
200,00 1

PaymentTypes

ID IsCash
1 True
2 False
3 True

Now I want to summarize all cash payed amounts (TypeID 1 and 3). That should be 100,00 + 150,00 + 200,00 = 450,00.

But

select sum(P.Amount) as SumAmount
from Payments P
join PaymentTypes PT on (PT.ID = P.TypeID)
where PT.IsCash = true

returns 350,00.

The workaround is using a group by clause.

* SQL-92 Table Creation Script with DBISAM Extensions */

CREATE TABLE IF NOT EXISTS "Payments"
(
   "Amount" MONEY,
   "TypeID" INTEGER
LANGUAGE "ANSI Standard" SORT "Default Order"
USER MAJOR VERSION 1
);

INSERT INTO "Payments" VALUES (100 , 1) NOAUTOINC;
INSERT INTO "Payments" VALUES (50 , 2) NOAUTOINC;
INSERT INTO "Payments" VALUES (150 , 3) NOAUTOINC;
INSERT INTO "Payments" VALUES (200 , 1) NOAUTOINC;

CREATE TABLE IF NOT EXISTS "PaymentTypes"
(
   "ID" AUTOINC,
   "IsCash" BOOLEAN
LANGUAGE "ANSI Standard" SORT "Default Order"
USER MAJOR VERSION 1
);

INSERT INTO "PaymentTypes" VALUES (1 , True) NOAUTOINC;
INSERT INTO "PaymentTypes" VALUES (2 , False) NOAUTOINC;
INSERT INTO "PaymentTypes" VALUES (3 , True) NOAUTOINC;

CODE TO REPRODUCE ERROR:

select sum(P.Amount) as SumAmount
from Payments P
join PaymentTypes PT on (PT.ID = P.TypeID)
where PT.IsCash = true

WORKAROUND:

select 1 as InternalDummyID,
sum(P.Amount) as SumAmount
from Payments P
join PaymentTypes PT on (PT.ID = P.TypeID)
where PT.IsCash = true
group by InternalDummyID



Resolution Resolution
Fixed Problem on 7/13/2002 in version 3.13 build 1
Image