我目前担任医疗保健分析师。在过去的几年里,我一直在使用 Microsoft Access 来查询和生成报告。我的公司现在希望我学习、扩展我的 SQL 知识并在以后每天使用 SQL Server2008。
我在理解 Access 和 SQL 之间的差异方面取得了一些进展,但它们仍然有很多我不熟悉的东西。
我想对子查询以及如何在 SQL 中使用 SUMIF 有所了解。下面我粘贴了步骤 (1) 我放在一起的 SQL 子查询的第一部分。步骤 (2) 是我想要转换为 SQL的 Microsoft Access 中的 SQL 视图,但需要对 SUMIF 语句的正确语法有所了解。
非常感谢任何见解...
步骤 (1) SQL Server 2008:
SELECT
[GROUPING_OF_CLINIC_ID].CLINIC_ID
, OUTPAT_ACCT_REC.CHARGE_CLASS
, [GROUPING_OF_CLINIC_ID].PATIENT_NUMBER
, [GROUPING_OF_CLINIC_ID].REGISTRATION_CD
, OUTPAT_REGISTER.PRIMARY_FINAN
, REPLACE(CONVERT(varchar(10), OUTPAT_ACCT_REC.ENTRY_DATE, 101), '/', '') AS ENTRY_DATE
, SUM(ENTRY_AMOUNT) AS ENTRY_AMOUNT
FROM
OUTPAT_REGISTER
LEFT JOIN
OUTPAT_BILL_REF
ON
(
OUTPAT_REGISTER.PATIENT_NUMBER = OUTPAT_BILL_REF.PATIENT_NUMBER
)
AND (OUTPAT_REGISTER.REGISTRATION_CD = OUTPAT_BILL_REF.REGISTRATION_CD)
LEFT JOIN
OUTPAT_ACCT_REC
ON
(
OUTPAT_BILL_REF.PATIENT_NUMBER = OUTPAT_ACCT_REC.PATIENT_NUMBER
)
AND (OUTPAT_BILL_REF.BILL_REFERENCE = OUTPAT_ACCT_REC.REFERENCE_NUM)
LEFT JOIN
(
SELECT
CLINIC_ID
, PATIENT_NUMBER
, REGISTRATION_CD
FROM
OP_VISIT_HISTRY
GROUP BY
CLINIC_ID
, PATIENT_NUMBER
, REGISTRATION_CD
--ORDER BY CLINIC_ID
) AS [GROUPING_OF_CLINIC_ID]
ON
(
OUTPAT_REGISTER.PATIENT_NUMBER = [GROUPING_OF_CLINIC_ID].PATIENT_NUMBER
)
AND (OUTPAT_REGISTER.REGISTRATION_CD = [GROUPING_OF_CLINIC_ID].REGISTRATION_CD)
GROUP BY
[GROUPING_OF_CLINIC_ID].CLINIC_ID
, OUTPAT_ACCT_REC.CHARGE_CLASS
, [GROUPING_OF_CLINIC_ID].PATIENT_NUMBER
, [GROUPING_OF_CLINIC_ID].REGISTRATION_CD
, OUTPAT_ACCT_REC.ENTRY_DATE
, OUTPAT_REGISTER.PRIMARY_FINAN
HAVING
OUTPAT_ACCT_REC.ENTRY_DATE > '12/31/2010'
步骤(2)从 SQL 视图中的访问:
SELECT [Non Recurring Clinic Step1].CLINIC_ID, dbo_HR_FINAN_CLASS.SUMMARY_CATGRY, [TABLE LAYOUT].ENTRY_TYPE,
Sum(IIf([entry_date] Between #12/1/2012# And #12/31/2012#,[sumofentry_amount],0)) AS [Dec'12], Sum(IIf([entry_date] Between #11/1/2012# And #11/30/2012#,[sumofentry_amount],0)) AS [Nov'12], Sum(IIf([entry_date] Between #10/1/2012# And #10/31/2012#,[sumofentry_amount],0)) AS [Oct'12], Sum(IIf([entry_date] Between #9/1/2012# And #9/30/2012#,[sumofentry_amount],0)) AS [Sept'12], Sum(IIf([entry_date] Between #8/1/2012# And #8/31/2012#,[sumofentry_amount],0)) AS [Aug'12], Sum(IIf([entry_date] Between #7/1/2012# And #7/31/2012#,[sumofentry_amount],0)) AS [Jul'12], Sum(IIf([entry_date] Between #6/1/2012# And #6/30/2012#,[sumofentry_amount],0)) AS [Jun'12], Sum(IIf([entry_date] Between #5/1/2012# And #5/31/2012#,[sumofentry_amount],0)) AS [May'12], Sum(IIf([entry_date] Between #4/1/2012# And #4/30/2012#,[sumofentry_amount],0)) AS [Apr'12], Sum(IIf([entry_date] Between #3/1/2012# And #3/31/2012#,[sumofentry_amount],0)) AS [Mar'12], Sum(IIf([entry_date] Between #2/1/2012# And #2/29/2012#,[sumofentry_amount],0)) AS [Feb'12], Sum(IIf([entry_date] Between #1/1/2012# And #1/31/2012#,[sumofentry_amount],0)) AS [Jan'12], Sum(IIf([entry_date] Between #12/1/2011# And #12/31/2011#,[sumofentry_amount],0)) AS [Dec'11], Sum(IIf([entry_date] Between #1/1/2012# And #12/31/2012#,[sumofentry_amount],0)) AS 2012YTD, Sum(IIf([entry_date] Between #1/1/2011# And #12/31/2011#,[sumofentry_amount],0)) AS 2011YTD
FROM [TABLE LAYOUT] INNER JOIN ([Non Recurring Clinic Step1] INNER JOIN ((dbo_OUTPAT_REGISTER INNER JOIN dbo_HR_FINAN_CLASS ON dbo_OUTPAT_REGISTER.PRIMARY_FINAN = dbo_HR_FINAN_CLASS.FINAN_CLASS_CD) INNER JOIN dbo_OUTPAT_AREA_CDS ON dbo_OUTPAT_REGISTER.REGISTER_AREA = dbo_OUTPAT_AREA_CDS.REGISTER_AREA) ON ([Non Recurring Clinic Step1].REGISTRATION_CD = dbo_OUTPAT_REGISTER.REGISTRATION_CD) AND ([Non Recurring Clinic Step1].PATIENT_NUMBER = dbo_OUTPAT_REGISTER.PATIENT_NUMBER)) ON [TABLE LAYOUT].ENTRY_CLASS = [Non Recurring Clinic Step1].CHARGE_CLASS
WHERE (((dbo_OUTPAT_AREA_CDS.REG_AREA_TYPE)<>"RE"))
GROUP BY [Non Recurring Clinic Step1].CLINIC_ID, dbo_HR_FINAN_CLASS.SUMMARY_CATGRY, [TABLE LAYOUT].ENTRY_TYPE
ORDER BY [Non Recurring Clinic Step1].CLINIC_ID;