带SELECT的子查询中的CASE

时间:2017-08-17 20:04:44

标签: sql oracle

这是我撰写的查询

SELECT id, name, (SELECT is_enable FROM customers WHERE id=table_one.id) AS some_tag FROM table_one;

以上查询返回此日期

    ID NAME                       SOME_TAG
     4 name 1                          0
     3 name 2                          0
     1 name 3                          1

我试图在上面的查询中使用CASE,以便将SOME_TAG的值设为"是"当1和"否"当0但到目前为止没有运气。任何帮助/线索将不胜感激!谢谢!

到目前为止,我试过这个

select id, name, (select is_enable case when is_enable is not null then "No" else "Yes" end from customers where id=table_one.id) as some_tag from table_one;

2 个答案:

答案 0 :(得分:3)

您可以使用联接而不是子查询...然后这就是案例的工作方式。

{
    "personWrapper": [{
        "PersonUuid": "31fb318d-a9bf-4c2f-ad16-0810ddd73746",
        "LastName": "LN0",
        "FirstName": "FN0",
        "BirthDate": "1997-08-17T15:10:08.9633612-04:00"
    },
    {
        "PersonUuid": "73fdacc7-e1e8-48ff-b161-1bd8b5f4aec1",
        "LastName": "LN1",
        "FirstName": "FN1",
        "BirthDate": "1996-08-17T15:10:08.9633612-04:00"
    },
    {
        "PersonUuid": "d18b4324-2d3e-41ca-9525-fe769af89e9c",
        "LastName": "LN2",
        "FirstName": "FN2",
        "BirthDate": "1995-08-17T15:10:08.9633612-04:00"
    }]
}

答案 1 :(得分:0)

以下代码也适用于您的问题

SELECT 
  id, 
  name, 
  (CASE is_enable 
    WHEN 1 THEN 'YES'
    WHEN 0 THEN 'NO' 
  END) AS TAG
FROM ( SELECT id, name, (SELECT is_enable FROM customers WHERE id=table_one.id) AS some_tag FROM table_one );