We help IT Professionals succeed at work.

Is there a SELECT & GROUP BY rule workaround in T-SQL

cwest1
cwest1 asked
on
195 Views
Last Modified: 2010-03-19
If a column name is included in a SELECT, afaik the same column name must also be included in a GROUP BY, unless a function is applied to it. For example, MAX(column name) AS "something" will obviate the "rule." If I don’t want a particular column name in a GROUP BY because it causes rows to be included in the result that I don’t want, how do I workaround this rule? I'm using MSSQL 2005 (and new to SQL semantics).
Comment
Watch Question

Author

Commented:
I found a solution—filter through yet another virtual table. Still appreciate any smarter suggestions though.

Commented:
Hi cwest1

Can you explain what you mean by this in more detail? If you don't group by the field then you may get many different values returned for the field that need to be combined into a single value (for the other group by fields) before being returned to you. In order for SQL to do this is needs to know whether you want to sum/max/min/etc.

Author

Commented:
I think we're on the same page. If there's a function that can be applied to an nvarchar type column so that the column name could be omitted from GROUP BY, I wouldn't have to filter the output through any more conditions to obtain my result. Conceptually,  doNothing(columnName)AS "somename" would allow the columnName to be omitted from GROUP BY. I'm basing my logic on the effect that max/min/etc has on GROUP BY.
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
I wasn't considering what you've explained, but you've changed my approach. I was grouping on max(col1) and the rows for the other columns were correct by default. However, I wanted other columns in the SELECT, but (also) including them in a GROUP BY added back rows I didn't want. So my workaround was to continue filtering, thinking that there was an easier way, which raised my question. It seems that SQL processes statements from the result end back to the start, or something like that. Not procedurally. I think that's what I didn't grasp.

Thanks for the help.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.