2012-05-03 56 views
-2

下面是MySQL數據庫查詢my tables here轉換MySQL查詢Oracle9i中,SQL Server和DB2

SELECT vc.vehicle_class_name 
, GROUP_CONCAT(CASE WHEN v.status = 2 THEN v.vehicle_id END) completed 
, GROUP_CONCAT(CASE WHEN v.status <> 2 THEN v.vehicle_id END) incomplete 
FROM vehicle_class vc 
JOIN vehicle v 
ON v.vehicle_class_id = vc.vehicle_class_id 
GROUP 
BY vc.vehicle_class_id; 

我需要幫助在Oracle9i,DB2和SQL Server轉換。

請幫忙。

+0

'具有集合函數但不工作的oracle'的情況'。 – user1373028

+0

Oracle9i不再受支持。爲什麼你使用這樣一個過時的版本? –

回答

1

這是一個Oracle9i解決方案。 該作業使用查詢中需要CONNECT BY子句的SYS_CONNECT_BY_PATH函數完成。爲了通過某些東西連接行VEHICLE表是「準備好的」......它用curr和prev行號填充。

SELECT 
    vc.vehicle_class_name, 
    RTRIM(REPLACE(MAX(SYS_CONNECT_BY_PATH(v.completed,'|')) KEEP (DENSE_RANK LAST ORDER BY v.curr),'|',''),',') completed, 
    RTRIM(REPLACE(MAX(SYS_CONNECT_BY_PATH(v.incomplete,'|')) KEEP (DENSE_RANK LAST ORDER BY v.curr),'|',''),',') incomplete 
FROM 
(SELECT 
    vehicle_class_id, 
    CASE WHEN status=2 THEN vehicle_id||',' END completed, 
    CASE WHEN status<>2 THEN vehicle_id||',' END incomplete, 
    ROW_NUMBER() OVER (PARTITION BY vehicle_class_id ORDER BY vehicle_id) curr, 
    ROW_NUMBER() OVER (PARTITION BY vehicle_class_id ORDER BY vehicle_id)-1 prev 
FROM 
    vehicle) v JOIN vehicle_class vc 
ON v.vehicle_class_id=vc.vehicle_class_id 
GROUP BY 
    vc.vehicle_class_name 
CONNECT BY 
    v.prev=PRIOR v.curr AND v.vehicle_class_id=PRIOR v.vehicle_class_id 
START WITH 
    v.curr=1;