简体   繁体   English

查询键值对结构

[英]Query for key value pair structure

I am using SQL Server 2008 R2. 我正在使用SQL Server 2008 R2。 I have a table structure as below : 我有一个表结构如下:

Id (Identity): int  
GroupId:       int  
Field Id:      int  
Field Name:    nvarchar(1000)  
Value:         nvarchar(max)  

This table can be viewed as a key-value pair structure. 可以将该表视为键值对结构。

So if I have the following records in the UI : 因此,如果我在UI中具有以下记录:

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  

In the database each column goes as a separate record. 在数据库中,每一列都作为单独的记录。 So the database will have: 因此数据库将具有:

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  

While retrieving back from database I would like to get output in the same format as I see in UI. 从数据库取回时,我希望以与UI中相同的格式获取输出。 What is the best way to get this result, considering the table will have a huge amount of data? 考虑到表中将包含大量数据,什么是获得此结果的最佳方法?

Any help in this regards will be really useful. 在这方面的任何帮助将非常有用。

Whether this is a good design or not, you can accomplish what you want with the data as presented in the OP. 不管这是一个好的设计,您都可以使用OP中显示的数据来完成所需的工作。 The key is to note that the difference between the ids identifies the rows. 关键是要注意,id之间的差异标识行。 I must emphasize that an explicit "record" reference should be included in the design, so I would call this message a hack. 我必须强调,在设计中应包含明确的“记录”引用,因此我将此消息称为“ hack”。

select (id - FieldId),
       max(case when FieldName = 'DueDate' then value end) as DueDate,
       max(case when FieldName = 'TargetDate' then value end) as TargetDate,
       max(case when FieldName = 'Status' then value end) as Status
from t
group by (id - FieldId)

By the way, the fact that you have to store dates as strings is further indication of a problem with the data structure. 顺便说一句,您必须将日期存储为字符串这一事实进一步表明了数据结构存在问题。 Even in an EAV structure, you can have "ValueStr", "ValueDate", "ValueInt" as separate columns to store different values (or use a sqlvariant type). 即使在EAV结构中,也可以将“ ValueStr”,“ ValueDate”,“ ValueInt”作为单独的列来存储不同的值(或使用sqlvariant类型)。

try this one 试试这个

SELECT 
      MAX(CASE WHEN fieldName = 'Due Date' THEN fvalue ELSE NULL END) [Due Date],
      MAX(CASE WHEN fieldName = 'Target Date' THEN fvalue ELSE NULL END) [Target Date],
      MAX(CASE WHEN fieldName = 'Status' THEN fvalue ELSE NULL END) [Status] 
FROM #tmp GROUP BY (id - FieldId)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM