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 2005 Forums
 Transact-SQL (2005)
 Related records in tables

Author  Topic 

smh6n
Starting Member

3 Posts

Posted - 2008-08-20 : 15:25:49
I have two tables, one containing people and a second table, used when applicable, that relates records in the first table.

Table One - Entity
EntityID LastName FirstName
1 Jones Sam
2 Jones Susan
3 Smith Jane
4 Thomas Fran
5 Thomas Phil
6 Doe Junior

Table Two - Link
LinkID EntityID1 EntityID2 Relationship1 Relationshiop2
1 1 2 Husband Wife
2 4 5 Wife Husband
3 4 6 Mother Son

I am trying to get a count of family units which in the example above would be
1 Sam & Susan Jones
1 Jane Smith
1 Fran, Phil Thomas and Junior Doe
---
3 Total

All I can think to do is use cursors which I've never used before or create a looping dts package. Any ideas are appreciated.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-08-20 : 15:34:10
Can the table design be changed, specifically "Table Two - Link"?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

smh6n
Starting Member

3 Posts

Posted - 2008-08-20 : 16:14:42
DB is in production so it would be difficult to change the table design. What did you have in mind?
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-08-20 : 16:16:51
The way that I see it is you'd break it (Table Two - Link) out to two tables. The first table would contain every possible relationship type. The second table would contain the links between your entities and my new table. This way you could do simple joins to get the data returned.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -