我有一个适用于Jaspersoft报告的MySQL SQL:
SELECT pr.id AS project_id,
pr.project_name as project_name,
pr.export_event_id,
au.full_name,
ee.timestamp
FROM (
SELECT project.id, project.project_name, MAX(project.export_event_id) AS max_export_event_id FROM project INNER JOIN export_event iee ON project.export_event_id = iee.id
where IIF ($P{exportEventDate} IS NULL, TRUE, CONVERT(DATE, iee.timestamp) <= $P{exportEventDate})
GROUP BY project_name
) AS in_PR INNER JOIN project AS pr ON pr.project_name = in_PR.project_name AND pr.export_event_id = in_PR.max_export_event_id
INNER JOIN project_owner_base pob ON pob.id = pr.project_owner_id
INNER JOIN export_event AS ee ON pr.export_event_id = ee.id
INNER JOIN auth_user au ON pob.auth_user_id = au.id
WHERE IIF ($P{projectOwner} IS NULL, TRUE, au.id = $P{projectOwner})
我正在尝试将其转换为SQL Server,但无法找出相应的内容。
将$ P {...}视为&#39;?&#39;在动态SQL中 任何的想法?
答案 0 :(得分:0)
我认为这只是一个简单的OR语句。
Where @ProjectOwner IS NULL
OR au.id = @ProjectOwner
答案 1 :(得分:0)
您的查询非常接近。我会在任一数据库中完全删除IIF()
。结果是这样的:
SELECT pr.id AS project_id,
pr.project_name as project_name,
pr.export_event_id,
au.full_name,
ee.timestamp
FROM (SELECT p.project_name, MAX(p.export_event_id) AS max_export_event_id
FROM project p INNER JOIN
export_event iee
ON p.export_event_id = iee.id
WHERE ? IS NULL OR CONVERT(DATE, iee.timestamp) <= ?
GROUP BY p.project_name
) in_PR INNER JOIN
project pr
ON pr.project_name = in_PR.project_name AND
pr.export_event_id = in_PR.max_export_event_id INNER JOIN
project_owner_base pob
ON pob.id = pr.project_owner_id INNER JOIN
export_event ee
ON pr.export_event_id = ee.id INNER JOIN
auth_user au
ON pob.auth_user_id = au.id
WHERE ? IS NULL OR au.id = ?;
我将变量替换为?
(根据您的问题建议)。以上内容适用于任一数据库。
请注意,这也会修复子查询中的聚合以删除p.id
,这似乎是不必要的(并且应该在SQL Server中导致错误)。