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
 General SQL Server Forums
 New to SQL Server Programming
 Group by nvarchar column

Author  Topic 

franman
Starting Member

3 Posts

Posted - 2011-10-05 : 12:25:58
Hi to everyone,

Compliments for the website very useful and powerful
I have a question, I hope someone can help me, I'll try to be as much clear as I can.

I need to use clausole GROUP BY one thing that I know when I use it I have to select the same column.
So is it possible to say ok for the aggregate rows I want select a custom value I mean a constant 'MORE'.
I try to explain in another way to be much clear

I have this table

A B C D
_ _ _ ___
1 2 3 ita
1 2 3 fra
1 2 3 eng
4 4 4 fra

I want obtain this

A B C D
_ _ _ ___
1 2 3 MORE
4 4 4 fra

But in the follow method doesn't work because all rows will have 'MORE'

select A, B, C, 'MORE' as D from Table
where ...
GROUP BY A, B, C, D

Please can you suggest me something I still looking for help, I tried to serch in your forum and on google as well but nothing.

Thank you very much in advance.






Francesco

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-10-05 : 12:29:38
[code]select A, B, C,CASE WHEN COUNT(*) > 1 THEN 'MORE' ELSE MAX(D) END as D from Table
where ...
GROUP BY A, B, C
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

franman
Starting Member

3 Posts

Posted - 2011-10-05 : 12:35:22
Wow I never used something like that,
Thank you very much I am going to try straight away!


Francesco
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-10-05 : 12:42:00
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

franman
Starting Member

3 Posts

Posted - 2011-10-06 : 06:34:00
I want to say to everyone that it works perfectly!

Thousands of Thanks :-)

Francesco
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-10-06 : 08:41:59


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -