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.
| Author |
Topic |
|
michael.appleton
Posting Yak Master
160 Posts |
Posted - 2008-08-01 : 05:37:58
|
| I'm wondering if anyone can help me with this problem am I'm sure it is very common. I'm trying to group objects that are unique, which would be easy if their attributes were only on one row, but the problem is they are more than one row.For example, say I have a table of people and a table of cars and they are joined by who owns what car. This means one person can own several cars. For the sake of this example lets say a car can only have one owner. So the two tables look like this:People:PersonId (pk)| Name | Earnings | Other attributes...Cars:CarId (pk) | PersonId (fk) | CarModel (fk) | Other attributes...Now my problem is what if I want to find out how many people own the same CarModels? So take two people:PersonId: 1 Cars Owned (Car Id):1,2Car 1 Model Id:1Car 2 Model Id:4PersonId: 2 Cars Owned (Car Id):3,4Car 1 Model Id:1Car 2 Model Id:4So I would want this two people to be associated as they have the same model cars and other other people with the same models. As an out put I would want a count of the number of people with the same model cars or perhaps even another aggrigate function, say an average of their earnings which would be an attribute in the people table.Thanks for the help!Mike |
|
|
michael.appleton
Posting Yak Master
160 Posts |
Posted - 2008-08-01 : 05:54:35
|
| Just to clarify, take these six people and the car models they own:person1:beetle,jeepperson2:beetle,jeepperson3:beetleperson4:jeepperson5:beetle, jeep,jagperson6:beetle, jeep,jagId want the resulting groups that I could use aggrigate functions on to be:Group1:Person1,Person2Group2:Person3Group3:Person4Group4:Person5,Person6 |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-08-01 : 06:09:32
|
| http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=81254MadhivananFailing to plan is Planning to fail |
 |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2008-08-01 : 06:43:03
|
| [code]--sample datadeclare @Cars table (CarId int identity(1, 1), PersonId int, CarModelId int)insert @Cars select 1, 1union all select 1, 4union all select 2, 1union all select 2, 4union all select 3, 1union all select 4, 1union all select 4, 4union all select 4, 5union all select 5, 1union all select 6, 1union all select 7, 4union all select 7, 5union all select 8, 1union all select 8, 4--calculation; with a as (select *, substring((select ',' + cast(CarModelId as varchar(10)) from @Cars where PersonId = a.PersonId for xml path('')), 2, 100) as CarModelIds from @Cars a), b as (select CarModelIds, substring((select distinct ',' + cast(PersonId as varchar(10)) from a where CarModelIds = b.CarModelIds for xml path('')), 2, 100) as PersonIds from a b group by CarModelIds)select row_number() over (order by PersonIds) as [Group], * from b/* resultsGroup CarModelIds PersonIds-------- ------------- ------------1 1,4 1,2,82 1 3,5,63 1,4,5 44 4,5 7*/[/code]Ryan Randall Solutions are easy. Understanding the problem, now, that's the hard part. |
 |
|
|
michael.appleton
Posting Yak Master
160 Posts |
Posted - 2008-08-01 : 06:50:04
|
| Cheers, think I get that! Thanks a lot |
 |
|
|
|
|
|
|
|