我正在尝试从表PROCESSED_FILE_LOGS中获取提交的2个文件的状态,无论是“通过”,“失败”还是“未提交”。
第一个文件名在文件名中不包含'PCR',第二个文件名在文件名中包含'PCR',这就是我如何区分这两个文件。
合同#是提交这两个文件的合同#我将4个不同的表组合在一起填充以下内容。
当我运行我的查询[下面]时,我对此表有两个问题:
i。'H0000'重复,尽管有查询,拉出MAX(PF.processed_date) - 我试图获取文件1和文件2的最后处理状态
ii。对于“H0000”,两个文件的状态不显示在同一行中。意思是 - 文件1的状态显示在一行中,状态为文件2显示在第二行。
我的查询:
select distinct
OC.cms_contract_number as 'Contract #'
,case when (PF.PROCESSED_FILE_NAME not like '%PCR%' and FS.DISPLAY is not null) then FS.display else 'Not Submitted' end as 'File 1 Status'
,case when (PF.PROCESSED_FILE_NAME like '%PCR%' and FS.DISPLAY is not null) then FS.display else 'Not Submitted' end as 'File 2 Status'
,MAX(PF.processed_date) as 'Date Submitted'
from
((((persons P join person_affiliations PA on P.person_id = PA.person_id)
join external_orgs EO on EO.org_id = PA.org_id)
join org_contracts OC on OC.org_contracts_id = PA.org_contracts_id)
left outer join processed_file_logs PF on PF.org_contracts_id = OC.org_contracts_id)
left outer join processed_file_status FS on FS.file_status_id = PF.file_status_id
group by OC.CMS_CONTRACT_NUMBER, PF.SUBMISSION_ID,
FS.DISPLAY, PF.PROCESSED_FILE_NAME
我哪里错了?
答案 0 :(得分:1)
好的,更新了这个答案以反映下面的评论 - 这里有很多语法我可能搞砸了,我不太可能在没有访问这些表的情况下为您提供完美的最终解决方案,但是你走了:
基本上,您的数据是脱节的,因为您希望将多行中的数据汇总到两个不同的列中。我们可以使用“with”查询来创建数据表,这些数据表为两个文件中的每个文件找到最后一次提交,然后通过合同编号密钥将其重新匹配到主数据集。
With A as (select OC.cms_contract_number as cms_contract_number, PF.PROCESSED_FILE_NAME as PROCESSED_FILE_NAME, FS.DISPLAY as DISPLAY, PF.processed_date as processed_date
from
((((persons P join person_affiliations PA on P.person_id = PA.person_id)
join external_orgs EO on EO.org_id = PA.org_id)
join org_contracts OC on OC.org_contracts_id = PA.org_contracts_id)
left outer join processed_file_logs PF on PF.org_contracts_id = OC.org_contracts_id)
left outer join processed_file_status FS on FS.file_status_id = PF.file_status_id )
select distinct
A.cms_contract_number as 'Contract #'
,max(s1.FileStatus) as 'File 1 Status'
,max(s2.FileStatus) as 'File 2 Status'
,MAX(A.processed_date) as 'Date Submitted'
from A
left outer join
(
select distinct
A.cms_contract_number as 'Contract'
,case when (A.DISPLAY is not null) then A.display else 'Not Submitted' end as 'FileStatus'
,MAX(A.processed_date) as 'DateSubmitted'
from A
where A.PROCESSED_FILE_NAME not like '%PCR%' or A.PROCESSED_FILE_NAME is null
group by A.CMS_CONTRACT_NUMBER, case when (A.DISPLAY is not null) then A.display else 'Not Submitted' end
) s1 on s1.Contract = A.CMS_CONTRACT_NUMBER and s1.DateSubmitted = A.processed_date
left outer join
(
select distinct
A.cms_contract_number as 'Contract'
,case when (A.DISPLAY is not null) then A.display else 'Not Submitted' end as 'FileStatus'
,MAX(A.processed_date) as 'DateSubmitted'
from A
where A.PROCESSED_FILE_NAME like '%PCR%' or A.PROCESSED_FILE_NAME is null
group by A.CMS_CONTRACT_NUMBER, case when (A.DISPLAY is not null) then A.display else 'Not Submitted' end
) s2 on s2.Contract = A.CMS_CONTRACT_NUMBER and s2.DateSubmitted = A.processed_date
group by A.CMS_CONTRACT_NUMBER
另请注意,如果您想要为每个File1和File2提交Last last日期,那么将它拉出来也是一个相当简单的练习:
在主查询中只需更改:
MAX(PF.processed_date) as 'DateSubmitted'
为:
max((select s1.DateSubmitted from StatusOfLastFile1 s1 where s1.Contract = A.CMS_CONTRACT_NUMBER)) as 'File 1 Last Submit',
max((select s2.DateSubmitted from StatusOfLastFile2 s2 where s2.Contract = A.CMS_CONTRACT_NUMBER)) as 'File 2 Last Submit'