samedi 26 mars 2016

Ignore day duplicates from timestamps in sqlite table selection

I have a sqlite database that contains timstamps and tick data. The timestamps contain date and time. For an 'end-of-day' analysis i want to leave out the timestamps duplicates from the same date. It doesn't matter which database row of the date duplicates is selected.

The table looks like this:

CREATE TABLE StockQuotes
    (`Timestamps` varchar(19), `Open` float)
;

INSERT INTO StockQuotes
    (`Timestamps`, `Open`)
VALUES
    ('2010-09-16 13:16:22', 33.63),
    ('2010-09-17 13:16:22', 33.53),
    ('2010-09-20 11:26:30', 33.46),
    ('2010-09-20 13:16:22', 33.46),
    ('2010-09-21 11:26:30', 33.76),
    ('2010-09-22 11:26:30', 33.56),
    ('2010-09-23 11:26:30', 33.86),
    ('2010-09-23 13:26:30', 33.86)
; 

My desired result is:

Timestamps              Open
2010-09-16 13:16:22     33.63
2010-09-17 13:16:22     33.53
2010-09-20 11:26:30     33.46
2010-09-21 11:26:30     33.76
2010-09-22 11:26:30     33.56
2010-09-23 11:26:30     33.86

or

Timestamps              Open
2010-09-16 13:16:22     33.63
2010-09-17 13:16:22     33.53
2010-09-20 13:16:22     33.46
2010-09-21 11:26:30     33.76
2010-09-22 11:26:30     33.56
2010-09-23 13:26:30     33.86

I can find the duplicates by this query:

SELECT Timestamps, COUNT(SubTS) AS CountSub FROM
(
  SELECT Timestamps,substr (Timestamps,1,10) AS SubTS, Open
  FROM StockQuotes
)
GROUP BY SubTS HAVING (COUNT(SubTS)>1);

But finally this does not work:

SELECT * FROM StockQuotes WHERE Timestamps NOT IN
(
SELECT Timestamps, COUNT(SubTS) AS CountSub FROM
(
  SELECT Timestamps,substr (Timestamps,1,10) AS SubTS, Open
  FROM StockQuotes
)
GROUP BY SubTS HAVING (COUNT(SubTS)>1)
);

What is my mistake?

Aucun commentaire:

Enregistrer un commentaire