sql server - Order SQL query by the sum of specefic columns -



sql server - Order SQL query by the sum of specefic columns -

here extract big table (sql server 2005) i'm querying against:

id (primary key) | business relationship | phone | employee | address ------------------------------------------------------------------ 1 | 123 | y | y | n 2 | 456 | n | n | n 3 | 789 | y | y | y

i need homecoming rows have @ to the lowest degree 1 y in phone, employee, or address (there 10 others not shown here). need order results number of y's have in of three.

i've tried getting "tagtotal" this:

select sum( case when [phone] = 'y' 1 else 0 end + case when [employee] = 'y' 1 else 0 end + case when [address] = 'y' 1 else 0 end ) table grouping id

this returns:

tagtotal --------------- 2 0 3

i'm @ loss on how combine existing giant query , order without adding each column grouping @ end.

since sum of values you're after on same row, don't need aggregrate results, thereby eliminating need grouping by..

select case when [phone] = 'y' 1 else 0 end + case when [employee] = 'y' 1 else 0 end + case when [address] = 'y' 1 else 0 end total table

sql sql-server sql-server-2005

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -