Icon View Incident Report

Serious Serious
Reported By: Richard Harding
Reported On: 6/3/2012
For: Version 2.08 Build 3
# 3596 Using Sub-Queries in HAVING Clause Can Cause Incorrect Results

I have the following table, which stores pathology tests that are grouped into TestGroups.

I want to find which groups are duplicated. In the above list, FATS and FAT contain the same tests.

Grouping the results on T1.TestGroup, T2.TestGroup and counting the number of rows, I get different results for 2 different versions of the HAVING clause.

CREATE TABLE "Tests"
(
"TestGroup" VARCHAR(5) COLLATE "ANSI_CI" NOT NULL,
"Test" VARCHAR(5) COLLATE "ANSI_CI" NOT NULL,
CONSTRAINT "indPrimaryKey" PRIMARY KEY ("TestGroup", "Test")
)

INSERT INTO "Tests" VALUES ('FAT','CHOL')!
INSERT INTO "Tests" VALUES ('FAT','HDL')!
INSERT INTO "Tests" VALUES ('FAT','LDL')!
INSERT INTO "Tests" VALUES ('FAT','TRIG')!
INSERT INTO "Tests" VALUES ('FATS','CHOL')!
INSERT INTO "Tests" VALUES ('FATS','HDL')!
INSERT INTO "Tests" VALUES ('FATS','LDL')!
INSERT INTO "Tests" VALUES ('FATS','TRIG')!
INSERT INTO "Tests" VALUES ('LFT','ALB')!
INSERT INTO "Tests" VALUES ('LFT','ALP')!
INSERT INTO "Tests" VALUES ('LFT','ALT')!
INSERT INTO "Tests" VALUES ('LFT','AST')!
INSERT INTO "Tests" VALUES ('LFT','GGT')!
INSERT INTO "Tests" VALUES ('LFT','TBIL')!
INSERT INTO "Tests" VALUES ('LIPID','HDL')!
INSERT INTO "Tests" VALUES ('LIPID','LDL')!
INSERT INTO "Tests" VALUES ('TAM','ALT')!
INSERT INTO "Tests" VALUES ('TAM','GGT')!

First version:

SELECT T1.TestGroup, T2.TestGroup,
    Count(*) AS C1, Count(T2.Test) AS C2,
    (SELECT COUNT(*) FROM Tests AS T3 WHERE T3.TestGroup = T1.TestGroup) AS C3,
    (SELECT COUNT(*) FROM Tests AS T4 WHERE T4.TestGroup = T2.TestGroup) AS C4
 FROM Tests AS T1
    INNER JOIN
        Tests AS T2
            ON T1.Test = T2.Test AND T1.TestGroup < T2.TestGroup
GROUP BY T1.TestGroup, T2.TestGroup
HAVING
  C1 = C3 AND C2 = C4

Second version:

SELECT T1.TestGroup, T2.TestGroup,
    Count(*) AS C1, Count(T2.Test) AS C2,
    (SELECT COUNT(*) FROM Tests AS T3 WHERE T3.TestGroup = T1.TestGroup) AS C3,
    (SELECT COUNT(*) FROM Tests AS T4 WHERE T4.TestGroup = T2.TestGroup) AS C4
 FROM Tests AS T1
    INNER JOIN
        Tests AS T2
            ON T1.Test = T2.Test AND T1.TestGroup < T2.TestGroup
GROUP BY T1.TestGroup, T2.TestGroup
HAVING
   count(*) = (SELECT COUNT(*) FROM Tests AS T3
               WHERE T3.TestGroup = T1.TestGroup)  AND
   count(T2.Test) = (SELECT COUNT(*) FROM Tests AS T4
                     WHERE T4.TestGroup = T2.TestGroup)



Comments Comments and Workarounds
The problem was with EDB not binding sub-queries in HAVING clauses properly, thus causing the sub-queries to be executed as normal sub-queries instead of aggregate expressions. The workaround was to use the first version of the query.


Resolution Resolution
Fixed Problem on 7/21/2012 in version 2.09 build 1


Products Affected Products Affected
ElevateDB Additional Software and Utilities
ElevateDB DAC Client-Server
ElevateDB DAC Client-Server with Source
ElevateDB DAC Standard
ElevateDB DAC Standard with Source
ElevateDB DAC Trial
ElevateDB LCL Standard with Source
ElevateDB PHP Standard
ElevateDB PHP Standard with Source
ElevateDB PHP Trial
ElevateDB VCL Client-Server
ElevateDB VCL Client-Server with Source
ElevateDB VCL Standard
ElevateDB VCL Standard with Source
ElevateDB VCL Trial

Image