0

我的情况与下面讨论的示例几乎相同。Impala:选择仅返回表2中最大(日期)但小于表1中的日期的行的数据

select data where date is max(date) less than x

我有表作为

s_currency

cdate       ratio      currency            
------------------------------------------------------------------- 
2017-06-06 00:00:00.0   1       USD             
2017-06-05 00:00:00.0   1       USD             
2017-06-04 00:00:00.0   1       USD  

s_transaction

tdate       amount     currency            
------------------------------------------------------------------- 
2017-06-05 00:00:00.0   100      USD             
2017-06-08 00:00:00.0   55      USD  
2017-06-08 00:00:00.0   55      USD  
2017-06-08 00:00:00.0   60      USD  

而结果我要的是

tdate       amount currency ratio  cdate           
-------------------------------------------------------------------------- 
2017-06-05 00:00:00.0   100  USD   1  2017-06-05 00:00:00.0         
2017-06-08 00:00:00.0   55   USD   1  2017-06-06 00:00:00.0 
2017-06-08 00:00:00.0   55   USD   1  2017-06-06 00:00:00.0  
2017-06-08 00:00:00.0   60   USD   1  2017-06-06 00:00:00.0          

其中cdate应基于tdate,其最新货币日期等于或早于交易日期。

其他文章中的解决方案在select子句中使用子查询,并且它在Impala中不起作用。我尝试使用CTE并与子查询连接,但没有一个返回所需的结果。下面是一些我建立了查询,其结果

SELECT tdate, amount, t1.currency, ratio, cdate FROM s_transaction t1 , s_currency t2 
     WHERE t1.currency = t2.currency AND 
     t2.cdate = (select max(cdate) from s_currency 
        where currency = t1.currency and cdate <= t1.tdate); 

但这连接表和所有的货币少,交易日期返回的交易,所以我得到

tdate       amount  currency  ratio  cdate 
------------------------------------------------------------------------------------------ 
2017-06-08 00:00:00.0   60   USD   1   2017-06-05 00:00:00.0   
2017-06-08 00:00:00.0   60   USD   1   2017-06-06 00:00:00.0   
2017-06-08 00:00:00.0   60   USD   1   2017-06-04 00:00:00.0   
2017-06-05 00:00:00.0   100   USD   1   2017-06-05 00:00:00.0   
2017-06-05 00:00:00.0   100   USD   1   2017-06-04 00:00:00.0   
2017-06-08 00:00:00.0   55   USD   1   2017-06-05 00:00:00.0   
2017-06-08 00:00:00.0   55   USD   1   2017-06-06 00:00:00.0   
2017-06-08 00:00:00.0   55   USD   1   2017-06-04 00:00:00.0   
2017-06-08 00:00:00.0   55   USD   1   2017-06-05 00:00:00.0   
2017-06-08 00:00:00.0   55   USD   1   2017-06-06 00:00:00.0   
2017-06-08 00:00:00.0   55   USD   1   2017-06-04 00:00:00.0 

所以,我摆脱最大(cdate)并使用限制1的顺序,但Impala抛出错误,其错误带有LIMIT子句的不受支持的相关子查询。

我尝试使用CTE和写

with lastupdate as (
select t2.currency, ratio, max(cdate) as cdate from s_currency t2 join s_transaction t1 
on cdate <= tdate and t2.currency = t1.currency group by t2.currency, ratio limit 1 
) select t11.*, lst.ratio, lst.cdate 
from s_transaction t11 join lastupdate lst   

但这里的CTE挑选一个值,并使用它的所有事务,所以我得到

tdate       amount  currency  ratio  cdate 
------------------------------------------------------------------------------------------- 
2017-06-05 00:00:00.0   100   USD   1   2017-06-06 00:00:00.0   
2017-06-08 00:00:00.0   55   USD   1   2017-06-06 00:00:00.0   
2017-06-08 00:00:00.0   60   USD   1   2017-06-06 00:00:00.0   
2017-06-08 00:00:00.0   55   USD   1   2017-06-06 00:00:00.0   

其中在6月05日的交易中,应该有一个cdate作为6月05日。

我甚至尝试在子查询中使用row_number()函数,但它无法解析t1.tdate以比较日期。

我怎样才能达到我想要的?

回答

0

我在mysql数据库中测试了以下SQL语句,它的工作原理。

select tdate,amount,b.currency,ratio,max(cdate) from s_transaction a,s_currency b where a.tdate>=b.cdate group by tdate 
+0

我将在s_transaction表多个事务,因此由tdate分组不会帮我...我也有比我的实际表中列出的列多,所以用同样的选择聚合函数将使其非常庞大。另外你的查询中的group by子句只有tdate,我认为它应该有select中的所有列,除了group by子句 – Sathiesh

+0

中列出的max(cdate)之外s_transaction表中是否有任何主键? 我想你应该在s_transaction表中有类似“transaction id”字段的东西,如果是这样的话,你可以通过“transaction id”进行分组,然后得到最大的cdate然后完成。 –

+0

我没有一个唯一的键,它基本上是一个视图,所以它只会有视图应该有的列。我尝试创建一个row_number()并将其用作主键,但它看起来不太好。我也怀疑它是否存在Impala中的缺陷...如果我使用row_number()的解决方案,我会在这里发布 – Sathiesh

相关问题