2013-12-22 122 views
0

我使用SQL Server 2008的如何从SQL Server中的键/值表中获取项目?

我有这样的一个表:

+-------------+--------+------------+-----------+--------+ 
| UnitPriceId | ItemId | ProductKey | Key | Value | 
+-------------+--------+------------+-----------+--------+ 
|   1 |  1 | x   | Quantity | 50  | 
|   2 |  1 | x   | PaperType | 1  | 
|   3 |  1 | x   | Price  | 25.00 | 
|   4 |  2 | x   | Quantity | 100 | 
|   5 |  2 | x   | PaperType | 1  | 
|   6 |  2 | x   | Price  | 40.00 | 
|   7 |  3 | x   | Quantity | 250 | 
|   8 |  3 | x   | PaperType | 1  | 
|   9 |  3 | x   | Price  | 80.00 | 
|   10 |  4 | x   | Quantity | 500 | 
|   11 |  4 | x   | PaperType | 1  | 
|   12 |  4 | x   | Price  | 120.00 | 
|   13 |  5 | x   | Quantity | 1000 | 
|   14 |  5 | x   | PaperType | 1  | 
|   15 |  5 | x   | Price  | 180.00 | 
|   16 |  6 | x   | Quantity | 3000 | 
|   17 |  6 | x   | PaperType | 1  | 
|   18 |  6 | x   | Price  | 300.00 | 
|   19 |  7 | x   | Quantity | 50  | 
|   20 |  7 | x   | PaperType | 2  | 
|   21 |  7 | x   | Price  | 30.00 | 
|   22 |  8 | x   | Quantity | 100 | 
|   23 |  8 | x   | PaperType | 2  | 
|   24 |  8 | x   | Price  | 50.00 | 
|   25 |  9 | x   | Quantity | 250 | 
|   26 |  9 | x   | PaperType | 2  | 
|   27 |  9 | x   | Price  | 100.00 | 
|   28 |  10 | x   | Quantity | 500 | 
|   29 |  10 | x   | PaperType | 2  | 
|   30 |  10 | x   | Price  | 150.00 | 
|   31 |  11 | x   | Quantity | 1000 | 
|   32 |  11 | x   | PaperType | 2  | 
|   33 |  11 | x   | Price  | 220.00 | 
|   34 |  12 | x   | Quantity | 3000 | 
|   35 |  12 | x   | PaperType | 2  | 
|   36 |  12 | x   | Price  | 350.00 | 
|   37 |  13 | x   | Quantity | 50  | 
|   38 |  13 | x   | PaperType | 3  | 
|   39 |  13 | x   | Price  | 35.00 | 
|   40 |  14 | x   | Quantity | 100 | 
|   41 |  14 | x   | PaperType | 3  | 
|   42 |  14 | x   | Price  | 60.00 | 
|   43 |  15 | x   | Quantity | 250 | 
|   44 |  15 | x   | PaperType | 3  | 
|   45 |  15 | x   | Price  | 120.00 | 
|   46 |  16 | x   | Quantity | 500 | 
|   47 |  16 | x   | PaperType | 3  | 
|   48 |  16 | x   | Price  | 180.00 | 
|   49 |  17 | x   | Quantity | 1000 | 
|   50 |  17 | x   | PaperType | 3  | 
|   51 |  17 | x   | Price  | 250.00 | 
|   52 |  18 | x   | Quantity | 3000 | 
|   53 |  18 | x   | PaperType | 3  | 
|   54 |  18 | x   | Price  | 400.00 | 
+-------------+--------+------------+-----------+--------+ 

我有量,papertype和的ProductKey。我如何通过查询获得该物品的价格?

ProductKey应该包含在查询中。

因为ProductKey可以不同。

+3

请参见[五个简单的数据库设计错误You Shou ld避免(点#3)](https://www.simple-talk.com/sql/database-administration/five-simple--database-design-errors-you-should-avoid/)讨论为什么EAV真的非常糟糕,并且看到了坏的CaRMa(https://www.simple-talk.com/content/article.aspx?article=292)了一个EAV系统设计的真实的恐怖故事,毁了一个蓬勃发展的公司。 –

回答

1
DECLARE @Quantity int 
DECLARE @PaperType int 
DECLARE @ProductKey varchar(1) 

SET @Quantity = 100 
SET @PaperType = 1 
SET @ProductKey = 'x' 

SELECT T.[Value] Price, T.[ProductKey] FROM T 
    JOIN T Q ON (Q.[Key]='Quantity' and T.[ItemId]=Q.[ItemId]) 
    JOIN T PT ON (PT.[Key]='PaperType' and T.[ItemId]=PT.[ItemId]) 
WHERE T.[Key]='Price' 
    AND Q.[Value][email protected] 
    AND PT.[Value][email protected] 
    AND T.[ProductKey][email protected] 

输出:

PRICE PRODUCTKEY 
40 x 

SQLFIDDLE

1

我建议使用T-SQL的PIVOT功能:

SELECT ItemId, ProductKey, Quantity, PaperType, Price 
FROM (
     SELECT ItemId, ProductKey, [Key], Value 
     FROM T 
    ) AS yadayada 
PIVOT (MAX(Value) FOR [Key] IN (Quantity, PaperType, Price)) AS pvt 

PIVOT查询的结果

(我相信这个结果与@revouaquery使用连接相同):

| ITEMID | PRODUCTKEY | QUANTITY | PAPERTYPE | PRICE | 
    |--------|------------|----------|-----------|-------| 
    |  1 |   x |  50 |   1 | 25 | 
    |  2 |   x |  100 |   1 | 40 | 
    |  3 |   x |  250 |   1 | 80 | 
    |  4 |   x |  500 |   1 | 120 | 
    |  5 |   x |  1000 |   1 | 180 | 
    |  6 |   x |  3000 |   1 | 300 | 
    |  7 |   x |  50 |   2 | 30 | 
    |  8 |   x |  100 |   2 | 50 | 
    |  9 |   x |  250 |   2 | 100 | 
    |  10 |   x |  500 |   2 | 150 | 
    |  11 |   x |  1000 |   2 | 220 | 
    |  12 |   x |  3000 |   2 | 350 | 
    |  13 |   x |  50 |   3 | 35 | 
    |  14 |   x |  100 |   3 | 60 | 
    |  15 |   x |  250 |   3 | 120 | 
    |  16 |   x |  500 |   3 | 180 | 
    |  17 |   x |  1000 |   3 | 250 | 
    |  18 |   x |  3000 |   3 | 400 | 

您可以用where子句这样的查询这样的:用一个CTE

WHERE PaperType = @PaperType AND Quantity = @Quantity 

OR

;WITH a1 AS 
(
SELECT ItemId, ProductKey, Quantity, PaperType, Price 
FROM (
     SELECT ItemId, ProductKey, [Key], Value 
     FROM T 
    ) AS yadayada 
PIVOT (MAX(Value) FOR [Key] IN (Quantity, PaperType, Price)) AS pvt 

) 
SELECT ItemId, Price, ProductKey 
FROM a1 
WHERE PaperType = @PaperType AND Quantity = @Quantity 

SQLFIDDLE

相关问题