473,503 Members | 2,167 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Bitwise question -

I am new to bitwise thing in MSSQL.

Let's suppose there's a table of favorite foods

insert int fav_foods(food_name,bitwiseVal)
values('Pasta',1)

insert int fav_foods(food_name,bitwiseVal)
values('Chicken',2)

insert int fav_foods(food_name,bitwiseVal)
values('Beef',4)

insert int fav_foods(food_name,bitwiseVal)
values('Fish',8)

insert int fav_foods(food_name,bitwiseVal)
values('Pork',16)

How do I write query to find people who selected more than one item and
selected items from "Pasta, Chicken, Beef, Pork"(but not fish)?
I hope my question is not confusing.....

Jul 23 '05 #1
6 5894
nib
Bostonasian wrote:
I am new to bitwise thing in MSSQL.

Let's suppose there's a table of favorite foods

insert int fav_foods(food_name,bitwiseVal)
values('Pasta',1)

insert int fav_foods(food_name,bitwiseVal)
values('Chicken',2)

insert int fav_foods(food_name,bitwiseVal)
values('Beef',4)

insert int fav_foods(food_name,bitwiseVal)
values('Fish',8)

insert int fav_foods(food_name,bitwiseVal)
values('Pork',16)

How do I write query to find people who selected more than one item and
selected items from "Pasta, Chicken, Beef, Pork"(but not fish)?
I hope my question is not confusing.....


Your question isn't confusing but your design decision is. Why use
bitwise on something like this? If you were to use proper table design
this query would be trivial (and fast).

Zach
Jul 23 '05 #2
I tried to simply the example as much as possible, that's probably why
it didn't look that neccesary to build table like this.

I actually have survey data. Survey answer includes text, single select
multiple choice and multi-select multiple choice.

In answered data table, I currently have schema like following :

customer | question_id | answer
-----------------------------------------------
John | 1 | Pasta
John | 1 | Beef
John | 1 | Chicken
John | 1 | Pork

And I've got 2.4 million customers to manage, so I thought it'd save
some rows by using bitwise to reduce row numbers to one.

Jul 23 '05 #3
nib
Bostonasian wrote:
I tried to simply the example as much as possible, that's probably why
it didn't look that neccesary to build table like this.

I actually have survey data. Survey answer includes text, single select
multiple choice and multi-select multiple choice.

In answered data table, I currently have schema like following :

customer | question_id | answer
-----------------------------------------------
John | 1 | Pasta
John | 1 | Beef
John | 1 | Chicken
John | 1 | Pork

And I've got 2.4 million customers to manage, so I thought it'd save
some rows by using bitwise to reduce row numbers to one.


What you save in rows (i.e. disk space, which is cheap), you'll likely
lose in readability, mainainability, performance and standardization.
Search out one of Joe Celko's rants about thinking like a procedural
programmer and not a SQL/set based programmer because I think that's the
problem here.

Zach
Jul 23 '05 #4
> And I've got 2.4 million customers to manage, so I thought it'd save
some rows by using bitwise to reduce row numbers to one.


I'll bet that disk space is much cheaper than the cost of the time you'll
spend fixing up a kludge like that :-)

Try this:

CREATE TABLE Foods (customer_id INTEGER NOT NULL REFERENCES Customers
(customer_id), food INTEGER NOT NULL REFERENCES Foods (food), PRIMARY KEY
(customer_id, food))

SELECT customer_id
FROM Foods
WHERE food IN (1,2,3,4,5) /* Pasta,Chicken,Beef,Pork,Fish */
GROUP BY customer_id
HAVING COUNT(CASE WHEN food IN (1,2,3,4) THEN 1 END) = COUNT(*)
/* Everything except fish */

--
David Portas
SQL Server MVP
--
Jul 23 '05 #5
Bostonasian (ax****@gmail.com) writes:
I am new to bitwise thing in MSSQL.

Let's suppose there's a table of favorite foods

insert int fav_foods(food_name,bitwiseVal)
values('Pasta',1)

insert int fav_foods(food_name,bitwiseVal)
values('Chicken',2)

insert int fav_foods(food_name,bitwiseVal)
values('Beef',4)

insert int fav_foods(food_name,bitwiseVal)
values('Fish',8)

insert int fav_foods(food_name,bitwiseVal)
values('Pork',16)

How do I write query to find people who selected more than one item and
selected items from "Pasta, Chicken, Beef, Pork"(but not fish)?
I hope my question is not confusing.....


SELECT *
FROM tbl
WHERE fav_food & (SELECT SUM(bitwiseVal)
FROM fav_foods
WHERE food_name IN ('Pasta', 'Chicken', 'Beef', 'Pork'))

But as pointed out by others, this is a poor design. You may
save disk space, but if you need to find all that selected Chicken,
you will find that you cannot have an index on bit in an integer
column, so you get awful performance.

Look at David's query, and use that instead of the above.

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #6
Bostonasian wrote:

I tried to simply the example as much as possible, that's probably why
it didn't look that neccesary to build table like this.

I actually have survey data. Survey answer includes text, single select
multiple choice and multi-select multiple choice.

In answered data table, I currently have schema like following :

customer | question_id | answer
-----------------------------------------------
John | 1 | Pasta
John | 1 | Beef
John | 1 | Chicken
John | 1 | Pork

And I've got 2.4 million customers to manage, so I thought it'd save
some rows by using bitwise to reduce row numbers to one.


Hi Bostonasian,

There is no need to denormalize or use bitwise operations for this. IMO,
bitwise operations are not suitable for this problem.

The database does not have to grow very fast. If you normalize all the
way through, you would get a Customers (reference) table, a Questions
(reference) table and a Answers (reference) table. All these reference
tables can have short keys, which you use in your CustomerAnswers (data)
table. If you have fewer than 64000 customers, fewer than 256 questions
and fewer than 256 (fixed) answers per question, then each row in
CustomerAnswers would be just 2+1+1 = 4 bytes (excluding the free format
text answers).

Your schema could look something like this:
CREATE TABLE Customers(CustomerID smallint PRIMARY KEY, Name
nvarchar(100))
CREATE TABLE Questions(QuestionID tinyint PRIMARY KEY, Question
nvarchar(3000))
CREATE TABLE Answers (QuestionID tinyint, AnswerID tinyint, Answer
nvarchar(200),PRIMARY KEY (QuestionID,AnswerID))

CREATE TABLE CustomerAnswers
(CustomerID smallint REFERENCES Customers
,QuestionID tinyint REFERENCES Questions
,AnswerID tinyint
,TextAnswer nvarchar(2000)
,PRIMARY KEY (CustomerID,QuestionID,AnswerID)
,FOREIGN KEY (QuestionID,AnswerID) REFERENCES Answers
)

Hope this helps,
Gert-Jan
Jul 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
8966
by: jas_lx | last post by:
The basic understanding of what bitwise operators (& ^ | >> << ) comes fairly simple, as long as one has a fundamental understanding of bits, bytes and binary. Having done some Win32...
2
3449
by: Steve Summit | last post by:
-----BEGIN PGP SIGNED MESSAGE----- It's often explained that the reason for some of the imprecision in C's definition is so that C can be implemented on different kinds of machines -- say, those...
8
6923
by: Paul E Collins | last post by:
Suppose I have a few Keys objects: Keys k1 = Keys.V; // V Keys k2 = Keys.Control | Keys.V; // Ctrl+V Keys k3 = Keys.Shift | Keys.J; // Shift+J I need to determine which of these include the...
9
7019
by: Christopher Weaver | last post by:
I know that the bitwise AND of 8 and 4 will return 0 or false and the bitwise AND of 8 and 9 will return 1 or true but I don't know how to write the synax for it in C#. I have a value that ranges...
5
5764
by: noridotjabi | last post by:
I'm learning to program in C and any tutorial or book that I read likes to briefly touch on birdies operators and then move on without giving any sort of example application of them. Call me what...
2
2731
by: Mark Rae | last post by:
Hi, This isn't *specifically* an ASP.NET question, so I've also posted it in the ADO.NET group - however, it's not too far off-topic... Imagine a SQL Server 2005 database with a table with an...
3
2373
by: Jay Ruyle | last post by:
I'm trying to figure out a way to list several items in a listbox and let the user select any number of items in the listbox. I have tried to code in the items as bitwise items but all it stores...
5
3299
by: Gigs_ | last post by:
Can someone explain me bitwise expression? few examples for every expression will be nice x << y Left shift x >y Right shift x & y Bitwise AND x | y Bitwise OR x ^ y Bitwise XOR (exclusive...
45
5240
by: Carramba | last post by:
Hi! I now that I can't do straight forward any bitwise operation on float (double etc..). But I wondering what is the easiest/best way to do this? I was thinking if I have float x=1.1111 so I can...
29
5911
by: Carl Banks | last post by:
Anyone with me here? (I know the deadline for P3 PEPs has passed; this is just talk.) Not many people are bit-fiddling these days. One of the main uses of bit fields is flags, but that's not...
0
7093
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7353
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7011
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7468
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5596
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5023
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3170
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
747
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
401
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.