Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 can I "de-normalize" to get one row per Person?

Author  Topic 

steelkilt
Constraint Violating Yak Guru

255 Posts

Posted - 2004-05-10 : 16:25:05
Anyone know of a quick way to "de-normalize" the data below:

PersonID/Item

1/1
1/2
1/5
2/3
2/8
3/9
3/10
3/1
3/2


into a one-record per row result set like this, where number of columns will "grow" as necessary to the max number of cols needed based on the Person with the most Items:


PersonID/Item1/Item2/Item3/Item4

1/1/2/5
2/3/8
3/9/10/1/2


I have no unique ID-per-record I can key on, i.e. the situation is as you see it.

Thanks.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2004-05-10 : 16:28:11
Why do you want to do this? What is the maximum number of Items that a person can have? If there isn't a maximum, how do you know how many columns you need in the table definition? You wouldn't want to change the table each time someone reaches the limit.

Or do you just want the result set denormalized and not the actual table?

Tara
Go to Top of Page

MichaelP
Jedi Yak

2489 Posts

Posted - 2004-05-10 : 16:53:56
This might do it, or point you in the right direction.
I'm with Tara on this one, this is a kinda weird result set that could cause problems down the road.

http://www.sqlteam.com/item.asp?ItemID=2955

Michael

<Yoda>Use the Search page you must. Find the answer you will.</Yoda>
Go to Top of Page

SMerrill
Posting Yak Master

206 Posts

Posted - 2004-05-11 : 13:30:32
This sounds like a job for the AWK scripting language. I would post it on the Tek-Tips forum.

~ Shaun Merrill
Seattle, WA
Go to Top of Page

steelkilt
Constraint Violating Yak Guru

255 Posts

Posted - 2004-05-11 : 13:53:40
Tara,

sorry if I wasn't clear. Yes, I want to de-normalize the result set, not the base table.

I mis-spoke earlier: there are 62 possible unique item selections per person, but in reality one wouldn't expect a person to have more than 10 items.

What I'm trying to do here is to get this result set de-normalized in a VIEW, then query this VIEW as part of a larger query that will return one record per person.

thx
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2004-05-11 : 14:18:33
You'll have to create a loop, find the MAX User Id and work backwards doing SQL Inserts into a table and setting a varianble

SELECT @x = @x + Item FROM TABLE WHERE Person_Id = @PersonId

INSERT INTO newTable(PersonId, ItemId)
SELECT @PersonId, @x

something like that....



Brett

8-)
Go to Top of Page

Page47
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2004-05-11 : 14:27:35
[url="http://www.sqlteam.com/item.asp?ItemID=11021"] Converting Multiple Rows into a CSV String (Set Based Method)
[/url]

Jay White
{0}
Go to Top of Page

steelkilt
Constraint Violating Yak Guru

255 Posts

Posted - 2004-05-11 : 15:33:46
Jay,

that is one cool script! thx!!
Go to Top of Page
   

- Advertisement -