PC Review


Reply
Thread Tools Rate Thread

How can I OR all the values in a particular column across multiple rows?

 
 
Ken Varn
Guest
Posts: n/a
 
      11th Feb 2005
Is there any way that I can run an ADO.NET query in SQLServer to do a
bitwise OR operation on a particular column for multiple rows?

i.e., Select OR(ColumnName) from Table

--
-----------------------------------
Ken Varn
Senior Software Engineer
Diebold Inc.

EmailID = varnk
Domain = Diebold.com
-----------------------------------


 
Reply With Quote
 
 
 
 
=?Utf-8?B?Sm9uIE1leWVy?=
Guest
Posts: n/a
 
      12th Feb 2005
Ken:

Here is a Where clause that I use to select multiple rows based on a mask
(int value) passed in:

WHERE (@pRecStatusMask & RecStatus)>0

So, if the mask has a value of 7, any rows with a RecStatus of 1,2,3,4,5,6
or 7 will get selected.

I believe this is what you needed?

"Ken Varn" wrote:

> Is there any way that I can run an ADO.NET query in SQLServer to do a
> bitwise OR operation on a particular column for multiple rows?
>
> i.e., Select OR(ColumnName) from Table
>
> --
> -----------------------------------
> Ken Varn
> Senior Software Engineer
> Diebold Inc.
>
> EmailID = varnk
> Domain = Diebold.com
> -----------------------------------
>
>
>

 
Reply With Quote
 
Frans Bouma [C# MVP]
Guest
Posts: n/a
 
      12th Feb 2005
Jon Meyer wrote:
> Ken:
>
> Here is a Where clause that I use to select multiple rows based on a mask
> (int value) passed in:
>
> WHERE (@pRecStatusMask & RecStatus)>0
>
> So, if the mask has a value of 7, any rows with a RecStatus of 1,2,3,4,5,6
> or 7 will get selected.
>
> I believe this is what you needed?


No I think he needs the value of 7 to be returned by the select if
there are 3 records, with value 1, 2 and 4. I toyed with the idea of
suggesting a query which uses an aggregate (SUM()) but I thought that
that isn't the answer, as the values might not be just flags but could
have more bits set, which then would result in bad values.

So I think it can't be done in 1 query, but perhaps some sql wiz knows
how to solve it

FB

>
> "Ken Varn" wrote:
>
>
>>Is there any way that I can run an ADO.NET query in SQLServer to do a
>>bitwise OR operation on a particular column for multiple rows?
>>
>>i.e., Select OR(ColumnName) from Table
>>



--
------------------------------------------------------------------------
Get LLBLGen Pro, productive O/R mapping for .NET: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
 
Reply With Quote
 
=?Utf-8?B?Sm9uIE1leWVy?=
Guest
Posts: n/a
 
      13th Feb 2005
Yes, Frans you're right. I overlooked his example (i.e., Select
OR(ColumnName) from Table). However, I don't see the value of a function
like this, particularly on integer type columns. Assuming a column type of
tinyint that can have random values of 0 to 255 and some large number of
rows, this function will tend to always return 255.
I guess I'm still missing what Ken is asking.

"Frans Bouma [C# MVP]" wrote:

> Jon Meyer wrote:
> > Ken:
> >
> > Here is a Where clause that I use to select multiple rows based on a mask
> > (int value) passed in:
> >
> > WHERE (@pRecStatusMask & RecStatus)>0
> >
> > So, if the mask has a value of 7, any rows with a RecStatus of 1,2,3,4,5,6
> > or 7 will get selected.
> >
> > I believe this is what you needed?

>
> No I think he needs the value of 7 to be returned by the select if
> there are 3 records, with value 1, 2 and 4. I toyed with the idea of
> suggesting a query which uses an aggregate (SUM()) but I thought that
> that isn't the answer, as the values might not be just flags but could
> have more bits set, which then would result in bad values.
>
> So I think it can't be done in 1 query, but perhaps some sql wiz knows
> how to solve it
>
> FB
>
> >
> > "Ken Varn" wrote:
> >
> >
> >>Is there any way that I can run an ADO.NET query in SQLServer to do a
> >>bitwise OR operation on a particular column for multiple rows?
> >>
> >>i.e., Select OR(ColumnName) from Table
> >>

>
>
> --
> ------------------------------------------------------------------------
> Get LLBLGen Pro, productive O/R mapping for .NET: http://www.llblgen.com
> My .NET blog: http://weblogs.asp.net/fbouma
> Microsoft MVP (C#)
> ------------------------------------------------------------------------
>

 
Reply With Quote
 
Adrian Parker
Guest
Posts: n/a
 
      14th Feb 2005
Ken,

As long as the column you're checking is an integer, you can do the
following..

Select * From <Table> Where <ColumnName> | <MaskValue> = <MaskValue>

e.g. if you have 8 rows with numbers 1 thru 8, then a mask of 7 would return
rows 1 to 7, but not 8.

HTH
Adrian Parker


"Ken Varn" <nospam> wrote in message
news:(E-Mail Removed)...
> Is there any way that I can run an ADO.NET query in SQLServer to do a
> bitwise OR operation on a particular column for multiple rows?
>
> i.e., Select OR(ColumnName) from Table
>
> --
> -----------------------------------
> Ken Varn
> Senior Software Engineer
> Diebold Inc.
>
> EmailID = varnk
> Domain = Diebold.com
> -----------------------------------
>
>



 
Reply With Quote
 
Ken Varn
Guest
Posts: n/a
 
      14th Feb 2005
> No I think he needs the value of 7 to be returned by the select if
> there are 3 records, with value 1, 2 and 4. I toyed with the idea of
> suggesting a query which uses an aggregate (SUM()) but I thought that
> that isn't the answer, as the values might not be just flags but could
> have more bits set, which then would result in bad values.
>
> So I think it can't be done in 1 query, but perhaps some sql wiz knows
> how to solve it
>


Your are right on what I am looking for. I want an equivalent SUM operation
but doing a bitwise OR instead.

I was directed to this query that seems to do what I am looking for. Seems
like a lot of work. I wish there was just a simple keyword like SUM to do
it, but the results are what I want. Not sure how good this is when it
comes to performance.

SELECT SUM(Distinct Value & 1) + SUM(Distinct Value & 2) + SUM(Distinct
Value & 4) + SUM(Distinct Value & 8) -- Take this out to bits neeeed

FROM tbl


--
-----------------------------------
Ken Varn
Senior Software Engineer
Diebold Inc.

EmailID = varnk
Domain = Diebold.com
-----------------------------------


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
retrieving multiple corresponding values with variable rows/column soph Microsoft Excel Worksheet Functions 4 15th Sep 2009 10:44 AM
Splitting multiple values of a column in to new rows while importing data svanga Microsoft Access External Data 9 30th Aug 2006 08:31 PM
Putting column values in multiple rows =?Utf-8?B?Q2hCb29kdHM=?= Microsoft Excel Programming 1 18th Jul 2005 08:50 PM
Formula to compare multiple rows values based on another column? =?Utf-8?B?TXVycGg=?= Microsoft Excel Worksheet Functions 4 21st Feb 2005 02:44 AM
Concatenating 2 column values into 1 column value for multiple rows Anders S Microsoft Excel New Users 4 14th Apr 2004 02:30 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:18 PM.