sql migrate.sql

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql migrate.sql相关的知识,希望对你有一定的参考价值。

WITH prepared AS (
    SELECT
      issue_task_id,
      MAX(stage_id)     AS stage_id,
      MAX(executor_id)  AS executor_id,
      MAX(day_id)          day_id,
      MAX(project_id)      project_id,
      MAX(area_id)         area_id,
      MAX(state_id)        state_id,
      MAX(location_id)     location_id,
      MAX(week_id)         week_id,
      MAX(repair_type_id)  repair_type_id,
      MAX(danger_id)       danger_id,
      MAX(country_id)      country_id,
      MAX(region_id)       region_id,
      MAX(city_id)         city_id,
      MAX(place_id)        place_id,
      MAX(ruad_id)         ruad_id,
      MAX(CASE WHEN md_measure_id = '1'
        THEN value END) AS area,
      MAX(CASE WHEN md_measure_id = '2'
        THEN value END) AS length,
      MAX(CASE WHEN md_measure_id = '4'
        THEN value END) AS depth,
      0
    FROM f_road
    GROUP BY issue_task_id, stage_id

)
INSERT INTO snapshot_road (issue_task_id, stage_id, executor_id, day_id, project_id, area_id, state_id, location_id,
                           week_id, repair_type_id, danger_id, country_id, region_id, city_id, place_id, ruad_id,
                           area, length, depth, actuality) SELECT *
                                                           FROM prepared;


WITH ranked AS (
    SELECT
      rank()
      OVER (PARTITION BY issue_task_id
        ORDER BY stage_id DESC) AS rnk,
      id
    FROM snapshot_road
), pre AS (
    SELECT id
    FROM ranked
    WHERE rnk = 1
)
UPDATE snapshot_road
SET actuality = 1 FROM pre
WHERE pre.id = snapshot_road.id

WITH pre AS (
    SELECT
      issue_task_id,
      stage_id,
      area,
      length,
      depth,
      rank()
      OVER (PARTITION BY issue_task_id
        ORDER BY stage_id DESC)
    FROM snapshot_road
    WHERE area IS NOT NULL AND actuality = 0
    ORDER BY id
), last_tasks AS (
    SELECT
      issue_task_id,
      area,
      length,
      depth
    FROM pre
    WHERE rank = 1
)
UPDATE snapshot_road
SET area = last_tasks.area, length = last_tasks.length, depth = last_tasks.depth FROM last_tasks
WHERE snapshot_road.issue_task_id = last_tasks.issue_task_id
      AND snapshot_road.area IS NULL

以上是关于sql migrate.sql的主要内容,如果未能解决你的问题,请参考以下文章

Flyway Migrate [SQL] - 处理对象依赖关系

SQL*Plus中怎么执行多个*sql脚本文件

sql 2008和sql 2012的区别

sql [sql技巧]一些sql技巧#sql

sql 2008 r2 在sql 2008上兼容么

SQL基础