Welcome to dbFreaks.com!
FAQFAQ      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Table with a lot of properties columns

 
   Database Help (Home) -> Datamining RSS
Next:  Search Multiple keywords across multiple fields  
Author Message
Utf-8BSm9obiBILg

External


Since: Feb 04, 2004
Posts: 2



(Msg. 1) Posted: Wed Feb 04, 2004 6:16 pm
Post subject: Table with a lot of properties columns
Archived from groups: microsoft>public>sqlserver>datamining (more info?)

Hi everybody,
can anyone suggest me a better sollution to the problem below if any exists?

Let's have a table of CARS. Each car can have a lot of properties - electrical windows, automatic transmission, leather, ... almost 50 of them. Each property can contain true/false value.
I want to search for all cars that have for example 5 properties set to true. Is there a better way than having one column per one property? An array of bits...???

Thanks,
J.

 >> Stay informed about: Table with a lot of properties columns 
Back to top
Login to vote
Anith Sen

External


Since: Feb 17, 2004
Posts: 310



(Msg. 2) Posted: Thu Feb 05, 2004 9:47 pm
Post subject: Re: Table with a lot of properties columns [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Since these are just boolean properties, a good approach is to represent
them as one column in your table. This wouls also allow for adding any
additional properties without altering the schema. One sample DDL can look
like:

CREATE TABLE Cars (
Car_Identifier INT NOT NULL PRIMARY KEY,
VIN... );

CREATE TABLE Properties (
Property_id INT NOT NULL PRIMARY KEY,
Description VARCHAR(40) NOT NULL ) ;

CREATE TABLE CarProperties (
Car_identifier INT NOT NULL
REFERENCES Cars (Car_identifier)
Property_id INT NOT NULL
REFERENCES Properties (Property_id)
PRIMARY KEY (Car_identifier, Property_id ));

Now you can simply write a SELECT statement joining the CarProperties table
to get the results you want.

--
Anith

 >> Stay informed about: Table with a lot of properties columns 
Back to top
Login to vote
Utf-8BSm9obi4gSC4

External


Since: Feb 06, 2004
Posts: 1



(Msg. 3) Posted: Fri Feb 06, 2004 4:51 am
Post subject: Re: Table with a lot of properties columns [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Thank you, an easy idea. I was too oriented on current schema.
J.
 >> Stay informed about: Table with a lot of properties columns 
Back to top
Login to vote
Utf-8BSm9obiBILg

External


Since: Feb 04, 2004
Posts: 2



(Msg. 4) Posted: Wed Feb 11, 2004 3:26 am
Post subject: Re: Table with a lot of properties columns [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Hello again,

it's not so easy as it looks in case I need to show a list of cars with 0/1 values of all properties.

car pro1 prop2 prop3
--- ------ ------- -------
Skoda 0 1 1

Any idea how to get such result from the previous concept?

Thanks,
John
 >> Stay informed about: Table with a lot of properties columns 
Back to top
Login to vote
Bern Taylor

External


Since: Apr 18, 2004
Posts: 8



(Msg. 5) Posted: Wed Feb 11, 2004 4:33 pm
Post subject: Re: Table with a lot of properties columns [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Have a look at fn_replinttobitstring and adjust it slightly into a where
clause?

Bern


"John H." <anonymous.RemoveThis@discussions.microsoft.com> wrote in message
news:01C4D01B-3245-4134-A675-B98222FD1642@microsoft.com...
 > Hi everybody,
 > can anyone suggest me a better sollution to the problem below if any
exists?
 >
 > Let's have a table of CARS. Each car can have a lot of properties -
electrical windows, automatic transmission, leather, ... almost 50 of them.
Each property can contain true/false value.
 > I want to search for all cars that have for example 5 properties set to
true. Is there a better way than having one column per one property? An
array of bits...???
 >
 > Thanks,
 > J.<!-- ~MESSAGE_AFTER~ -->
 >> Stay informed about: Table with a lot of properties columns 
Back to top
Login to vote
Bern Taylor

External


Since: Apr 18, 2004
Posts: 8



(Msg. 6) Posted: Wed Feb 11, 2004 5:18 pm
Post subject: Re: Table with a lot of properties columns [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Equivalent of

create function gnum (@num int)
returns int
as
begin
declare @i int
declare @j int
set @j=0
set @i=1
while @i<1073741823
begin
if not (@i & @num)=0
set @j=@j+1
set @i=@i*2
end
return @j
end

select * from testcars where mydb.dbo.gnum(props)=5

Alternatively, you could create and populate a table with the options that
totalled 5 and do a join!

Bern

 > Have a look at fn_replinttobitstring and adjust it slightly into a where<!-- ~MESSAGE_AFTER~ -->
 >> Stay informed about: Table with a lot of properties columns 
Back to top
Login to vote
Anith Sen

External


Since: Feb 17, 2004
Posts: 310



(Msg. 7) Posted: Wed Feb 11, 2004 7:31 pm
Post subject: Re: Table with a lot of properties columns [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

You are looking for a cross-tab representation of the data. Generally, since
this is not a data management task and more presentation oriented, it is
recommended that you leverage the capabilities of your client application to
handle it. If you must do it in your SELECT statement, then you can refer to
the following links which illustrates how to generate cross tabulations with
SQL.
http://support.microsoft.com/?scid=kb;EN-US;q175574

--
Anith
 >> Stay informed about: Table with a lot of properties columns 
Back to top
Login to vote
Display posts from previous:   
   Database Help (Home) -> Datamining All times are: Pacific Time (US & Canada) (change)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]