值我有一个SQL查询类似如下:如何从另一行获得SQL
select
,c.customer_leg1
,d.mid
,c.previous_customer_leg1
,c.creation_date
,c.end_date
,c.cid
from table1 c
JOIN table2 d
ON c.cid = d.cid
where c.cid = '1234'
这给下面的输出:
customer_leg1 | previous_customer_leg1 | creation_data | end_date | cid
4092 | 1888 | 05/06/17 | 05/07/17 | 735
8915 | 4092 | 05/06/17 | 05/08/17 | 735
我想添加一个新列,使得对于每个customer_leg1
,我们发现在previous_customer_leg1
中应该将该行的"end_date"
放在该列中。
对于例如:在上面的输出的第1行是customer_leg1
4092
,这在第2行中发现previous_customer_leg1
,所以在第1行,这new_column
应该有它05/08/17
。对于那些customer_leg1在previous_customer_leg1中不匹配的应用程序,它应该为NULL。我想我可能会使用分区和滞后功能,但我对这些并不十分清楚。任何帮助将不胜感激。谢谢!
你根本没有关闭。为什么你认为你需要'lag()'和'partition'?你需要的是自我加入,而不是分析功能。最好只说出你的问题(就像你一样);不包括如何在你的问题陈述中解决它。 (你没有,你只是给你的印象你给的标题。) – mathguy
你发布的查询没有太大意义。你选择的所有列都来自'table1','where'条件也都是基于同一个表,为什么你加入'table2'? – mathguy
,因为实际的查询真的很大,这只是我想要的东西 – Arman