SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 Old Forums
 CLOSED - General SQL Server
 Group question
 Forum Locked
 Printer Friendly
Author Previous Topic Topic Next Topic  

sonu
Posting Yak Master

110 Posts

Posted - 02/08/2005 :  13:26:08  Show Profile
Hello,

i have a quert like this:

select 
   somefields
from 
  sometable
where
  someconditions = someotherconditions
Having
  y = 'bla'
group by
  x

Now the problem is that if I run the query then it tells me that "Not a group by expression"

I do not want to group by "y". How can I get rid of that?
Thanks

spirit1
Cybernetic Yak Master

Slovenia
11750 Posts

Posted - 02/08/2005 :  13:28:58  Show Profile  Visit spirit1's Homepage
will this do?
group by comes before having, because having works on already formed groups of data.

select
somefields
from
sometable
where
someconditions = someotherconditions
group by
x
Having
y = 'bla'


Go with the flow & have fun! Else fight the flow
Go to Top of Page

sonu
Posting Yak Master

110 Posts

Posted - 02/08/2005 :  13:31:07  Show Profile
Nope, same error
Go to Top of Page

spirit1
Cybernetic Yak Master

Slovenia
11750 Posts

Posted - 02/08/2005 :  13:33:45  Show Profile  Visit spirit1's Homepage
how bout you give us the acctuall query?

Go with the flow & have fun! Else fight the flow
Go to Top of Page

sonu
Posting Yak Master

110 Posts

Posted - 02/08/2005 :  13:35:14  Show Profile
I cant show you the original query. Sorry :(
Go to Top of Page

spirit1
Cybernetic Yak Master

Slovenia
11750 Posts

Posted - 02/08/2005 :  13:45:06  Show Profile  Visit spirit1's Homepage
well then i guess we need jeff and his crystall ball...
another option is to inner join the original query to the subquery in which you somehow use "y" to your liking...

That's about all my ESP can sense...

Go with the flow & have fun! Else fight the flow
Go to Top of Page

Xerxes
Aged Yak Warrior

USA
665 Posts

Posted - 02/08/2005 :  13:56:43  Show Profile
Don't know why sonu can't show the query, but wouldn't

select
somefields
from
sometable
where
someconditions = someotherconditions
group by
x, y
Having
y = 'bla'


do the trick?



Semper fi,

Xerxes, USMC (Ret)
Go to Top of Page

spirit1
Cybernetic Yak Master

Slovenia
11750 Posts

Posted - 02/08/2005 :  13:58:17  Show Profile  Visit spirit1's Homepage
it would but he said that he doesn't want to group on y...


Go with the flow & have fun! Else fight the flow
Go to Top of Page

Seventhnight
Flowing Fount of Yak Knowledge

USA
2878 Posts

Posted - 02/08/2005 :  14:03:15  Show Profile  Visit Seventhnight's Homepage
Why wouldn't it be:


select 
	somefields
from 
	sometable
where
	someconditions = someotherconditions
	
	and y = 'bla'
	
group by
	x


Having is for adding conditions to aggregated values. For example:
count(*) > 1, sum(value)>1000, or the like...

Corey

"If the only tool you have is a hammer, the whole world looks like a nail." - Mark Twain
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 02/08/2005 :  14:04:27  Show Profile
First the HAVING Need to a scalar function, second if you want to include additional Columns, they need to be part of some scalar function as well

SELECT Col1, MAX(Col2), SUM(Col3)
FROM myTable99
GROUP BY Col1
HAVING COUNT(*) > 1

Or something like that.

Brett

8-)
Go to Top of Page
  Previous Topic Topic Next Topic  
 Forum Locked
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000