title: Query for key value pair structure
body: <p>I am using SQL Server 2008 R2. I have a table structure as below :</p>
  
  <pre><code>Id (Identity): int  
  GroupId:       int  
  Field Id:      int  
  Field Name:    nvarchar(1000)  
  Value:         nvarchar(max)  
  </code></pre>
  
  <p>This table can be viewed as a key-value pair structure. </p>
  
  <p>So if I have the following records in the UI :</p>
  
  <pre><code>Due Date    | Target Date       | Status  
  -----------------------------------
  5-Jun-2013  |       4-June-2013 | Pending  
  10-Jun-2013 |       8-June-2013 | Completed  
  15-Jun-2013 |      11-June-2013 | Pending  
  </code></pre>
  
  <p>In the database each column goes as a separate record. So the database will have:</p>
  
  <pre><code>Id  | Group Id  | Field Id  | Field Name    | Value  
  -----------------------------------------------------------
  1   | 1         | 1         | Due Date      | 5-Jun-2013  
  2   | 1         | 2         | Target Date   | 4-Jun-2013  
  3   | 1         | 3         | Status        | Pending  
  
  4   | 2         | 1         | Due Date      | 10-Jun-2013  
  5   | 2         | 2         | Target Date   | 8-Jun-2013  
  6   | 2         | 3         | Status        | Completed  
  
  7   | 3         | 1         | Due Date      | 15-Jun-2013  
  8   | 3         | 2         | Target Date   | 11-Jun-2013  
  9   | 3         | 3         | Status        | Pending  
  </code></pre>
  
  <p>While retrieving back from database I would like to get output in the same format as I see in UI. What is the best way to get this result, considering the table will have a huge amount of data?</p>
  
  <p>Any help in this regards will be really useful.</p>