对于Liquibase createView,是否可以存储子搜索的值?

时间:2019-10-31 02:35:34

标签: sql sql-server liquibase liquibase-sql

我正在尝试使用liquiBase yaml changelog文件在SQL Server中创建一个视图,其中每一行如下:

employee_id | provider_id | days_remaining | employed_dates

并且有三个表

  • 雇员-雇员ID,名字,姓氏
  • 作业-作业ID,员工ID,开始日期,结束日期,提供者ID
  • 分配-作业ID,作业ID,员工ID,开始日期,结束日期,提供者ID

  • employee_id为行

  • provider_idprovider_id的{​​{1}},为此job_id
  • 具有最新的end_date
  • employee_id是上述days_remaining
  • 上该employee_id的最后一个分配结束日期之前剩余的天数。
  • provider_id是上述employed_dates的所有assignment_id的所有employee_id的串联

到目前为止,我的代码:

provider_id

我有两个问题:

  1. 是否可以避免重复计算provider_id(以下代码)三次?

    databaseChangeLog:
      - changeSet:
      id: 100
      author: 100
      dbms: "mssql"
      changes:
        - createView:
            schemaName: ${schemaName}
            viewName: employee_history_view
            replaceIfExists: true
            selectQuery: >
              SELECT EMPLOYEE.employee_id,
              (
                SELECT TOP 1 provider_id from JOB
                where JOB.employee_id = employee_id
                order by end_date DESC
              ) as provider_id,
              employee.first_name,
              employee.last_name,
              DATEDIFF(DAY,
              (
                SELECT max(ASSIGNMENT.end_Date)
                from ASSIGNMENT
                where ASSIGNMENT.employee_id = EMPLOYEE.employee_id
                and ASSIGNMENT.provider_id = (
                                                SELECT TOP 1 provider_id from JOB
                                                where JOB.employee_id = employee_id
                                                order by end_date DESC
                                              )
                and end_date < getDate()
              ),
              getdate() ) as days_remaining,
              (
                '[' + SUBSTRING (
                  (
                    SELECT TOP 100 ', {"start":"'+CONVERT(CHAR(10),ASSIGNMENT.start_date,120)+'","end":"'+CONVERT(CHAR(10),ASSIGNMENT.end_date,120)+'"}'
                    from ASSIGNMENT
                    where ASSIGNMENT.employee_id = EMPLOYEE.employee_id
                    and ASSIGNMENT.provider_id = (
                                                    SELECT TOP 1 provider_id from JOB
                                                    where JOB.employee_id = EMPLOYEE.employee_id
                                                    order by end_date DESC
                                                  )
                    order by program_type DESC
                    for xml path ('')
                  ), 2, 8000) + ']'
              ) as employed_dates
              FROM ${employeeSchema}.EMPLOYEE
    

    由于liquibase中不支持变量,变量表或CTE,我该怎么办?

  2. 是否可以修改上面的内容,以便将每个employee_id列在多行中(针对与该employee_id相关联的每个provider_id)? UNION无效,因为liquibase不支持循环或某种for-each构造,并且CROSS JOINS无效,因为它将employee_id与每个provider_id配对,无论它们是否关联。

1 个答案:

答案 0 :(得分:1)

对不起,之前并没有明确考虑,可以通过在末尾添加以下INNER JOIN来解决:

position: absolute