Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


multiple columns to single column (normalization)


multiple columns to single column (normalization)

Author
Message
memymasta
memymasta
SSC Journeyman
SSC Journeyman (99 reputation)SSC Journeyman (99 reputation)SSC Journeyman (99 reputation)SSC Journeyman (99 reputation)SSC Journeyman (99 reputation)SSC Journeyman (99 reputation)SSC Journeyman (99 reputation)SSC Journeyman (99 reputation)

Group: General Forum Members
Points: 99 Visits: 402
Hi, I'm working on normalizing a small part of our DB, but having trouble building a particular query that would enable to move data to an appropriate table.

Say, i have this table:

CREATE TABLE [#cars](
[id] [int] NOT NULL,
[part1] [int] NULL,
[part2] [int] NULL,
[part3] [int] NULL
) ON [PRIMARY]

GO

INSERT INTO [#cars] (id, [part1], [part2],[part3])
SELECT 1,1,1,1 UNION ALL
SELECT 2,0,0,0 UNION ALL
SELECT 3,0,1,0 UNION ALL
SELECT 4,1,0,0 UNION ALL
SELECT 5,0,0,1

SELECT * FROM #cars

DROP TABLE #cars



I want the select result to be:

carid | carpart
1 | 1
1 | 2
1 | 3
3 | 2
4 | 2
5 | 3

Thanks for your time.
memymasta
memymasta
SSC Journeyman
SSC Journeyman (99 reputation)SSC Journeyman (99 reputation)SSC Journeyman (99 reputation)SSC Journeyman (99 reputation)SSC Journeyman (99 reputation)SSC Journeyman (99 reputation)SSC Journeyman (99 reputation)SSC Journeyman (99 reputation)

Group: General Forum Members
Points: 99 Visits: 402
ah, solved it with lots of unions... sometimes it helps to write it in text...

ill post the query in a sec, if someone else needs it.

UPDATE: here's the query.


SELECT id as carid, '1' as carparts FROM #cars WHERE part1=1
UNION
SELECT id as carid, '2' as carparts FROM #cars WHERE part2=1
UNION
SELECT id as carid, '3' as carparts FROM #cars WHERE part3=1


Stewart "Arturius" Campbell
Stewart "Arturius" Campbell
SSCertifiable
SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)

Group: General Forum Members
Points: 5769 Visits: 7130
How about this:
CREATE TABLE [#cars](
[id] [int] NOT NULL,
[part1] [int] NULL,
[part2] [int] NULL,
[part3] [int] NULL
) ON [PRIMARY]

INSERT [#cars] (id, [part1], [part2],[part3])
VALUES(1,1,1,1),(2,0,0,0),(3,0,1,0),(4,1,0,0),(5,0,0,1)

SELECT * FROM #cars

SELECT id, part1 as Part, 1 as Seq
FROM #cars
WHERE part1 <>0
UNION ALL
SELECT id, part2 as Part, 2 as Seq
FROM #cars
WHERE part2 <>0
UNION ALL
SELECT id, part3 as Part, 3 as Seq
FROM #cars
WHERE part3 <>0

DROP TABLE #cars



____________________________________________
Space, the final frontier? not any more...
All limits henceforth are self-imposed.
“libera tute vulgaris ex”
Eugene Elutin
Eugene Elutin
Hall of Fame
Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)

Group: General Forum Members
Points: 3042 Visits: 5478
what about this:



select id, a.PartId
from #cars
cross apply (VALUES (1*[part1]), (2*[part2]), (3*[part3])) a(PartId)
where a.PartId > 0




_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
karthik M
karthik M
SSCrazy
SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)

Group: General Forum Members
Points: 2045 Visits: 2582
+1.

select id,1*part1 from #cars where part1 > 0
union
select id,2*part2 from #cars where part2 > 0
union
select id,3*part3 from #cars where part3 > 0



karthik
WolfgangE
WolfgangE
SSC Veteran
SSC Veteran (216 reputation)SSC Veteran (216 reputation)SSC Veteran (216 reputation)SSC Veteran (216 reputation)SSC Veteran (216 reputation)SSC Veteran (216 reputation)SSC Veteran (216 reputation)SSC Veteran (216 reputation)

Group: General Forum Members
Points: 216 Visits: 777
I don't think your table is normalized. Your have two entities, cars and parts. One car can have several parts, on part can be used in several cars. That's an m:n-relationship. This has to be realized by two 1:n-relationships.


CREATE TABLE #cars(
[id] [int] NOT NULL primary key( id )
) ON [PRIMARY]

create table #parts(
Id int not null
primary key (id )
)

create table #mnPartsCars(
CarId int not null,
PartId int not null
);

insert into #cars
SELECT 1 UNION ALL
SELECT 2 UNION ALL
SELECT 3 UNION ALL
SELECT 4 UNION ALL
SELECT 5

insert into #parts( Id ) values ( 1 ), ( 2 ), ( 3 );

insert into #mnPartsCars( CarId, PartId ) values
( 1, 1 ),
( 1, 2 ),
( 1, 3 ),
( 3, 2),
( 4, 1),
( 5, 3);

select
CarId = c.id,
PartId = p.id
from
#cars c inner join
#mnPartsCars mn on
mn.CarId = c.id inner join
#parts p on
p.Id = mn.PartId


drop table #cars
drop table #parts
drop table #mnPartsCars



There would be two foreign keys on the table #mnPartsCars, one referencing the #Cars-table, one referencing the #Parts-table.
memymasta
memymasta
SSC Journeyman
SSC Journeyman (99 reputation)SSC Journeyman (99 reputation)SSC Journeyman (99 reputation)SSC Journeyman (99 reputation)SSC Journeyman (99 reputation)SSC Journeyman (99 reputation)SSC Journeyman (99 reputation)SSC Journeyman (99 reputation)

Group: General Forum Members
Points: 99 Visits: 402
I don't think your table is normalized. Your have two entities, cars and parts. One car can have several parts, on part can be used in several cars. That's an m:n-relationship. This has to be realized by two 1:n-relationships.


You're right it wasn't normalized, that why i needed the query to do an insert into the new normalized table.
My new structure looks like the one you suggested.
WolfgangE
WolfgangE
SSC Veteran
SSC Veteran (216 reputation)SSC Veteran (216 reputation)SSC Veteran (216 reputation)SSC Veteran (216 reputation)SSC Veteran (216 reputation)SSC Veteran (216 reputation)SSC Veteran (216 reputation)SSC Veteran (216 reputation)

Group: General Forum Members
Points: 216 Visits: 777
ahaaaaaaa :-) now I understood
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search