Hello,
I am trying to run this query to find all instances where booktype + '_' + bookno exist more than once. Sometimes one of the fileds will be NULL so I did this:
When running this query I get the following messages:
Server: Msg 8120, Level 16, State 1, Line 1
Column 'mytable.bookType' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Server: Msg 8120, Level 16, State 1, Line 1
Column 'mytable.bookType' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Server: Msg 8120, Level 16, State 1, Line 1
Column 'mytable.bookNo' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Server: Msg 8120, Level 16, State 1, Line 1
Column 'mytable.bookNo' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
However, If I use isnull instead of coalesce, the query will run just fine:
Is there a reason for this? Is coalesce picking up something that ISNULL does not pick up?
Thanks!
Brian
I am trying to run this query to find all instances where booktype + '_' + bookno exist more than once. Sometimes one of the fileds will be NULL so I did this:
Code:
select * from mytable
where coalesce(booktype, '') + '_' + coalesce(bookno, '')
in (select coalesce(booktype, '') + '_' + coalesce(bookno, '') from mytable
group by coalesce(booktype, '') + '_' + coalesce(bookno, '')
having count(coalesce(booktype, '') + '_' + coalesce(bookno, ''))>1)
When running this query I get the following messages:
Server: Msg 8120, Level 16, State 1, Line 1
Column 'mytable.bookType' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Server: Msg 8120, Level 16, State 1, Line 1
Column 'mytable.bookType' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Server: Msg 8120, Level 16, State 1, Line 1
Column 'mytable.bookNo' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Server: Msg 8120, Level 16, State 1, Line 1
Column 'mytable.bookNo' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
However, If I use isnull instead of coalesce, the query will run just fine:
Code:
select * from mytable
where isnull(booktype, '') + '_' + isnull(bookno, '')
in (select isnull(booktype, '') + '_' + isnull(bookno, '') from mytable
group by isnull(booktype, '') + '_' + isnull(bookno, '')
having count(isnull(booktype, '') + '_' + isnull(bookno, ''))>1)
Is there a reason for this? Is coalesce picking up something that ISNULL does not pick up?
Thanks!
Brian