2014-11-25 101 views
-1
id  record_name  record_value 
------------------------------------- 
1001 price1   12 
1001 price2   1 
1001 price3   8 
1201 price1   18 
1201 price2   2 
1201 price3   6 
1601 price1   12 
1601 price2   8 
1601 price3   8 

輸出的SQL Server ...子查詢返回多個

id  price1  value  price2  value  price3  value 
-------------------------------------------------------------------------- 
1001 price1  12   price2  1   price3  8 
1201 price1  18   price2  2   price3  6 

我收到錯誤

子查詢返回多個值。當子查詢遵循=,!=,<,< =,>,> =或當子查詢用作表達式時,這是不允許的。

我用這個查詢:

select distinct 
    a.id, 'Price1', 
    (select record_value 
    from table_name 
    where id = a.id and record_name = 'price1') as 'value1', 
    'Price2', 
    (select record_value 
    from table_name 
    where id = a.id and record_name = 'price2') as 'value2', 
    'Price3', 
    (select record_value 
    from table_name 
    where id = a.id and record_name = 'price3') as 'value3' 
from 
    table_name a 

請儘量解決這個問題!

+3

那麼,哪個DBMS? Postgres的? SQL Server? SQLite的? – 2014-11-25 07:30:01

+0

好問題。哪一個 – Jaques 2014-11-25 07:31:30

+0

隨着你在這裏發佈的數據..有沒有像這樣的錯誤..它的工作很好.. – Deepshikha 2014-11-25 07:36:56

回答

5

這種方法有點不同。但是,你可以這樣做:

SELECT 
    table_name.id, 
    'price1' AS price1, 
    SUM(CASE WHEN record_name='price1' THEN record_value ELSE 0 END) AS value1, 
    'price2' AS price2, 
    SUM(CASE WHEN record_name='price2' THEN record_value ELSE 0 END) AS value2, 
    'price3' AS price2, 
    SUM(CASE WHEN record_name='price3' THEN record_value ELSE 0 END) AS value3 
FROM 
    table_name 
GROUP BY 
    table_name.id 

更新

要回復記者的置評。是的,它會工作。如果我們看一下這樣一個簡單的測試:

DECLARE @tbl TABLE(ID INT, test VARCHAR(100)) 

INSERT INTO @tbl 
VALUES 
(1,'foo'), 
(1,'foo'), 
(1,'bar'), 
(1,'bar') 

此查詢將有一個靜態值作爲price1。

SELECT 
    tbl.ID, 
    'price1' as price1 
FROM 
    @tbl AS tbl 
GROUP BY 
    tbl.ID 

更新2

然後,如果你不想SUM值。然後,您可以改用MAX。像這樣:

SELECT 
    table_name.id, 
    'price1' AS price1, 
    MAX(CASE WHEN record_name='price1' THEN record_value ELSE 0 END) AS value1, 
    'price2' AS price2, 
    MAX(CASE WHEN record_name='price2' THEN record_value ELSE 0 END) AS value2, 
    'price3' AS price2, 
    MAX(CASE WHEN record_name='price3' THEN record_value ELSE 0 END) AS value3 
FROM 
    table_name 
GROUP BY 
    table_name.id 
+0

由於price1,price2,..etc包含在Group by中,此查詢是否有效? – 2014-11-25 07:37:20

+0

@PareshJ:更新了答案 – Arion 2014-11-25 07:42:28

+0

Arion,很好的例子。我一直懷疑靜態列值是否需要分組。現在,它很清楚。 – 2014-11-25 07:46:08

0

我剛剛修改了查詢,在查詢中加入了'top 1'以避免'Subquery返回多個值'的錯誤。 檢查並更新您的狀態。

select distinct 
    a.id, 'Price1', 
    (select top 1 record_value 
    from table_name 
    where id = a.id and record_name = 'price1') as 'value1', 
    'Price2', 
    (select top 1 record_value 
    from table_name 
    where id = a.id and record_name = 'price2') as 'value2', 
    'Price3', 
    (select top 1 record_value 
    from table_name 
    where id = a.id and record_name = 'price3') as 'value3' 
from 
    table_name a 
+0

我需要所有記錄,包括重複.....頂1不會爲我工作:( – Kumar 2014-11-25 13:23:55

0

你也可以嘗試在子查詢Distinct。它不是最好的解決方案,但它的工作。

select distinct 
a.id, 'Price1', 
(select distinct record_value 
from table_name 
where id = a.id and record_name = 'price1') as 'value1', 
'Price2', 
(select distinct record_value 
from table_name 
where id = a.id and record_name = 'price2') as 'value2', 
'Price3', 
(select distinct record_value 
from table_name 
where id = a.id and record_name = 'price3') as 'value3' 
from 
table_name a