sql >> データベース >  >> RDS >> PostgreSQL

PostgreSQLでの1日の累積合計の計算

    集計関数とウィンドウ関数を一緒に使用できます:

    select date_trunc('day', date_created) as day_created,
           location_state,
           count(*) opp_count,
           sum(count(*)) over (partition by location_state order by min(date_created)) as cumulative_opps_received
    from t
    group by day_created
    order by location_state, day_created;
    

    パーセンテージが必要な場合は分割できます:

    select date_trunc('day', date_created) as day_created,
           location_state,
           count(*) opp_count,
           sum(count(*)) over (partition by location_state order by min(date_created))  as cumulative_opps_received,
           (sum(count(*)) over (partition by location_state order by min(date_created)) * 1.0 /
            sum(count(*)) over (partition by location_state)
           ) as cumulative_ratio
    from t
    group by day_created
    order by location_state, day_created;
    



    1. Virtualmin:パスワードを変更した後はこのMySQLデータベースにアクセスできません

    2. sql個別、2列を取得

    3. 変数を使用してデータ入力をロード

    4. MySQLは、15分のウィンドウで過剰なトランザクションを見つけるためにSQLを作成する方法を教えてください。