0

我正在尝试使用 liquiBase yaml 更改日志文件在 SQL Server 中创建一个视图,其中每一行如下所示:

employee_id | provider_id | days_remaining | employed_dates

并且有三张桌子

  • EMPLOYEE -employee_id、first_name、last_name
  • 工作 - job_id、employee_id、start_date、end_date、provider_id
  • 分配 - assignment_id、job_id、employee_id、start_date、end_date、provider_id

  • employee_id给定的行

  • provider_id是最新的provider_id_job_idend_dateemployee_id
  • days_remaining是距离最后一个分配结束天的天employee_idprovider_id
  • employed_dates是以上所有的assignment_id串联employee_idprovider_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

我有两个问题:

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

    SELECT TOP 1 provider_id 
    FROM JOB  
    WHERE JOB.employee_id = employee_id  
    ORDER BY end_date DESC 
    

    由于 liquibase 中不支持变量、变量表或 CTE,我还能怎么做呢?

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

4

1 回答 1

1

抱歉,之前没有想清楚,这可以通过最后的以下 INNER JOIN 来解决:

          INNER JOIN (
            SELECT DISTINCT employee_id, provider_id from JOB
          ) as DT
          ON EMPLOYEE.employee_id = DT.employee_id
于 2019-10-31T17:07:32.540 回答