-1

私は以下のクエリに取り組んでいますが、ここのメンバーからの洞察により、ようやくエラーなしで実行できるようになりました。

私の問題は、2013年と2012年のYTD列の日付パラメーターを調整する必要があることです。以下のクエリは、YTD列を調整する前に正常に機能しました。

Select CLINIC_ID,SUMMARY_CATGRY,ENTRY_TYPE,
SUM(case when EntryMonth = 1 and EntryYear = 2012 then [sumofentry_amount] else 0 end) [Jan12],
SUM(case when EntryMonth = 2 and EntryYear = 2012 then [sumofentry_amount] else 0 end) [Feb12],
SUM(case when EntryMonth = 3 and EntryYear = 2012 then [sumofentry_amount] else 0 end) [Mar12],
SUM(case when EntryMonth = 4 and EntryYear = 2012 then [sumofentry_amount] else 0 end) [Apr12],
SUM(case when EntryMonth = 5 and EntryYear = 2012 then [sumofentry_amount] else 0 end) [May12],
SUM(case when EntryMonth = 6 and EntryYear = 2012 then [sumofentry_amount] else 0 end) [Jun12],
SUM(case when EntryMonth = 7 and EntryYear = 2012 then [sumofentry_amount] else 0 end) [Jul12],
SUM(case when EntryMonth = 8 and EntryYear = 2012 then [sumofentry_amount] else 0 end) [Aug12],
SUM(case when EntryMonth = 9 and EntryYear = 2012 then [sumofentry_amount] else 0 end) [Sep12],
SUM(case when EntryMonth = 10 and EntryYear = 2012 then [sumofentry_amount] else 0 end)[Oct12],
SUM(case when EntryMonth = 11 and EntryYear = 2012 then [sumofentry_amount] else 0 end)[Nov12],
SUM(case when EntryMonth = 12 and EntryYear = 2012 then [sumofentry_amount] else 0 end)[Dec12],
**SUM(case when EntryYear = 2012 then [sumofentry_amount] else 0 end) [2012YTD],
SUM(case when EntryYear = 2011 then [sumofentry_amount] else 0 end) [2011YTD]**
From
(
    SELECT [Non_Recurring_Clinic_Step1].CLINIC_ID, 
        HR_FINAN_CLASS.SUMMARY_CATGRY, 
        [TABLE_LAYOUT].ENTRY_TYPE, 
        datepart(month,[entry_date]) EntryMonth,
        datepart(year,[entry_date]) EntryYear,
         ENTRY_AMOUNT as [sumofentry_amount] 

 FROM [TABLE_LAYOUT]
 Inner Join

(
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, OUTPAT_ACCT_REC.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'
)AS [Non_Recurring_Clinic_Step1]

On([TABLE_LAYOUT].ENTRY_CLASS = [Non_Recurring_Clinic_Step1].CHARGE_CLASS)

Inner Join
OUTPAT_REGISTER

On([Non_Recurring_Clinic_Step1].PATIENT_NUMBER = OUTPAT_REGISTER.PATIENT_NUMBER)
And([Non_Recurring_Clinic_Step1].REGISTRATION_CD = OUTPAT_REGISTER.REGISTRATION_CD)

Inner Join
OUTPAT_AREA_CDS
On(OUTPAT_REGISTER.REGISTER_AREA = OUTPAT_AREA_CDS.REGISTER_AREA)

Inner Join
HR_FINAN_CLASS
On(OUTPAT_REGISTER.PRIMARY_FINAN = HR_FINAN_CLASS.FINAN_CLASS_CD)

Where OUTPAT_AREA_CDS.REG_AREA_TYPE <>'RE'
        and datepart(year,[entry_date]) in (2011, 2012)
) src
group by CLINIC_ID, SUMMARY_CATGRY, ENTRY_TYPE

YTD 2013および2012列に別の日付パラメーターが必要なため、YTD構文を以下に切り替えたところ、このエラーが発生しました

メッセージ245、レベル16、状態1、行
1varchar値「1/1/2013」をデータ型intに変換するときに変換に失敗しました。

このタイプの日付パラメータは、YTD ">='1/1/2013'およびEntryday<='1/31/2013'"にのみ使用したいと思います。

Select CLINIC_ID,SUMMARY_CATGRY,ENTRY_TYPE,
SUM(case when EntryMonth = 1 and EntryYear = 2013 then [sumofentry_amount] else 0 end) [Jan'13],
SUM(case when EntryMonth = 12 and EntryYear = 2012 then [sumofentry_amount] else 0 end) [Dec'12],
SUM(case when EntryMonth = 11 and EntryYear = 2012 then [sumofentry_amount] else 0 end) [Nov'12],
SUM(case when EntryMonth = 10 and EntryYear = 2012 then [sumofentry_amount] else 0 end) [Oct'12],
SUM(case when EntryMonth = 9 and EntryYear = 2012 then [sumofentry_amount] else 0 end) [Sept'12],
SUM(case when EntryMonth = 8 and EntryYear = 2012 then [sumofentry_amount] else 0 end) [Aug'12],
SUM(case when EntryMonth = 7 and EntryYear = 2012 then [sumofentry_amount] else 0 end) [Jul'12],
SUM(case when EntryMonth = 6 and EntryYear = 2012 then [sumofentry_amount] else 0 end) [Jun'12],
SUM(case when EntryMonth = 5 and EntryYear = 2012 then [sumofentry_amount] else 0 end) [May'12],
SUM(case when EntryMonth = 4 and EntryYear = 2012 then [sumofentry_amount] else 0 end)[Apr'12],
SUM(case when EntryMonth = 3 and EntryYear = 2012 then [sumofentry_amount] else 0 end)[Mar'12],
SUM(case when EntryMonth = 2 and EntryYear = 2012 then [sumofentry_amount] else 0 end)[Feb'12],
SUM(case when EntryMonth = 1 and EntryYear = 2012 then [sumofentry_amount] else 0 end)[Jan'12],
**SUM(case when Entryday > = '1/1/2013' and Entryday <= '1/31/2013' then 
[sumofentry_amount] else 0 end) [2013YTD],
SUM(case when Entryday > = '1/1/2012' and Entryday <= '1/31/2012' then 
[sumofentry_amount] else 0 end) [2012YTD]**

From
(
    SELECT [Non_Recurring_Clinic_Step1].CLINIC_ID, 
        HR_FINAN_CLASS.SUMMARY_CATGRY, 
        [TABLE_LAYOUT].ENTRY_TYPE, 
        datepart(month,[entry_date]) EntryMonth,
        datepart(year,[entry_date]) EntryYear,
        datepart(day,[entry_date])  Entryday,
         ENTRY_AMOUNT as [sumofentry_amount] 

 FROM [TABLE_LAYOUT]
 Inner Join

(
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, OUTPAT_ACCT_REC.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/2011'
)AS [Non_Recurring_Clinic_Step1]

On([TABLE_LAYOUT].ENTRY_CLASS = [Non_Recurring_Clinic_Step1].CHARGE_CLASS)

Inner Join
OUTPAT_REGISTER

On([Non_Recurring_Clinic_Step1].PATIENT_NUMBER = OUTPAT_REGISTER.PATIENT_NUMBER)
And([Non_Recurring_Clinic_Step1].REGISTRATION_CD = OUTPAT_REGISTER.REGISTRATION_CD)

Inner Join
OUTPAT_AREA_CDS
On(OUTPAT_REGISTER.REGISTER_AREA = OUTPAT_AREA_CDS.REGISTER_AREA)

Inner Join
HR_FINAN_CLASS
On(OUTPAT_REGISTER.PRIMARY_FINAN = HR_FINAN_CLASS.FINAN_CLASS_CD)

Where OUTPAT_AREA_CDS.REG_AREA_TYPE <>'RE'
        and datepart(year,[entry_date]) in (2012, 2013)
) src
group by CLINIC_ID, SUMMARY_CATGRY, ENTRY_TYPE
4

3 に答える 3

0

Entrydayは整数(こちらを参照)になるため、無効なキャストが得られます。全体に対して比較を実行する必要がありますentry_date

の比較もできますEntryYear = 2013 AND EntryMonth = 1 AND EntryDay IS BETWEEN 1 AND 31。ただし、日付全体を比較する場合は、各部分ではなく日付に対してチェックする最初のオプションを使用します。

于 2013-02-11T18:14:57.457 に答える
0

問題は、それEntrydayint機能しない日時と比較していることです。はDatePart(day, entry_date)1 ~ 31 の数値を返します。

を使用する代わりにdatepart(day,[entry_date]) Entryday、次のものを使用する必要がありますentry_date

SELECT [Non_Recurring_Clinic_Step1].CLINIC_ID, 
    HR_FINAN_CLASS.SUMMARY_CATGRY, 
    [TABLE_LAYOUT].ENTRY_TYPE, 
    datepart(month,[entry_date]) EntryMonth,
    datepart(year,[entry_date]) EntryYear,
    [entry_date],
     ENTRY_AMOUNT as [sumofentry_amount] 

次に、合計を取得するときに使用します。

SUM(case 
      when entry_date > = '1/1/2013' 
        and entry_date <= '1/31/2013' 
      then [sumofentry_amount] else 0 end) [2013YTD],
SUM(case 
      when entry_date > = '1/1/2012' 
        and entry_date <= '1/31/2012' 
    then [sumofentry_amount] else 0 end) [2012YTD]

しかし、YTD 値を取得するには、次のものが必要です。

SUM(case when EntryYear = 2013 then [sumofentry_amount] else 0 end) [2013YTD],
SUM(case when EntryYear = 2012 then [sumofentry_amount] else 0 end) [2012YTD]
于 2013-02-11T18:16:11.073 に答える
0

EntrydayOK、数字 (1 から 31) である を日付と比較しようとしているため、エラーが発生します。実際にはその列はまったく必要ありません。次のようにするだけです。

SUM(case when EntryYear = '2013' AND EntryMonth = 1 
    then [sumofentry_amount] else 0 end) [2013YTD],
SUM(case when EntryYear = '2012' AND EntryMonth = 1 
    then [sumofentry_amount] else 0 end) [2012YTD]
于 2013-02-11T18:16:29.753 に答える