| Author |
Topic |
|
jimoomba
Yak Posting Veteran
90 Posts |
Posted - 2011-09-29 : 22:43:05
|
| Hi All,I need a sql query to sum the mask values :Ex: 1. sum of 4 and 15 is 15 as 4 is already in 152, Sum of 1 and 3 is 33. Sum of 1 and 2 is 3.kindly let me know the sql query for this.Thanks,Ramsrams |
|
|
vmvadivel
Yak Posting Veteran
69 Posts |
Posted - 2011-09-29 : 23:07:09
|
| What would be the SUM of 20 and 5. As per your logic again the result should be 20 as 5 is already in 20. Doesn't that mean you always want the MAX of the two numbers?Best RegardsVadivelhttp://vadivel.blogspot.com |
 |
|
|
jimoomba
Yak Posting Veteran
90 Posts |
Posted - 2011-09-30 : 02:16:37
|
| HI Vadivel,I want to know how do we use bitmasks first and how to do sum of bitmasking.could you provide me some good examples on the same.Thanks,Ramrams |
 |
|
|
vmvadivel
Yak Posting Veteran
69 Posts |
Posted - 2011-09-30 : 03:34:20
|
| May be you are looking for this then:DECLARE @tblTestBit TABLE (iValue INT) INSERT @tblTestBit SELECT 4 UNION SELECT 15DECLARE @counter INT SET @counter = 0 SELECT @counter = @counter | iValue FROM @tblTestBit --ResultSELECT @counter as [Result]Best RegardsVadivelhttp://vadivel.blogspot.com |
 |
|
|
jimoomba
Yak Posting Veteran
90 Posts |
Posted - 2011-09-30 : 05:04:08
|
| Hi Vadivel,Iam having the following table and values :create table BitmaskDemo(ID int Primary key,Supermarket varchar(10),bitmask varbinary(1000))/*values1 = Banana 000000012 = Strawberries 000000104 = Oranges 000001008 = Apples 0000100016 = plums 0001000032 = pineapple 00100000*/Insert into Bitmaskdemovalues (1,'Tesco',7) -- Bananas,strawberries & oranges 00000111Insert into Bitmaskdemovalues (2,'Sainsburys',31) -- Bananas, strawberries, oranges, apples & plums 00011111Insert into Bitmaskdemovalues (3,'Morrissons',8) -- Apples 00001000Insert into Bitmaskdemovalues (4,'Waitrose',24) -- Apples and plums 00011000Insert into BitmaskDemovalues (5,'Aldi',25) -- Bananas, Apples and Plums 00011001Insert into BitmaskDemovalues (6,'Somerfield',9) -- Bananas & Apples 00001001And i want the sql query to get sum of bitmasks as follows :sum of 4 and 15 is 15 ,Sum of 1 and 3 is 3 ,and Sum of 1 and 2 is 3rams |
 |
|
|
vmvadivel
Yak Posting Veteran
69 Posts |
Posted - 2011-09-30 : 05:20:22
|
| I dont understand from where does 15 came because it doesn't seem to be there in your INSERT script.I think the logic which i have in my previous post would give you the result which you are asking for. I have used INT as the column datatype. In your example you seems to have been using VARBINARY as your datatype. Then you got to convert it into INT and still use the same sample.DECLARE @tblTestBit TABLE ( iValue varbinary(1000)) INSERT @tblTestBit SELECT 1 UNION SELECT 3DECLARE @counter INT SET @counter = 0 SELECT @counter = @counter | CONVERT(INT,iValue) FROM @tblTestBit --ResultSELECT @counter as [Result]Best RegardsVadivelhttp://vadivel.blogspot.com |
 |
|
|
jimoomba
Yak Posting Veteran
90 Posts |
Posted - 2011-09-30 : 05:30:40
|
| Hi Vadivel,Thanks for your immediate response. Could you please implement your code for my example and show me the output please. That would be a great help for me.Thanks,Ramrams |
 |
|
|
vmvadivel
Yak Posting Veteran
69 Posts |
Posted - 2011-09-30 : 05:35:41
|
| You are inserting some 6 records into the table. Can you pls explain based on what condition you want to add those records 1 & 2 OR 1 & 3?Best RegardsVadivelhttp://vadivel.blogspot.com |
 |
|
|
jimoomba
Yak Posting Veteran
90 Posts |
Posted - 2011-09-30 : 05:39:59
|
| Hi Vadivel,I have used numbers in the power of 2 in this example. This ensures that every value gets assigned a unique number and that the sum of these numbers will always generate a unique combination. So bananas and oranges = 5. rams |
 |
|
|
vmvadivel
Yak Posting Veteran
69 Posts |
Posted - 2011-09-30 : 05:40:15
|
| Here you go, I am just adding the rows whose ID is 1 or 3.DECLARE @counter INT SET @counter = 0 /*SELECT @counter = @counter | CONVERT(INT, bitmask) FROM Bitmaskdemo WHERE ID in (1,3)*/SELECT @counter = @counter | CONVERT(INT, bitmask) FROM Bitmaskdemo WHERE ID = 1 or ID = 3--ResultsSELECT @Counter as [Results]Best RegardsVadivelhttp://vadivel.blogspot.com |
 |
|
|
jimoomba
Yak Posting Veteran
90 Posts |
Posted - 2011-09-30 : 05:48:23
|
| Hi Vadivel,I will be selecting the table based on bitmask values :eg: select * from Bitmaskdemo where Bitmask & 17 = 17in the same way i need for SUM of bitmasks if 1 & 2 as 3..etcThanks,Ramrams |
 |
|
|
jimoomba
Yak Posting Veteran
90 Posts |
Posted - 2011-09-30 : 06:02:04
|
| it is fine if you provide me the code without using bit operators alsorams |
 |
|
|
vmvadivel
Yak Posting Veteran
69 Posts |
Posted - 2011-09-30 : 06:10:09
|
| Not sure what exactly you are trying to achieve! May be you need to write the question more clearly on what exactly you want. The below query is my last try. If this isn't want you want you have to write your question as clearly as possible with sample data and tell exactly what you want to achieve from your sample data.SELECT * FROM Bitmaskdemo WHERE CONVERT(INT, bitmask) | 8 = 15Best RegardsVadivelhttp://vadivel.blogspot.com |
 |
|
|
jimoomba
Yak Posting Veteran
90 Posts |
Posted - 2011-09-30 : 06:28:35
|
| HI Vadivel,Sorry if had troubled you much. Here is the detailed question :As shown From the table Bitmaskdemo we are having bitmaskcolumn as well.Now the clear requirement of mine is I need to SUM the mask values and make the output as :1. SUM of 1 & 3 gives 32. SUM of 1 & 2 gives 3.The example which u have given is giving the exact ouput as shown below:DECLARE @tblTestBit TABLE (iValue varbinary(1000)) INSERT @tblTestBit SELECT 1 UNION SELECT 3DECLARE @counter INT SET @counter = 0 SELECT @counter = @counter | CONVERT(INT,iValue) FROM @tblTestBit --ResultSELECT @counter as [Result] I wanted to know how this to be implemented in the real code..sql query:)rams |
 |
|
|
jimoomba
Yak Posting Veteran
90 Posts |
Posted - 2011-09-30 : 06:33:58
|
| Hi Vadivel,It would be clear for me if you coulld create a samole table with multiple columns and give one good example :)I feel really motivated by the way u are guiding me. Very thankful to u for ur efforts. Iam new to this field..Please dont mind if i irriitate u with repeated questions or so...Thanks a lot :)rams |
 |
|
|
|