sql >> Database teknologi >  >> RDS >> PostgreSQL

Opdag og slet huller i tidsserier

Jeg ville kun oprette én forespørgsel:

DELETE FROM mytable 
WHERE company in (
  SELECT Company 
  FROM (
    SELECT Company, 
      COUNT(CASE WHEN value IS NULL THEN 1 END) 
         OVER (PARTITION BY company ORDER BY id 
               ROWS BETWEEN CURRENT ROW AND 2 FOLLOWING) As cnt,
      COUNT(CASE WHEN value IS NULL THEN 1 END) 
         OVER (PARTITION BY company)
      / 
      COUNT(*) 
         OVER (PARTITION BY company) As p50
  ) alias
  WHERE cnt >= 3 OR p50 > 0.5
)

Et sammensat indeks på (virksomhed + værdi) kolonner kan hjælpe med at opnå en maksimal hastighed på denne forespørgsel.

REDIGER

Ovenstående forespørgsel virker ikke
Jeg har rettet det lidt, her er en demo:http://sqlfiddle.com/#!15/c9bfe/7
To ting er blevet ændret:
- OPDELING EFTER virksomhed ORDNER EFTER dato i stedet for ORDER BY id
- eksplicit cast til numerisk (fordi heltal er blevet trunkeret til 0):
OVER (OPDELING EFTER virksomhed)::numeric

  SELECT company, cnt, p50
  FROM (
    SELECT company, 
      COUNT(CASE WHEN value IS NULL THEN 1 END) 
         OVER (PARTITION BY company ORDER BY date 
               ROWS BETWEEN CURRENT ROW AND 2 FOLLOWING) As cnt,
      SUM(CASE WHEN value IS NULL THEN 1 ELSE 0 END) 
         OVER (PARTITION BY company)::numeric
      / 
      COUNT(*) 
         OVER (PARTITION BY company) As p50
    FROM mytable
  ) alias
--  WHERE cnt >= 3 OR p50 > 0.5 

og nu skulle sletteforespørgslen virke:

DELETE FROM mytable 
WHERE company in (
      SELECT company
      FROM (
        SELECT company, 
          COUNT(CASE WHEN value IS NULL THEN 1 END) 
             OVER (PARTITION BY company ORDER BY date 
                   ROWS BETWEEN CURRENT ROW AND 2 FOLLOWING) As cnt,
          SUM(CASE WHEN value IS NULL THEN 1 ELSE 0 END) 
             OVER (PARTITION BY company)::numeric
          / 
          COUNT(*) 
             OVER (PARTITION BY company) As p50
        FROM mytable
      ) alias
    WHERE cnt >= 3 OR p50 > 0.5
)


  1. Hvordan man betinget summerer to kolonner i PostgreSQL 9.3

  2. mysql_num_rows():det leverede argument er ikke en gyldig MySQL resultatressource

  3. Kontroller, om rækken findes i databasen, før du indsætter

  4. Laravel - multi-indsæt rækker og hent id'er