Question

I'm trying to 2nf some data:

Refid | Reason
------|---------
1     | Admission
1     | Advice and Support
1     | Behaviour

As you can see one person might have multiple reasons so i need another table to have the following format:

Refid | Reason1   | Reason2            | Reason3     |  ETC...
------|-----------|--------------------|-----------
1     | Admission | Advice and Support | Behaviour

But I don't know how to write a query to extract the data and write it in a new table like this. The reasons don't have dates of other criteria that would make any reason to be in any special order. All reasons are assigned at the time of referral.

Thanks For yor Help.. SQL Server 2012

Was it helpful?

Solution

You are modelling a many to many relationship

You need 3 tables

- One for Reasons (say ReasonID and Reason)
- One for each entity identified by RefID (say RefID and ReferenceOtherData)
- An junction (or intersection) table with the keys (RefID, ReasonID)

This way,

  • Multiple reasons can apply to one Ref entity
  • Multiple Refs can have the same reason

You turn repeated columns into rows.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top