Unix Technical Forum

Count of Columns <> 0

This is a discussion on Count of Columns <> 0 within the SQL Server forums, part of the Microsoft SQL Server category; --> How would you count the number of columns with a value not equal to 0 for each row in ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-29-2008, 02:21 AM
Joe User
 
Posts: n/a
Default Count of Columns <> 0

How would you count the number of columns with a value not equal to 0 for
each row in a table?

Thanks!

Joe


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-29-2008, 02:22 AM
Simon Hayes
 
Posts: n/a
Default Re: Count of Columns <> 0


"Joe User" <joe@user.com> wrote in message
news:c4s5vp$6je$1@tribune.mayo.edu...
> How would you count the number of columns with a value not equal to 0 for
> each row in a table?
>
> Thanks!
>
> Joe
>
>


Here's one way:

select PrimaryKeyColumn,
case when col1 = 0 then 0 else 1 end +
case when col2 = 0 then 0 else 1 end +
case when col3 = 0 then 0 else 1 end +
...
case when coln = 0 then 0 else 1 end as 'NonZeroColumns'
from
dbo.MyTable


Simon


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-29-2008, 02:22 AM
Joe User
 
Posts: n/a
Default Re: Count of Columns <> 0

Excellent!

Thanks!

Next question.....
How does someone relatively new to tsql learn this sort of thing?

TIA



"Simon Hayes" <sql@hayes.ch> wrote in message
news:4071a1c0$1_3@news.bluewin.ch...
>
> "Joe User" <joe@user.com> wrote in message
> news:c4s5vp$6je$1@tribune.mayo.edu...
> > How would you count the number of columns with a value not equal to 0

for
> > each row in a table?
> >
> > Thanks!
> >
> > Joe
> >
> >

>
> Here's one way:
>
> select PrimaryKeyColumn,
> case when col1 = 0 then 0 else 1 end +
> case when col2 = 0 then 0 else 1 end +
> case when col3 = 0 then 0 else 1 end +
> ...
> case when coln = 0 then 0 else 1 end as 'NonZeroColumns'
> from
> dbo.MyTable
>
>
> Simon
>
>



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-29-2008, 02:22 AM
Simon Hayes
 
Posts: n/a
Default Re: Count of Columns <> 0


"Joe User" <joe@user.com> wrote in message
news:c4sa4g$c6p$1@tribune.mayo.edu...
> Excellent!
>
> Thanks!
>
> Next question.....
> How does someone relatively new to tsql learn this sort of thing?
>
> TIA
>
>


<snip>

Get a good book or two - there are some suggestions here:

http://vyaskn.tripod.com/sqlbooks.htm

But don't forget Books Online itself - it's very helpful to read through the
TSQL reference part. I don't mean read every word (unless you have a lot of
time on your hands...), but it helps to have an idea of what's available in
the language. Even if you only vaguely remember what a keyword does, or if
you only remember the name, you can always look it up. The list of functions
is another useful page to review, for the same reason. The
SELECT/INSERT/UPDATE/DELETE entries are very important, as are the CREATE
XXXX entries - all of them are linked to lots of related information, so you
can go into as much detail as you want.

Simon


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-29-2008, 02:22 AM
--CELKO--
 
Posts: n/a
Default Re: Count of Columns <> 0

>> How would you count the number of columns with a value not equal to
0 for each row in a table? <<

SELECT keycol,
ABS(SIGN(col1)) +
ABS(SIGN(col2)) +
ABS(SIGN(col3)) + .. AS non_zero_tally
FROM Foobar;
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 12:45 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com