简体   繁体   English

使用选择语句中的数据选择子查询?

[英]select subquery using data from the select statement?

I have two tables, headers and lines . 我有两个表, headerslines I need to grab the batch_submission_date from the header table, but sometimes a query for batch_id will return a null for batch_submission_date, but will also return a parent_batch_id, and if we query THAT parent_batch_id as a batch_id, it will then return the correct batch_submission_date. 我需要batch_submission_date表中获取batch_submission_date ,但有时对batch_id的查询将返回batch_idnull ,但还将返回parent_batch_id,并且如果将THAT parent_batch_id查询为batch_id,则它将返回正确的batch_submission_date。

eg 例如

SELECT t1.batch_id, 
    t1.parent_batch_id, 
    t2.batch_submission_date 
FROM db.headers t1, db.lines t2 
WHERE t1.batch_id = '12345';

output = 12345, 99999, null

Then we use that parent batch_id as a batch_id : 然后,我们将该父batch_id用作batch_id:

SELECT t1.batch_id, 
    t1.parent_batch_id, 
    t2.batch_submission_date
FROM db.headers t1, db.lines t2
WHERE t1.batch_id = '99999';

and we get output = 99999,99999,'2018-01-01'

So I'm trying to write a query that will do this for me - anytime a batch_id's batch_submission_date is null, we find that batch_id's parent batch_id and query that instead. 因此,我正在尝试编写一个查询来为我完成此操作-每当batch_id的batch_submission_date为null时,我们都会找到该batch_id的父parent batch_id,并对其进行查询。

This was my idea - but I just get back null both for bp_batch_submission_date and for new_submission_date . 这是我的想法-但我只是拿回空既为bp_batch_submission_datenew_submission_date

SELECT
    t1.parent_id as parent_id,
    t1.BATCH_ID as bp_batch_id,
    t2.BATCH_LINE_NUMBER as bp_batch_li,
    t1.BATCH_SUBMISSION_DATE as bp_batch_submission_date,

    CASE 
        WHEN t1.BATCH_SUBMISSION_DATE is null
    THEN
        (SELECT a.BATCH_SUBMISSION_DATE 
        FROM 
        db.headers a,
        db.lines b 
        WHERE 
            a.SD_BATCH_HEADERS_SKEY = b.SD_BATCH_HEADERS_SKEY   
            and a.parent_batch_id = bp_batch_id
            and b.batch_line_number = bp_batch_li
        ) END as new_submission_date

FROM    
    db.headers t1,
    db.lines t2 
WHERE
    t1.SD_BATCH_HEADERS_SKEY = t2.SD_BATCH_HEADERS_SKEY   
    and (t1.BATCH_ID = '12345' or t1.PARENT_BATCH_ID = '12345') 
    and t2.BATCH_LINE_NUMBER = '1'              
GROUP BY
    t2.BATCH_CLAIM_LINE_STATUS_DESC,
    t1.PARENT_BATCH_ID,
    t1.BATCH_ID,
    t2.BATCH_LINE_NUMBER,
    t1.BATCH_SUBMISSION_DATE;

is what I'm trying to do possible? 是我想做的可能吗? using the bp_batch_id and bp_batch_li variables 使用bp_batch_idbp_batch_li变量

Use CTE (common table expression) to avoid redundant code, then use coalesce() to find parent date in case of null. 使用CTE(公用表表达式)来避免冗余代码,然后在为null的情况下使用coalesce()查找父日期。 In your first queries you didn't attach joining condition between two tables, I assumed it's based on sd_batch_headers_skey like in last query. 在您的第一个查询中,您没有在两个表之间附加连接条件,我假设它是基于sd_batch_headers_skey就像在上一个查询中一样。

dbfiddle demo dbfiddle演示

with t as (
    select h.batch_id, h.parent_batch_id, l.batch_submission_date bs_date
      from headers h
      join lines l on l.sd_batch_headers_skey = h.sd_batch_headers_skey 
                  and l.batch_line_number = '1' )
select batch_id, parent_batch_id, 
       coalesce(bs_date, (select bs_date from t x where x.batch_id = t.parent_batch_id)) bs_date
  from t 
  where batch_id = 12345;

You could use simpler syntax with connect by and level <= 2 but if in your data there are really rows containing same ids ( 99999 , 99999 ) then we get cycle error. 你可以使用简单的语法与connect bylevel <= 2 ,但如果在你的数据真的有含有相同的ID(行9999999999 ),那么我们得到循环错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM