B4J Question SQL DB Exclude duplicate entries

strupp01

Active Member
Licensed User
Longtime User
I have a database 'test' with a table 'Allgemein. This table has hundreds of entries.
The columns are 'Datum_Aufzeichnung', 'Uhrzeit', 'Druck' and 'Anschluesse'.
I want to find the sum of the column 'Druck' in the time from '170402' to '170403'.
This is still possible for me with

Test = sql_SchlafGut.ExecQuerySingleResult("SELECT sum(Druck) FROM Allgemein where Datum_Aufzeichnung <= 170403 and Datum_Aufzeichnung >= 170402 order by Datum_Aufzeichnung ASC")

The result from the attached DB is 1507.
However, because the record_date is partially present in the table, my result does not match. The number of 'Druck' for 'Aufzeichnungs_Datum' can only be counted once. The result must be 501.

That means I have to use DISTINCT at the record_date.
Here I am with my Latin at the end and hope to get an SQL expert the solution.
 

Attachments

  • Test.zip
    541 bytes · Views: 210

Jeffrey Cameron

Well-Known Member
Licensed User
Longtime User
I think you will need to create a sub-select to accomplish what you are trying to do (if I understand your problem).

Try:
B4X:
SELECT SUM(Druck) FROM
(SELECT DISTINCT Datum_Aufzeichnung, Druck FROM Allgemein where Datum_Aufzeichnung <= 170403
and Datum_Aufzeichnung >= 170402 order by Datum_Aufzeichnung ASC)

(Note that you don't really need the order by clause in this case).
 
Upvote 0
Top