我正在尝试使用liquiBase yaml changelog文件在SQL Server中创建一个视图,其中每一行如下:
employee_id | provider_id | days_remaining | employed_dates
并且有三个表
分配-作业ID,作业ID,员工ID,开始日期,结束日期,提供者ID
employee_id
为行
provider_id
是provider_id
的{{1}},为此job_id
end_date
employee_id
是上述days_remaining
employee_id
的最后一个分配结束日期之前剩余的天数。
provider_id
是上述employed_dates
的所有assignment_id
的所有employee_id
的串联到目前为止,我的代码:
provider_id
我有两个问题:
是否可以避免重复计算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,我该怎么办?
是否可以修改上面的内容,以便将每个employee_id列在多行中(针对与该employee_id相关联的每个provider_id)? UNION无效,因为liquibase不支持循环或某种for-each构造,并且CROSS JOINS无效,因为它将employee_id与每个provider_id配对,无论它们是否关联。
答案 0 :(得分:1)
对不起,之前并没有明确考虑,可以通过在末尾添加以下INNER JOIN来解决:
position: absolute