2017-12-28 11 views
1

Ich weiß, dass in SQL (Google Big Query) ist eine SUMME-Funktion, aber wenn ich Intervalle zählen muss. Zum BeispielSQL-Count Anzahl der Intervalle mehr als N sec

create table BATTLE (battleID VARCHAR(256), countryID VARCHAR(256), yearBATTLE INT); 

insert into BATTLE VALUES ('Battle1', 'Country1', 1567); 
insert into BATTLE VALUES ('Battle2', 'Country2', 1568); 
insert into BATTLE VALUES ('Battle3', 'Country2', 1570); 
insert into BATTLE VALUES ('Battle4', 'Country3', 1599); 
insert into BATTLE VALUES ('Battle5', 'Country2', 1575); 
insert into BATTLE VALUES ('Battle6', 'Country2', 1620); 
...... 

ich brauche Menge des Krieges zu finden, wo ein Krieg für eine Client-Sequenz des Gefechts mit Abständen von weniger als 10 Jahren.

+0

Ihre Frage ist unklar. Daten und Ergebnisse in Form einer Tabelle würden helfen. Und was ist "Quantität des Krieges"? –

Antwort

1

Unten ist für BigQuery Standard-SQL

#standardSQL 
SELECT 
    countryID, 
    MIN(yearBATTLE) start, 
    MAX(yearBATTLE) finish, 
    ARRAY_AGG(STRUCT(battleID, yearBATTLE)) battels 
FROM (
    SELECT 
    battleID, countryID, yearBATTLE, 
    SUM(delta) OVER(PARTITION BY countryID ORDER BY yearBATTLE) AS grp 
    FROM (
    SELECT 
     battleID, countryID, yearBATTLE, 
     IF(yearBATTLE - IFNULL(LAG(yearBATTLE) OVER(PARTITION BY countryID ORDER BY yearBATTLE), yearBATTLE) <= 10, 0, 1) AS delta 
    FROM `yourproject.yourdataset.battle` 
) 
) 
GROUP BY countryID, grp 
ORDER BY countryID, grp 

Sie können testen/spielen mit über Dummy-Daten aus Ihrer Frage mit (ich habe gerade 10 Jahre bis 5 Jahre zum Wohle Beispiel)

#standardSQL 
WITH `yourproject.yourdataset.battle` AS (
    SELECT 'Battle1' battleID, 'Country1' countryID, 1567 yearBATTLE UNION ALL 
    SELECT 'Battle2',   'Country2',   1568 UNION ALL 
    SELECT 'Battle3',   'Country2',   1570 UNION ALL 
    SELECT 'Battle4',   'Country3',   1599 UNION ALL 
    SELECT 'Battle5',   'Country2',   1575 UNION ALL 
    SELECT 'Battle6',   'Country2',   1620 
) 
SELECT 
    countryID, 
    MIN(yearBATTLE) start, 
    MAX(yearBATTLE) finish, 
    ARRAY_AGG(STRUCT(battleID, yearBATTLE)) battels 
FROM (
    SELECT 
    battleID, countryID, yearBATTLE, 
    SUM(delta) OVER(PARTITION BY countryID ORDER BY yearBATTLE) AS grp 
    FROM (
    SELECT 
     battleID, countryID, yearBATTLE, 
     IF(yearBATTLE - IFNULL(LAG(yearBATTLE) OVER(PARTITION BY countryID ORDER BY yearBATTLE), yearBATTLE) <= 5, 0, 1) AS delta 
    FROM `yourproject.yourdataset.battle` 
) 
) 
GROUP BY countryID, grp 
ORDER BY countryID, grp 

Ergebnis ist wie folgt

countryID start finish  battels.battleID battels.yearBATTLE  
Country1 1567 1567  Battle1    1567 
Country2 1568 1575  Battle2    1568 
           Battle3    1570 
           Battle5    1575 
Country2 1620 1620  Battle6    1620 
Country3 1599 1599  Battle4    1599 
+0

Grand danke. Das Skript ist erfolgreich. – Vadim

0

Ihre Frage ist nicht klar, aber wenn Sie meinen, dass ein Krieg eine Folge von Kämpfen ist, dann können Sie eine Sequenz erhalten, indem Sie die Tabelle mit sich selbst verbindet:

select * 
from BATTLE a 
join BATTLE b on a.countryId = b.countryId 
where abs(a.yearBATTLE - b.yearBATTLE) < 10 
and  a.BattleId <> b.battleId 
Verwandte Themen