在where子句中使用子查询

时间:2014-07-28 18:47:41

标签: sql sql-server sql-server-2005 subquery where

我需要让这些报告的结果只显示状态为I的患者。不确定如何做到这一点,因为这个报告很重要,其中很多都是相关的。不确定我是否可以在where语句中放置一个子查询来覆盖整个事情。在此先感谢您的帮助。 SQL Server 2005

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

enter image description here

1 个答案:

答案 0 :(得分:1)

您的FROM子句只包含一个表(billing_transaction_balance),每个子查询都会引用该表。

如果您将该表的结果限制为只有Status ='I'的表,那么它将“覆盖”您的整个查询。您可以使用INNER JOIN对包含Status列的表进行最有效的操作。