我正在尝试使用 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_id
end_date
employee_id
days_remaining
是距离最后一个分配结束天的天employee_id
数provider_id
employed_dates
是以上所有的assignment_id
串联employee_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
我有两个问题:
是否可以避免重复计算 provider_id(下面的代码)三次?
SELECT TOP 1 provider_id FROM JOB WHERE JOB.employee_id = employee_id ORDER BY end_date DESC
由于 liquibase 中不支持变量、变量表或 CTE,我还能怎么做呢?
是否可以修改上述内容,使每个employee_id 列在多行中(对于与此employee_id 关联的每个provider_id)?UNION 不起作用,因为 liquibase 不支持循环或某种 for-each 构造,并且 CROSS JOINS 不起作用,因为它将employee_ids 与每个provider_id 配对,无论它们是否关联。