查找值是否存在于第二列中的任何位置并返回结果

时间:2015-11-30 07:59:39

标签: sql oracle birt

我需要将一个列数据与另一个列数据匹配,如果第一列存在于第二列中,那么结果应该是' 0.5' (1/2)否则,如果不匹配或跨越两列,结果应为' 0'(零)。

我的表包含以下数据:

Job_Id   link_Id   
2         3
3         2
4         5
5         4
6         null
7         8
8         7
10        null

预期结果:

Job_Id  link_Id  cycle
2         3        0.5
3         2        0.5
4         5        0.5
5         4        0.5
6         null     0
7         8        0.5
8         7        0.5
10        null     0

enter image description here

我的查询:

select t.job_id
     , t.link_id
     , round((case when t.link_job_id IS NULL then 1 else null end))/2 cycles 
   from T_QCMS_JOB_STATE_HIS t

这不太有用

3 个答案:

答案 0 :(得分:2)

您的表格看起来可能是分层的,在这种情况下,recursive CTE/sub-query factoring clause可能会在将来帮助您。

要获得当前结果,您只需要进行自我加入:

select coalesce(l.job_id, j.job_id) as job_id
     , l.link_id
     , case when l.link_id is not null then 0.5 else 0 end as cycle
  from t_qcms_job_state_his j
  left outer join t_qcms_job_state_his l
    on j.job_id = l.link_id;

    JOB_ID    LINK_ID      CYCLE
---------- ---------- ----------
         2          3         .5
         3          2         .5
         4          5         .5
         5          4         .5
         7          8         .5
         8          7         .5
        10                     0
         6                     0

8 rows selected.

外部联接用于处理并非所有链接ID都存在的事实。

另一种非ANSI兼容方式,但只涉及单个表扫描将使用Oracle的FIRST函数,这会更加混乱,但会更有效:

with the_data as(
select job_id
     , max(link_id) keep (dense_rank first order by case when job_id = link_id then 0 else 1 end) as link_id
  from t_qcms_job_state_his
 group by job_id
       )
select job_id
     , link_id
     , case when link_id is not null then 0.5 else 0 end as cycle
  from the_data

答案 1 :(得分:2)

使用超前和滞后功能来获得所需的输出

    SELECT job_id
    ,link_id
    ,nvl(CASE 
        WHEN lead(job_id) OVER (
                ORDER BY job_id
                ) = link_id
            AND lead(link_id) OVER (
                ORDER BY job_id
                ) = job_id
            OR lag(job_id) OVER (
                ORDER BY job_id
                ) = link_id
            AND lag(link_id) OVER (
                ORDER BY job_id
                ) = job_id
            THEN 0.5
        END,0) status1
FROM Table1

答案 2 :(得分:1)

为什么不只是标准的外部联接?

public Response getJsonData()
{
    params = new Params();
    String url = "https://www.yoururl.com/controller/function_you_want;
    Response response = null;
    HttpGet httpGet = new HttpGet(url);

    try
    {
        response = httpClient.execute(httpGet);

        //check to make sure that everything is ok
        if(response.getStatusLine().getStatusCode() == 200)
        {
            entity = response.getEntity();
            jsonResponse = EntityUtils.toString(entity);
            JsonNode root = mapper.readTree(jsonResponse).get("result");
            response = gson.fromJson(root.toString(),Response.class);
        }
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }

    return response;
}

自由写,因为没有提供创建表和数据。根据需要采用。