Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9290885
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T20:31:20+00:00 2026-06-18T20:31:20+00:00

I have been working on the query below, and with some insight from members

  • 0

I have been working on the query below, and with some insight from members here I finally got it to run without errors.

My problem now is I need to adjust the date parameters for my YTD columns 2013 and 2012.
This query below worked fine before I tried to adjust the YTD columns.

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

I need the YTD 2013 and 2012 columns to have another date parameter, so I switched my YTD syntax to the below, and I get this error

Msg 245, Level 16, State 1, Line 1
Conversion failed when converting the varchar value ‘1/1/2013’ to data type int.

I would like to use this type of date parameter only for YTD “> = ‘1/1/2013’ and 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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T20:31:21+00:00Added an answer on June 18, 2026 at 8:31 pm

    Ok, you are trying to compare Entryday, wich is a number (1 to 31), with a date, that’s why you get an error. You actually don’t need that column at all, you can just do:

    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]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a rather lengthy query i have been working with that is throwing
i have been working on query which uses compute by clause for avg and
I am having trouble writing a query. I have been working with UNION on
I'm having trouble with some MYSQL. The code below has been working fine until
For the problem below, I got the query working. But when I am running
I have been working with a Linq query in a Silverlight application which returns
I have a really weird issue with an SQL query I've been working with
Following is the schema of my database. I have been working on a query,
Have been working on this question for a couple hours and have come close
I have been working with SQL Server as a Developer a while. One thing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.