Your real problem is that you have no idea what a table is! By definition, we never use IDENTITY in RDBMS. It is a proprietary table property that counts the physical insertion attempts (not even successes!) on one disk. Instead, we need a key for each table. A key is a subset oi attributes of the entity modeled by the table. We also use industry standards for identifiers, like a DUNS for suppler. Do you really have one with a hundred character name?? The USPS only uses 35 for an address line. But if you allow garbage data, you will get garbage data.You do not know ISO-11179 naming rules. Or that we have a DATE data type. The old Sybase getdate() is now replaced by the ANSI/ISO Standard CURRENT_TIMESTAMP. And we have the ANSI/ISO Standard insertion syntax. But what you really screwed up is the lack of tables for any relationships. It looks like your supplier_idclient, et al are fake pointers from inside a table to another table! You even used INTEGER, which was the default data type for 1970's network databases. That is not how we write RDBMS; we have references. There is no physical ordering in a table. CREATE TABLE Suppliers(supplier_duns CHAR(9) NOT NULL PRIMARY KEY, supplier_name VARCHAR(35) NOT NULL);INSERT INTO Suppliers VALUES ('000000001', 'Supplier Ltd');CREATE TABLE Documents(document_nbr CHAR(5) NOT NULL PRIMARY KEY, supplier_duns CHAR(9) NOT NULL REFERENCES Suppliers (supplier_duns), doc_txt VARCHAR(400) NOT NULL, doc_date CURRENT_TIMESTAMP DATE);INSERT INTO Documents(document_nbr, supplier_duns, doc_document_txt) VALUES ('00001', '000000001', 'sometext1') ('00002', '000000001', 'sometext2'), ('00003', '000000001', 'sometext3');CREATE TABLE Verifications(document_nbr CHAR(5) NOT NULL REFERENCES Documents(document_nbr) ON DELETE CASCADE, verification_date DATE NOT NULL,PRIMARY KEY (document_nbr, verification_date) verification_result CHAR(3) NOT NULL CHECK verification_result IN ( 'BAD', 'OK')), );INSERT INTO Verifications(document_nbr, verification_result, verification_date) VALUES (1, 'OK', '2015-03-22'),(1, 'BAD', '2015-03-18'),(1, 'OK', '2015-03-15'),(2, 'BAD', '2015-03-22'),(2, 'OK', '2015-03-13'),(2, 'OK', '2015-03-10'),(3, 'OK', '2015-03-22'),(3, 'OK', '2015-03-13'),(3, 'BAD', '2015-03-12');>> What I need for output is to sum [sic: you show a count, not a sum] the documents where last verification_result is BAD <<Here is a guess, based on the narrative:WITH Recent_Verifications AS(SELECT document_nbr, verification_result, MAX(verification_date) AS recent_verification_date FROM Verifications)Bad_Recent_VerificationsAS(SELECT D.supplier_name, COUNT(DISTINCT document_nbr) AS document_cnt FROM Recent_Verifications AS R, Documents AS DWHERE R.document_nbr = D.document_nbr AND R.verification_result = 'BAD')GROUP BY D.supplier_name)SELECT * FROM Bad_Recent_Verifications;