Postgres WHEN案例选择查询

时间:2017-08-23 20:04:11

标签: postgresql postgresql-9.3

我有一个Postgres WHEN案例查询错误。我该如何修复查询?

SELECT CASE
            WHEN  AccountStatus='Open' 
           THEN
             (
              SELECT  * 
               from Accounts
               where statusID=1
             )

        WHEN  AccountType='Mutual'
           THEN
             (
               SELECT *
               FROM Accounts
               WHERE AccountTypeID=2
             )

   END as Status, *
FROM Accounts

显示错误:

  

用作表达式

的子查询返回的多行

2 个答案:

答案 0 :(得分:0)

我认为您的问题不是案例查询,您希望获得如下所示的表格行。如果您真的想使用case when类型查询,请详细定义表格。

SELECT *, 'Open' AS Status from Accounts where statusID = 1
UNION ALL
SELECT *, 'Mutual' AS Status FROM Accounts WHERE AccountTypeID = 2 

答案 1 :(得分:-1)

你应该试试这个: -

SELECT *
FROM Accounts
WHERE CASE  WHEN (AccountStatus='Open' ) THEN statusID=1 
            WHEN (AccountType='Mutual')THEN AccountTypeID=2 
      END;