【问题标题】:Adding a max date subquery to a group by clause将最大日期子查询添加到 group by 子句
【发布时间】:2014-10-03 18:30:30
【问题描述】:

我需要在此报告中添加一个最大交易日期,以便我可以看到不属于查询参数范围的患者的最近交易日期。该报告将进入水晶报告,我计划将最后服务日期放在每个患者的组标题中。下面的代码是获取每个患者的最大日期。我需要插入结果,因此它需要属于以下报告的患者并给出他们的最大日期。无论输入的日期是什么。先感谢您。 SQL Server 2005。

select 
patient_id, 
Max(proc_chron) 

from patient_clin_tran pct
group by patient_id

交易代码

select patient_id,

(select p.case_status from patient p where p.patient_id = btb.patient_id and p.episode_id = (select max(episode_id) from patient p2 where p2.patient_id = p.patient_id)) as 'Status',
(select p.lname +', '+ p.fname from patient p where p.patient_id = btb.patient_id and p.episode_id = (select max(episode_id) from patient p2 where p2.patient_id = p.patient_id)) AS 'client',
Coverage_plan_id, 
(select proc_code from billing_transaction bt where bt.clinical_transaction_no = btb.clinical_transaction_no and bt.coverage_plan_id=btb.coverage_plan_id and bt.coverage_plan_id = btb.coverage_plan_id) as 'Procedure',
proc_chron, 
(select billing_amt from billing_transaction bt where bt.clinical_transaction_no = btb.clinical_transaction_no and bt.coverage_plan_id = btb.coverage_plan_id) as 'Billing Amount',
balance_amount,
(select max (accounting_date) from  billing_ledger bl where bl.clinical_transaction_no = btb.clinical_transaction_no and subtype = 'pa' and bl.coverage_plan_id = 'standard') as 'Last Payment on Trans',
(select max (instrument_date) from payment p where p.patient_id = btb.patient_id and  p.coverage_plan_id = 'standard') as 'Last Payment on Acct',
(select sum(balance_amount) from billing_transaction_balance btb2 where btb2.patient_id=btb.patient_id and btb2.coverage_plan_id=btb.coverage_plan_id and proc_chron <= CONVERT(CHAR(6), DATEADD(year, -1, DATEDIFF(day, 0,GETDATE())), 112) + '01' and btb2.coverage_plan_id   in('standard')) AS 'Balance'

from billing_transaction_balance btb

where proc_chron <= CONVERT(CHAR(6), DATEADD(year, -1, DATEDIFF(day, 0, GETDATE())), 112) + '01' and coverage_plan_id   in('standard') 

group by patient_id, proc_chron, coverage_plan_id, balance_amount, clinical_transaction_no

【问题讨论】:

    标签: sql sql-server-2005 crystal-reports subquery correlated-subquery


    【解决方案1】:

    在您的事务代码中,您需要将所有未聚合的 Select 列添加到 Group By。我在 Select 语句中计算了 8 个非聚合列,因此您必须在 Group By 中拥有全部 8 个列。

    【讨论】:

    • 我也注意到 group by 中的内容与 group by 不匹配。为每个患者添加最大日期(proc_chron)的子查询代码是什么?
    • 如果你只是想从上面的事务代码中的 billing_transaction_balance btb 中获取 max(proc_chron),你所要做的就是在 select 中将 proc_chron 更改为 max(proc_chron) 并将其从组中删除由。
    • (select Max(btb.proc_chron)),类似的东西?因为它没有给我最近的交易。
    • 好的,这可能是因为您分组的所有其他列。你能通过删除任何无关的列来简化这个查询吗?如果不添加一个 where 子句,通过加入同一个表来限制每个 patient_id 的有效 proc_chron。类似的东西:
    • and btb.proc_chron = (select max(btb3.proc_chron) from billing_transaction_balance btb3 where btb3.patient_id=btb.patient_id and proc_chron
    猜你喜欢
    • 1970-01-01
    • 2021-02-07
    • 2018-04-11
    • 2019-03-06
    • 2012-10-01
    • 2020-06-13
    • 1970-01-01
    • 2015-10-23
    • 1970-01-01
    相关资源
    最近更新 更多