sql server ...子查询返回的不止

时间:2014-11-25 07:27:49

标签: sql sql-server sql-server-2008

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

输出

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

我收到错误

  

子查询返回的值超过1。当子查询遵循=,!=,<,< =,>,> =或子查询用作表达式时,不允许这样做。

我使用了这个查询:

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 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

我刚刚通过在查询中添加“top 1”来修改您的查询,以避免错误“Subquery返回的值超过1”。 检查并更新您的状态。

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 

答案 2 :(得分: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