2014-02-18 62 views
0
獲取

表如下:分離行和JSP

+------------+--------------+ 
| product id | Rates  | 
+------------+--------------+ 
| 108  | 10, 20, 30 | 
+------------+--------------+ 
| 109  | 10,30  | 
+------------+--------------+ 

我想創建以下文件:

+------------+--------------+ 
| Name  | Rates  | 
+------------+--------------+ 
| 108  | 10   | 
+------------+--------------+ 
|   | 20   | 
+------------+--------------+ 
|   | 30   | 
+------------+--------------+ 

我想這樣在Oracle 11g和我在jsp中獲取行。

回答

1

嘗試使用此查詢:

WITH tab(product_id, Rates) AS 
(SELECT 108, '10, 20, 30' FROM dual UNION ALL 
SELECT 109, '10,30'  FROM dual) 
------- 
--End of data 
------- 
SELECT CASE WHEN LEVEL = 1 THEN product_id ELSE NULL END AS product_id, 
     regexp_substr(rates, '[^,]+', 1, LEVEL) rates 
    FROM TAB 
CONNECT BY regexp_substr(rates, '[^,]+', 1, LEVEL) IS NOT NULL 
    AND PRIOR rates = rates 
    and prior sys_guid() is not null; 

輸出:

| PRODUCT_ID | RATES | 
|------------|-------| 
|  108 | 10 | 
|  (null) | 20 | 
|  (null) | 30 | 
|  109 | 10 | 
|  (null) | 30 | 
2

假設表名爲product_rate,查詢箱可以導致了答案:

SELECT CASE 
      WHEN LEVEL = 1 THEN product_id ELSE NULL END AS Name, 
     regexp_substr(Rates, '[^,]+', 1, LEVEL) Rates 
    FROM product_rate 
CONNECT BY LEVEL <= length(regexp_replace(Rates, '[^,]+')) + 1; 

Sqlfiddle demo

+0

我不想再次打印身份證打印只有一次和所有費率在該ID .. – Arpit

+0

其更多的建議做你的用戶界面層的東西,BTW我已經更新了答案。 –

+0

我不知道如何處理用戶界面..我使用jsp。 – Arpit