将条件RANK窗口函数从SQL转换为Pandas

时间:2019-11-06 05:24:48

标签: python sql sql-server pandas

我正在尝试将窗口函数从SQL转换为Pandas,仅在可以匹配的条件下应用该函数-否则将插入NULL(无)值。

SQL代码(示例)

0x51ee878764 naveen
0x51ee878764
0x51ee878764 naveen

示例

将最新客户请求的ID(如果有)加入到客户中。

SELECT 
    [ID_customer]
    [cTimestamp]
    [TMP_Latest_request].[ID_req] AS [ID of Latest request]
FROM [table].[Customer] AS [Customer]
LEFT JOIN (
    SELECT * FROM(
        SELECT [ID_req], [ID_customer], [rTimestamp], 
        RANK() OVER(PARTITION BY ID_customer ORDER BY rTimestamp DESC) as rnk
        FROM [table].[Customer_request]
    ) AS [Q]
    WHERE rnk = 1
) AS [TMP_Latest_request]
ON [Customer].[ID_customer] = [TMP_Latest_request].[ID_customer]

Python Pandas中的等效项是什么?

1 个答案:

答案 0 :(得分:0)

除了使用RANK()函数外,您还可以简单地使用以下内容,并且转换起来很容易。

SELECT A.ID_Customer,A.cTimeStamp,B.ID_req
FROM Customer A
LEFT JOIN (
    SELECT ID_Customer,MAX(ID_req)ID_req 
    FROM Customer_request
    GROUP BY ID_Customer
   )B
ON A.ID_Customer = B.ID_Customer

尝试以下查询,如果您遇到任何问题,请在评论中问我。