2014-02-26 49 views
1

以下是sql server中的CTE示例。我必須在KDB中做類似的遞歸。 KDB是否支持遞歸查詢或與之相近的東西。目前我能想到的功能,創造和保持各遞歸的臨時數據到的東西......kdb遞歸查詢中的CTE

 
USE AdventureWorks2012; 
GO 
WITH DirectReports(ManagerID, EmployeeID, Title, EmployeeLevel) AS 
(
    SELECT ManagerID, EmployeeID, Title, 0 AS EmployeeLevel 
    FROM dbo.MyEmployees 
    WHERE ManagerID IS NULL 
    UNION ALL 
    SELECT e.ManagerID, e.EmployeeID, e.Title, EmployeeLevel + 1 
    FROM dbo.MyEmployees AS e 
     INNER JOIN DirectReports AS d 
     ON e.ManagerID = d.EmployeeID 
) 
SELECT ManagerID, EmployeeID, Title, EmployeeLevel 
FROM DirectReports 
ORDER BY ManagerID; 
GO 

回答

1

我不是100%肯定你正在嘗試做的,但在KDB兩個有用的遞歸關鍵字

over 

scan 

如果你只在遞歸的最終結果有興趣的你使用過:

ex。

q){x+2*y} over 2 3 5 7 
32 

但如果你想從每個步驟的輸出,使用掃描:

前。

q){x+2*y} scan 2 3 5 7 
2 8 18 32 

這些都是傑夫Borror的q爲凡人的例子。更多這裏:http://code.kx.com/q/ref/control/#over