Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Grouping by a formated column

Author  Topic 

homeguard
Starting Member

32 Posts

Posted - 2008-05-13 : 10:33:10
I have a date column that formated and i need to group by it, how do it do this? i get this error code:

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

here is my query:
select * from(
SELECT CONVERT(VARCHAR(10), AITDate, 110) AS FormatDate
, TechInt as Initials
, [Drug]
,[Missed PT Notes]
,[Pt Same Name]
,[PT Family]
,[Wrong PT]
,[Wrong Ques. Asked]
,[DAW]
,[Rx/Refills Omitted]
,[Allergy]
,[Wrong Sig]
,[Strength]
,[Wrong PT Address]
,[Quantity]
,[SVQ]
,[Refills]
,[Write Date]
,[Wrong MD/MD Info]
,[Rx Should be Questioned]
,[TSTF],[Non-Link Image]
,[DC Date],[Days Supply]
,[No Call Card]
,[Order Count]
,[Credit Hold Procedure]
,[Routing]
,[Sig Typo]
,[Verse Missing/Wrong]
,[Right Drug Wrong Form]
,[Others]
from tblaitinfo
pivot (count(ercode) for ercode in ([Drug],[Missed PT Notes],[Pt Same Name],[PT Family],[Wrong PT],[Wrong Ques. Asked],[DAW],[Rx/Refills Omitted],[Allergy],[Wrong Sig],[Strength],[Wrong PT Address],[Quantity],[SVQ],[Refills],[Write Date],[Wrong MD/MD Info],[Rx Should be Questioned],[TSTF],[Non-Link Image],[DC Date],[Days Supply],[No Call Card],[Order Count],[Credit Hold Procedure],[Routing],[Sig Typo],[Verse Missing/Wrong],[Right Drug Wrong Form],[Others])) p
)o
group by formatdate

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-05-13 : 10:36:56
Why are you grouping by format code and trying to retrieve details? once you group by date, you cant include the fields in the select list without applying any aggregation function. Can you tell what is your requirement?
Go to Top of Page

homeguard
Starting Member

32 Posts

Posted - 2008-05-13 : 11:48:57
here is how my data looks, i want to group by the date and sum the ercode columns

03-17-2008 jnm 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
03-17-2008 jnm 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
03-17-2008 jnm 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0
04-16-2008 jnm 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0
05-06-2008 jnm 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-05-13 : 11:55:24
SELECT FormatDate,max(TechInt),sum(Drug),sum([Missed PT Notes]),sum(otherfields),...
FROM (yourquery) t
GROUP BY t.FormatDate
Go to Top of Page

homeguard
Starting Member

32 Posts

Posted - 2008-05-13 : 12:41:22
very good, thank you sir
Go to Top of Page
   

- Advertisement -