值我有一個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