0

I am trying to get a chart date vs number of patients .I am trying to make a chart using MS Chart in asp.net where I get date on x axis and number of patients on y axis. I am accepting from date and to date as input. I want number of patients between these 2 dates on each date between from and to date. I am trying something as follows

select 
convert(varchar,creation_Date,105) as 'creation_Date',
count(Pat_ID)
FROM Patient_Ref_master
where         
 (CONVERT(VARCHAR(10),Patient_Ref_master.creation_Date,111)  BETWEEN '2013/07/23' AND '2013/07/25')

group by Pat_ID

but giving me an error of

Msg 8120, Level 16, State 1, Line 1
Column 'Patient_Ref_master.creation_Date' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

Where I am going wrong.? Please suggest. Thanks

1
  • Why, why, why are you converting to a string Commented Jul 25, 2013 at 19:03

4 Answers 4

3

Group by creation_Date instead of Pat_ID

Sign up to request clarification or add additional context in comments.

Comments

1

You should also have Creation_Date in the group By clause. Following is the correct query:

    select 
convert(varchar,creation_Date,105) as 'creation_Date',
count(Pat_ID)
FROM Patient_Ref_master
where         
 (CONVERT(VARCHAR(10),Patient_Ref_master.creation_Date,111)  BETWEEN '2013/07/23' AND '2013/07/25')

group by creation_Date

2 Comments

too good Sonam...It worked. Can you please tell me where I was going wrong.?
Instead of Pat_Id, you should have Creation_Date in the group by clause.
1

You want to count patients on a date, so the date needs to be in the group by clause. However, you need to be sure it really is a date and not a datetime for what you are doing. The following uses your preferred format:

select convert(varchar,creation_Date, 105) as 'creation_Date',
       count(Pat_ID) as Numpateitns
FROM Patient_Ref_master
where CONVERT(VARCHAR(10),Patient_Ref_master.creation_Date, 111)  BETWEEN '2013/07/23' AND '2013/07/25'
group by convert(varchar,creation_Date, 105)

Comments

1

You wouldn't group by Pat_ID, you want to group by creation_Date, or your expression that is creation_Date. Also, I wouldn't convert your dates to varchars in your where clause. Just keep them as date times.

select 
convert(varchar,creation_Date,105) as 'creation_Date',
count(Pat_ID)
FROM Patient_Ref_master
where Patient_Ref_master.creation_Date >= convert(datetime,'2013/07/23') 
AND Patient_Ref_master.creation_Date < dateadd(day,1,convert(datetime,'2013/07/25'))
group by convert(varchar,creation_Date,105)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.