max statment in sql 2008 not working

Guys

I am trying to do a max statement on the following t-sql

select 
udb.fk_cmdb_id,
udb.ad_login,
udb.fullname,
udb.firstname,
udb.surname,
udb.job_title, 
udb.department, 
udb.mobiletel,
udb.faxtel,
udb.email,
udb.user4,
udb.location,
udb.site,
udb.postcode,
udb.telext,
udb.fk_company_id,
udb.companyname,
udb.fk_manager,
udb.status, 
cmdb.ck_config_item, 
cmdb.ck_config_type, 
cmdb.cmdb_status, 
cmdb.fk_status_level, 
cmdb.fk_userdb, 
cmdb.pk_auto_id, 
max((convert(varchar, DATEADD(second, diary.updatedonx, '19700101') ,121))) as date1 ,
diary.updatetxt, 
diary.analystid, 
diary.udcode, 
diary.udsource, 
diary.fk_ci_id, 
diary.pk_auto_id 

from 

config_itemi cmdb, 
userdb udb, 
config_diary diary  

where 

cmdb.fk_userdb = udb.keysearch 
and cmdb.pk_auto_id = diary.fk_ci_id 
and cmdb.ck_config_type like 'HARDWARE%' 

group by

udb.fk_cmdb_id,
udb.ad_login,
udb.fullname,
udb.firstname,
udb.surname,
udb.job_title, 
udb.department, 
udb.mobiletel,
udb.faxtel,
udb.email,
udb.user4,
udb.location,
udb.site,
udb.postcode,
udb.telext,
udb.fk_company_id,
udb.companyname,
udb.fk_manager,
udb.status, 
cmdb.ck_config_item, 
cmdb.ck_config_type, 
cmdb.cmdb_status, 
cmdb.fk_status_level, 
cmdb.fk_userdb, 
cmdb.pk_auto_id, 
diary.updatetxt, 
diary.analystid, 
diary.udcode, 
diary.udsource, 
diary.fk_ci_id, 
diary.pk_auto_id 

Open in new window


it errors with msg

The text, ntext, and image data types cannot be compared or sorted, except when using IS NULL or LIKE operator.

the field  diary.updatedonx that is getting maxed is originally in seconds and im converting it to a date time and now want to just return the lastest line

Any ideas

Regards
DarrenJacksonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Brian CroweDatabase AdministratorCommented:
You can't group by text, ntext, or image datatypes.

Try using it as a windowed function.  You will have to remove any fields from the PARTITION BY section that are of the types mentioned.  At this point you can also remove your GROUP BY clause.

SELECT ...
   MAX((convert(varchar, DATEADD(second, diary.updatedonx, '19700101') ,121))) OVER (PARTITION BY udb.fk_cmdb_id,
udb.ad_login,
udb.fullname,
udb.firstname,
udb.surname,
udb.job_title,
udb.department,
udb.mobiletel,
udb.faxtel,
udb.email,
udb.user4,
udb.location,
udb.site,
udb.postcode,
udb.telext,
udb.fk_company_id,
udb.companyname,
udb.fk_manager,
udb.status,
cmdb.ck_config_item,
cmdb.ck_config_type,
cmdb.cmdb_status,
cmdb.fk_status_level,
cmdb.fk_userdb,
cmdb.pk_auto_id,
diary.updatetxt,
diary.analystid,
diary.udcode,
diary.udsource,
diary.fk_ci_id,
diary.pk_auto_id ) as date1 ,
0
LowfatspreadCommented:
then you don't want to max you just want to select the last diary entry for each "row"
so do a subselect on diary and obtain that using the row_number() windowing function...



select 
udb.fk_cmdb_id,
udb.ad_login,
udb.fullname,
udb.firstname,
udb.surname,
udb.job_title, 
udb.department, 
udb.mobiletel,
udb.faxtel,
udb.email,
udb.user4,
udb.location,
udb.site,
udb.postcode,
udb.telext,
udb.fk_company_id,
udb.companyname,
udb.fk_manager,
udb.status, 
cmdb.ck_config_item, 
cmdb.ck_config_type, 
cmdb.cmdb_status, 
cmdb.fk_status_level, 
cmdb.fk_userdb, 
cmdb.pk_auto_id, 
max((convert(varchar, DATEADD(second, diary.updatedonx, '19700101') ,121))) as date1 ,
diary.updatetxt, 
diary.analystid, 
diary.udcode, 
diary.udsource, 
diary.fk_ci_id, 
diary.pk_auto_id 

from 

config_itemi cmdb, 
userdb udb, 
(select 
updatetxt, 
analystid, 
udcode, 
udsource, 
fk_ci_id, 
pk_auto_id 
,updateonx
,row_number() over (partition by fk_ci_id order by updateonx desc) as rn
   from config_diary ) as diary  

where 

cmdb.fk_userdb = udb.keysearch 
and cmdb.pk_auto_id = diary.fk_ci_id 
and diary.rn=1
and cmdb.ck_config_type like 'HARDWARE%' 

Open in new window

0
PortletPaulfreelancerCommented:
If you are really just wanting "the most recent diary information" as part of the output then using row_number() as outlined by LowFatSpeed would be the way to go. But; you don't then also need the MAX( ... ) in the outer query and it should be removed.


I'd prefer to see you using ANSI join syntax, look how much easier the where clause when this is adopted :)
SELECT
        udb.fk_cmdb_id
      , udb.ad_login
      , udb.fullname
      , udb.firstname
      , udb.surname
      , udb.job_title
      , udb.department
      , udb.mobiletel
      , udb.faxtel
      , udb.email
      , udb.user4
      , udb.location
      , udb.site
      , udb.postcode
      , udb.telext
      , udb.fk_company_id
      , udb.companyname
      , udb.fk_manager
      , udb.status
      , cmdb.ck_config_item
      , cmdb.ck_config_type
      , cmdb.cmdb_status
      , cmdb.fk_status_level
      , cmdb.fk_userdb
      , cmdb.pk_auto_id
      , CONVERT(varchar, DATEADD(SECOND, diary.updatedonx, '19700101'), 121) AS date1
      , diary.updatetxt
      , diary.analystid
      , diary.udcode
      , diary.udsource
      , diary.fk_ci_id
      , diary.pk_auto_id

FROM config_itemi cmdb
INNER JOIN userdb udb ON cmdb.fk_userdb = udb.keysearch
INNER JOIN (
             SELECT
                     updatetxt
                   , analystid
                   , udcode
                   , udsource
                   , fk_ci_id
                   , pk_auto_id
                   , updateonx
                   , ROW_NUMBER() OVER (PARTITION BY fk_ci_id ORDER BY updateonx DESC) AS rn
             FROM config_diary
          ) AS diary ON cmdb.pk_auto_id = diary.fk_ci_id AND diary.rn = 1

WHERE cmdb.ck_config_type LIKE 'HARDWARE%'

Open in new window

The MAX() OVER() approach suggested by BriCrowe would be useful if you aren't just wanting "the most recent diary information" but do need that maximum date. However I think you would could trim back that partition by to just fk_ci_id (i.e. the field that diary is joined by)

i.e.
CONVERT(varchar, DATEADD(SECOND, ( MAX(diary.updatedonx) OVER (PARTITION BY diary.fk_ci_id) ) , '19700101'), 121) AS date1

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DarrenJacksonAuthor Commented:
I've proportioned points as I appreciated all the help.

Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Applications

From novice to tech pro — start learning today.