Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Need a query to get data from two tables with specific format

Need a query to get data from two tables with specific format

Scheduled Pinned Locked Moved Database
database
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sowvin
    wrote on last edited by
    #1

    Hello, Thanks in advance. Table-1 startdate MSNUM 12/12/2012 1,2,3 12/10/2010 4 12/9/2009 5,6 12/13/2014 10/3/2003 Table-2 ID DESC 1 ONE 2 TWO 3 THREE 4 FOUR 5 FIVE Expected OutPut Result MSGNUM DESC 1,2,3 ONE,TWO,THREE 4 FOUR 5,6 FIVE,SIX Note: Table-1 contains 7690 records and table-2 contains 160 fixed rows Please let me know how can get the above output. I am trying using cursors still didn't found any solution. If anyone already worked on this can save my time. best Regards, Kumar

    M N 2 Replies Last reply
    0
    • S sowvin

      Hello, Thanks in advance. Table-1 startdate MSNUM 12/12/2012 1,2,3 12/10/2010 4 12/9/2009 5,6 12/13/2014 10/3/2003 Table-2 ID DESC 1 ONE 2 TWO 3 THREE 4 FOUR 5 FIVE Expected OutPut Result MSGNUM DESC 1,2,3 ONE,TWO,THREE 4 FOUR 5,6 FIVE,SIX Note: Table-1 contains 7690 records and table-2 contains 160 fixed rows Please let me know how can get the above output. I am trying using cursors still didn't found any solution. If anyone already worked on this can save my time. best Regards, Kumar

      M Offline
      M Offline
      Mycroft Holmes
      wrote on last edited by
      #2

      Because you have stored you MSNUM as a comma separated string (idiot) you are going to have to split the string. For this you need a split function that will return a table with the date and 1 ID per row. You then use this table to join you 2 existing tables. This is the direct result of crappy design where someone is too bloody lazy to create the additional table to store the Date/MSNUM as discreet records. There are plenty of examples around[^]

      Never underestimate the power of human stupidity RAH

      1 Reply Last reply
      0
      • S sowvin

        Hello, Thanks in advance. Table-1 startdate MSNUM 12/12/2012 1,2,3 12/10/2010 4 12/9/2009 5,6 12/13/2014 10/3/2003 Table-2 ID DESC 1 ONE 2 TWO 3 THREE 4 FOUR 5 FIVE Expected OutPut Result MSGNUM DESC 1,2,3 ONE,TWO,THREE 4 FOUR 5,6 FIVE,SIX Note: Table-1 contains 7690 records and table-2 contains 160 fixed rows Please let me know how can get the above output. I am trying using cursors still didn't found any solution. If anyone already worked on this can save my time. best Regards, Kumar

        N Offline
        N Offline
        Niral Soni
        wrote on last edited by
        #3

        I am not sure what database you are using, but below is the query which resolves your problem and that works well in Oracle 11g.

        SELECT MSGNUM, LISTAGG(DESCR, ',') WITHIN GROUP (ORDER BY LVL) AS MSGDESCR
        FROM (SELECT DISTINCT MSGNUM, LEVEL LVL
        ,SUBSTR(NVL2(MSGNUM, MSGNUM || ',', NULL)
        ,(CASE WHEN LEVEL > 1 THEN
        INSTR(NVL2(MSGNUM, MSGNUM || ',', NULL), ',', 1, LEVEL - 1) + 1
        ELSE LEVEL END)
        ,INSTR(NVL2(MSGNUM, MSGNUM || ',', NULL), ',', 1, LEVEL)
        - (CASE WHEN LEVEL > 1 THEN
        INSTR(NVL2(MSGNUM, MSGNUM || ',', NULL), ',', 1, LEVEL - 1) + 1
        ELSE LEVEL END)
        ) MSGNUM_TO_ID
        FROM
        -- REPLACE FOLLOWING BLOCK WITH YOUR FIRST TABLE
        (SELECT SYSDATE - 1 STARTDATE, '1,2,3' MSGNUM FROM DUAL UNION
        SELECT SYSDATE - 2 STARTDATE, '4' MSGNUM FROM DUAL UNION
        SELECT SYSDATE - 3 STARTDATE, '5,6' MSGNUM FROM DUAL UNION
        SELECT SYSDATE - 4 STARTDATE, '' MSGNUM FROM DUAL UNION
        SELECT SYSDATE - 5 STARTDATE, '4,2,5' MSGNUM FROM DUAL UNION
        SELECT SYSDATE - 6 STARTDATE, '' MSGNUM FROM DUAL)
        CONNECT BY INSTR(NVL2(MSGNUM, MSGNUM || ',', NULL), ',', 1, LEVEL) != 0
        ) T1
        -- REPLACE FOLLOWING BLOCK WITH YOUR SECOND TABLE
        ,(SELECT 1 ID, 'ONE' DESCR FROM DUAL UNION
        SELECT 2 ID, 'TWO' DESCR FROM DUAL UNION
        SELECT 3 ID, 'THREE' DESCR FROM DUAL UNION
        SELECT 4 ID, 'FOUR' DESCR FROM DUAL UNION
        SELECT 5 ID, 'FIVE' DESCR FROM DUAL UNION
        SELECT 6 ID, 'SIX' DESCR FROM DUAL) T2
        WHERE T1.MSGNUM_TO_ID = T2.ID
        GROUP BY MSGNUM
        ;

        Thanks & Regards, Niral Soni

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups