用另一个查询扩充sql查询的输出

用另一个查询扩充sql查询的输出,sql,oracle,variables,Sql,Oracle,Variables,我有两个表,我想用 来自第二个表上另一个查询的值 我可以使用下面的第二个查询从第二个表中获得所需的值。 此查询可以返回多行 select ip_country from table2 where account_id = 'customer1' and created_on >= to_timestamp('15-NOV-2013','dd-MON-yyyy') order by created_on desc; 输出: 美国 (空) 我们 (空) 我只对最新的一行感兴趣,所以我使用这

我有两个表,我想用 来自第二个表上另一个查询的值

我可以使用下面的第二个查询从第二个表中获得所需的值。 此查询可以返回多行

select ip_country from table2
where 
account_id = 'customer1' and
created_on >= to_timestamp('15-NOV-2013','dd-MON-yyyy')
order by created_on desc;
输出:

美国
(空)
我们
(空)

我只对最新的一行感兴趣,所以我使用这样的查询

select ip_country from (select ip_country from table2 
where 
account_id = 'customer1' and
created_on >= to_timestamp('15-NOV-2013','dd-MON-yyyy')
order by created_on desc) where rownum = 1;
输出:
us

我想将其扩展到的查询是:

select
txnid, account_id, result
from table1     
where  
table1.txnid = 101
输出:
101,客户1,通过

我想写一个如下的查询

select
txnid, account_id, result, ip_country = (select ip_country from (select ip_country from table2 
where 
account_id = 'customer1' and
created_on >= to_timestamp('15-NOV-2013','dd-MON-yyyy')
order by created_on desc) where rownum = 1;)
from table1, table2     
where  
table1.txnid = 101;
这当然在语法上是错误的,但希望这能传达其含义。 我的问题是如何编写这个查询

期望输出:
101,客户1,通行证,美国


我试图在没有任何运气的情况下使用连接。我省略了一些与这里的查询无关的其他列。我正在开发一个Oracle数据库。请随意将标题更改为更合适的名称,我不太知道如何更好地表达这一点。

我认为这将满足您的要求:

select txnid, account_id, result,
       (select max(ip_country) keep (dense_rank first order by created_on desc)
        from table2
        where account_id = 'customer1' and
              created_on >= to_timestamp('15-NOV-2013','dd-MON-yyyy')
       ) as ip_country
from table1 
where table1.txnid = 101;

然而,我认为您的查询有两个简单的问题。首先在子查询中有一个分号,然后在外部查询中有一个不必要的联接。通过使用Oracles
keep
/
first
功能,上面消除了一层子查询。我认为这将满足您的要求:

select txnid, account_id, result,
       (select max(ip_country) keep (dense_rank first order by created_on desc)
        from table2
        where account_id = 'customer1' and
              created_on >= to_timestamp('15-NOV-2013','dd-MON-yyyy')
       ) as ip_country
from table1 
where table1.txnid = 101;

然而,我认为您的查询有两个简单的问题。首先在子查询中有一个分号,然后在外部查询中有一个不必要的联接。通过使用Oracles
keep
/
first
功能,上面消除了一层子查询。我认为这将满足您的要求:

select txnid, account_id, result,
       (select max(ip_country) keep (dense_rank first order by created_on desc)
        from table2
        where account_id = 'customer1' and
              created_on >= to_timestamp('15-NOV-2013','dd-MON-yyyy')
       ) as ip_country
from table1 
where table1.txnid = 101;

然而,我认为您的查询有两个简单的问题。首先在子查询中有一个分号,然后在外部查询中有一个不必要的联接。通过使用Oracles
keep
/
first
功能,上面消除了一层子查询。我认为这将满足您的要求:

select txnid, account_id, result,
       (select max(ip_country) keep (dense_rank first order by created_on desc)
        from table2
        where account_id = 'customer1' and
              created_on >= to_timestamp('15-NOV-2013','dd-MON-yyyy')
       ) as ip_country
from table1 
where table1.txnid = 101;

然而,我认为您的查询有两个简单的问题。首先在子查询中有一个分号,然后在外部查询中有一个不必要的联接。通过使用Oracles
keep
/
first
功能,上述功能消除了一层子查询。

如果需要相同的帐户id:

select t1.txnid, t1.account_id, t1.result, t2max.ip_country
from table1 t1, 
(select t2.ip_country, t2.account_id from table2 t2
 where t2.account_id = 'customer1' and 
 t2.created_on >= to_timestamp('15-NOV-2013','dd-MON-yyyy')
 order by t2.created_on desc limit 1) t2max 
where t2max.account_id = t1.account_id
group by t1.txnid, t1.account_id, t1.result;
或者表1和表2中的帐户id不同:

select t1.txnid, t1.account_id, t1.result, t2max.ip_country
from table1 t1, 
(select t2.ip_country from table2 t2
 where t2.account_id = 'customer1' and 
 t2.created_on >= to_timestamp('15-NOV-2013','dd-MON-yyyy')
 order by t2.created_on desc limit 1) t2max 
group by t1.txnid, t1.account_id, t1.result;

如果需要相同的帐户\u id:

select t1.txnid, t1.account_id, t1.result, t2max.ip_country
from table1 t1, 
(select t2.ip_country, t2.account_id from table2 t2
 where t2.account_id = 'customer1' and 
 t2.created_on >= to_timestamp('15-NOV-2013','dd-MON-yyyy')
 order by t2.created_on desc limit 1) t2max 
where t2max.account_id = t1.account_id
group by t1.txnid, t1.account_id, t1.result;
或者表1和表2中的帐户id不同:

select t1.txnid, t1.account_id, t1.result, t2max.ip_country
from table1 t1, 
(select t2.ip_country from table2 t2
 where t2.account_id = 'customer1' and 
 t2.created_on >= to_timestamp('15-NOV-2013','dd-MON-yyyy')
 order by t2.created_on desc limit 1) t2max 
group by t1.txnid, t1.account_id, t1.result;

如果需要相同的帐户\u id:

select t1.txnid, t1.account_id, t1.result, t2max.ip_country
from table1 t1, 
(select t2.ip_country, t2.account_id from table2 t2
 where t2.account_id = 'customer1' and 
 t2.created_on >= to_timestamp('15-NOV-2013','dd-MON-yyyy')
 order by t2.created_on desc limit 1) t2max 
where t2max.account_id = t1.account_id
group by t1.txnid, t1.account_id, t1.result;
或者表1和表2中的帐户id不同:

select t1.txnid, t1.account_id, t1.result, t2max.ip_country
from table1 t1, 
(select t2.ip_country from table2 t2
 where t2.account_id = 'customer1' and 
 t2.created_on >= to_timestamp('15-NOV-2013','dd-MON-yyyy')
 order by t2.created_on desc limit 1) t2max 
group by t1.txnid, t1.account_id, t1.result;

如果需要相同的帐户\u id:

select t1.txnid, t1.account_id, t1.result, t2max.ip_country
from table1 t1, 
(select t2.ip_country, t2.account_id from table2 t2
 where t2.account_id = 'customer1' and 
 t2.created_on >= to_timestamp('15-NOV-2013','dd-MON-yyyy')
 order by t2.created_on desc limit 1) t2max 
where t2max.account_id = t1.account_id
group by t1.txnid, t1.account_id, t1.result;
或者表1和表2中的帐户id不同:

select t1.txnid, t1.account_id, t1.result, t2max.ip_country
from table1 t1, 
(select t2.ip_country from table2 t2
 where t2.account_id = 'customer1' and 
 t2.created_on >= to_timestamp('15-NOV-2013','dd-MON-yyyy')
 order by t2.created_on desc limit 1) t2max 
group by t1.txnid, t1.account_id, t1.result;


你能提供一些样本数据和期望的结果吗?例如,我发现很难遵循查询的逻辑。当然,我会为表添加示例数据。您能提供一些示例数据和所需的结果吗?例如,我发现很难遵循查询的逻辑。当然,我会为表添加示例数据。您能提供一些示例数据和所需的结果吗?例如,我发现很难遵循查询的逻辑。当然,我会为表添加示例数据。您能提供一些示例数据和所需的结果吗?例如,我发现很难遵循查询的逻辑。当然,我将为表添加示例数据。我在这个查询中得到一个语法错误,它表示FROM关键字未在预期位置找到。在条件
where account\u id='customer1'
中,customer1的值来自表1。@nikhil您应该删除
ip\u country=
部分(将
ip\u country
放在子查询之后以别名列)。在内部查询中将
'customer1'
替换为
表1.account\u id
。谢谢@YaroslavShabalin。这很好用。非常感谢Gordon,在此之前我从未听说过keep/first或dense rank。我在这个查询中发现一个语法错误,它说FROM关键字未在预期的位置找到。在条件
where account\u id='customer1'
中,customer1的值来自表1。@nikhil您应该删除
ip\u country=
部分(将
ip\u country
放在子查询之后以别名列)。在内部查询中将
'customer1'
替换为
表1.account\u id
。谢谢@YaroslavShabalin。这很好用。非常感谢Gordon,在此之前我从未听说过keep/first或dense rank。我在这个查询中发现一个语法错误,它说FROM关键字未在预期的位置找到。在条件
where account\u id='customer1'
中,customer1的值来自表1。@nikhil您应该删除
ip\u country=
部分(将
ip\u country
放在子查询之后以别名列)。在内部查询中将
'customer1'
替换为
表1.account\u id
。谢谢@YaroslavShabalin。这很好用。非常感谢Gordon,在此之前我从未听说过keep/first或dense rank。我在这个查询中发现一个语法错误,它说FROM关键字未在预期的位置找到。在条件
where account\u id='customer1'
中,customer1的值来自表1。@nikhil您应该删除
ip\u country=
部分(将
ip\u country
放在子查询之后以别名列)。在内部查询中将
'customer1'
替换为
表1.account\u id
。谢谢@YaroslavShabalin。这很好用。非常感谢戈登,在此之前我从未听说过keep/first或dense rank。