Oracle
 sql >> डेटाबेस >  >> RDS >> Oracle

तीन स्तंभों के साथ ORACLE 10g में स्ट्रिंग एकत्रीकरण

Oracle 10 के लिए, अपने दृष्टिकोण का उपयोग करते हुए - समस्या आपकी आंतरिक क्वेरी में विभाजन है।

WITH tab as (
SELECT 1 as fdate,     'Apple' as fruit,    1 as num from dual union
SELECT 1 as fdate,     'Apple' as fruit,    2 as num from dual union
SELECT 1 as fdate,     'Apple' as fruit,    3 as num from dual union
SELECT 1 as fdate,     'Kiwi' as fruit,    6 as num from dual union
SELECT 1 as fdate,     'Kiwi' as fruit,    10 as num from dual union
SELECT 2 as fdate,     'Apple' as fruit,    4 as num from dual union
SELECT 2 as fdate,     'Apple' as fruit,    5 as num from dual union
SELECT 2 as fdate,     'Apple' as fruit,    6 as num from dual union
SELECT 2 as fdate,     'Kiwi' as fruit,    4 as num from dual union
SELECT 2 as fdate,     'Kiwi' as fruit,    7 as num from dual )
SELECT fdate, fruit,LTRIM(MAX(SYS_CONNECT_BY_PATH(num,','))
    KEEP (DENSE_RANK LAST ORDER BY curr),',') AS fruits_agg
    FROM   (SELECT fdate,
            fruit,
            num,
            ROW_NUMBER() OVER (PARTITION BY fdate, fruit ORDER BY num) AS curr,
            ROW_NUMBER() OVER (PARTITION BY fdate, fruit ORDER BY num) -1 AS prev
     FROM   tab)
  GROUP BY fdate,fruit
  CONNECT BY prev = PRIOR curr AND fruit = PRIOR fruit AND fdate = PRIOR fdate
 START WITH curr = 1;

देता है:

FDATE             FRUIT   FRUITS_AGG                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
1                "Kiwi"  "6,10"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
1                "Apple" "1,2,3"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
2                "Kiwi"  "4,7"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
2                "Apple" "4,5,6"

Oracle 11 समाधान बहुत आसान है:

WITH tab as (
SELECT 1 as fdate,     'Apple' as fruit,    1 as num from dual union
SELECT 1 as fdate,     'Apple' as fruit,    2 as num from dual union
SELECT 1 as fdate,     'Apple' as fruit,    3 as num from dual union
SELECT 1 as fdate,     'Kiwi' as fruit,    6 as num from dual union
SELECT 1 as fdate,     'Kiwi' as fruit,    10 as num from dual union
SELECT 2 as fdate,     'Apple' as fruit,    4 as num from dual union
SELECT 2 as fdate,     'Apple' as fruit,    5 as num from dual union
SELECT 2 as fdate,     'Apple' as fruit,    6 as num from dual union
SELECT 2 as fdate,     'Kiwi' as fruit,    4 as num from dual union
SELECT 2 as fdate,     'Kiwi' as fruit,    7 as num from dual )
select fdate
     , fruit
     , listagg(num,'-') within group ( order by num ) fruit_agg
from tab
group by fdate, fruit 

रिटर्न:

FDATE  FRUIT    FRUIT_AGG
1      Kiwi      6-10
1      Apple     1-2-3
2      Kiwi      4-7
2      Apple     4-5-6



  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Oracle इकाई फ्रेमवर्क - विभिन्न स्कीमा में तालिकाओं से इकाई उत्पन्न करें

  2. Oracle ODP.NET संस्करण अज्ञेयवादी विकल्प

  3. भारतीय नंबरिंग सिस्टम में नंबर को शब्दों में बदलें

  4. Oracle 10g में एक टेबल कॉलम का नाम कैसे बदलें

  5. अपडेट ज्वाइन करने में त्रुटि