TARGET RESULT COMPLEX SQL QUERY

Hello

I'm writing a sql, for the scenario below, but don't get it.

There are two Table TEMP_AAA and TEMP_BBB,
Under the table, we have values in descending order C_ID,

Now to give a value in D_ID,.
We must find the value of C_ID
and if this C_ID value is contained in the B_ID,
then we should display the value of allowance A_ID,

The complexity here is,
When you come down (in fact different levels) lines of C_ID,
already consumed values should not be used.

In this example,.
for D_ID ABC says
We have 1234 as C_ID and
B_ID is 12345 which contains C_ID
A_ID coresponding is 111 and 222.

Next step for D_ID (same level): DEF since the C_ID is the same as the process described above
We have A_ID 111 and 222.

Then for D_ID GHI.
We have 12 as C_ID and
B_ID is 12345,12345,12045, which contains the C_ID
A_ID coresponding allocation is 111,222,333,
Now since 111 and 222 is already used in the ABC/DEF D_ID we would not have considered.
A_ID would be 333

Then for D_ID MNO.
We have NULL as C_ID, which means all B_ID default matches (special account)
B_ID is 12345,12345,12045,10045, which contains C_ID (default)
A_ID coresponding allocation is 111,222,333,444
Now given that 111,222,333 is already used in D_ID ABC/DEF/GHI/JKL we would not have considered.
A_ID would be 444.



Here is the Script and exit

 

CREATE TABLE TEMP_AAA( A_ID VARCHAR2(10),  B_ID VARCHAR2(10));
CREATE TABLE TEMP_BBB( C_ID VARCHAR2(10),  D_ID VARCHAR2(10));

INSERT INTO TEMP_AAA(A_ID,B_ID) VALUES('111','12345');
INSERT INTO TEMP_AAA(A_ID,B_ID) VALUES('222','12345');
INSERT INTO TEMP_AAA(A_ID,B_ID) VALUES('333','12000');
INSERT INTO TEMP_AAA(A_ID,B_ID) VALUES('444','10000');

INSERT INTO TEMP_BBB(C_ID,D_ID) VALUES('1234','ABC');
INSERT INTO TEMP_BBB(C_ID,D_ID) VALUES('1234','DEF');
INSERT INTO TEMP_BBB(C_ID,D_ID) VALUES('12','GHI');
INSERT INTO TEMP_BBB(C_ID,D_ID) VALUES('12','JKL');
INSERT INTO TEMP_BBB(C_ID,D_ID) VALUES(NULL,'MNO');
EXPECTED RESULTS
---------------
 

D_ID      RESULT A_ID
---------------
ABC     111
ABC     222
DEF     111
DEF     222
GHI     333
JKL     333
MNO     444

Hello

Thanks for posting the CREATE TABLE and INSERT statements; This helps a lot.

Interesting problem!
For each allocation a_id, you have to find the number of times where a c_id equaled and choose only the lines where it is the 1st time.
This sounds like a job for the analytic RANK function:

WITH     got_c_id_rank     AS
(
     SELECT       b.d_id
     ,       a.a_id
     ,       RANK () OVER ( PARTITION BY  a_id
                                 ORDER BY      c_id     DESC     NULLS LAST
                      ) AS c_id_rank
     FROM       temp_aaa     a
     JOIN       temp_bbb     b     ON     a.b_id     LIKE b.c_id || '%'
)
SELECT       d_id
,       a_id          AS result_a_id
FROM       got_c_id_rank
WHERE       c_id_rank     = 1
ORDER BY  d_id
,            result_a_id
;

Tags: Database

Similar Questions

  • Complex sql query

    table a and table b.

    Table 1
    EmpID (pk)
    EMRP number
    year

    Table 2
    detail_id (PK)
    EmpID (FK)
    SSN
    wages


    I have 2 tables (TABLE 1) parent and a child (TABLE 2)

    I want to write a sql query to get records from TABLE 2 THAT HAVE THE SSN AND WAGES. with the help of a condition that is less than

    as far as I know is the EMRP number and year, using what I need to get all records from Table1 that has the same number of EMRP and year and then for records in table 2 (emp id) that I need to get duplicates (which have the same ssn and wages)


    does anyone know how to write a query for that

    If you do not understand ask, thank you very much to try

    Thank you

    Hello

    You probably want something like this:

    SELECT  t2.*
    FROM     table_1     t1
    JOIN     table_2     t2     ON     ta.empid     = t2.empid
    WHERE     t1.empr     = :p_empr
    AND     t1.year     = :p_year
    ;
    

    but I'm not sure I understand the problem and had to guess things.

    If you wish, you can hardcode the literals in lieu of the bind variable: p_empr and: p_year.

    Whenever you have a problem, please post CREATE TABLE and INSERT statements for some examples of data.
    Also post the results desired (with specific examples from the data you've posted) these data, as well as an explanation of how to get these results.

  • The Variable value based on the results of SQL query

    With the help of OBIEE 11.1

    Is it possible to set the value of a variable presentation of the results of a SQL statement?

    Scenario:

    I have 2 topics.

    SubjectArea1

    Text1

    Date1

    SubjectArea2

    Field1

    Field2

    UpdateDate

    I have an analysis that uses only the SubjectArea2.

    Using a dashboard quickly, the user must be able to select Text1 in the other topic area (SubjectArea1).

    Assuming that selection will be stored in a variable of presentation (SelectedText),

    The filter in the analysis should be something like

    Upper UpdateDate to @{SelectedDate}

    How can I set a variable (SelectedDate) using a SQL statement?

    Something like this:

    SELECT "Date1" TO "SubjectArea1" WHERE "Text1" = @{SelectedText}

    So the command prompt text selection should give a date that is used to filter the second review?

    If so:

    The prompt on SA1.textCol

    hidden analysis who is invited on SA1.texCol has SA1.dateCol in the criteria

    Analysis SA2.dateCol is filetered on basis of the results of another analysis (any value analysis hidden SA1.dateCol)

  • Problems...  ORA-00907 otherwise ORA-00937 complex SQL query.

    Hello

    Anyone can solve this query? or tell me what is wrong with him?

    SELECT COUNT (1) Total, RAPP_SERVER, SYSTEM, GOOD, ERROR
    Of
    (
    SELECT tblName.RAPP_SERVER,

    DECODE (SUBSTR (tblName.NOM_SYSTEM, 1, 2), "C0", "CON", "C1", "CON", "CH", "PEPS", "AD", "PEPS", "EA", "PEPS", "OE", "PEPS", "CR", "SYSSERV", 'GS', 'SYSSERV', 'MC', 'SYSSERV', 'EC', 'SYSSERV', a ', 'SYSSERV',)

    ('DB', 'INFO', 'PP', 'INFO', 'ED', 'INFO', 'DO', 'END_SUP', EM ","HIGHER EDUCATION","EP","END_SUP","SE","END_SUP","IF","END_SUP","SR","HIGHER EDUCATION","EI","END_SUP","SG","SAID","ZA","AEL","ZF","AEL","ZI","AEL","IG", ' OTHER ',"LAUGHED", 'OTHER') SYSTEM,
    Sum (case when tblName.ST_CODE = 4 Gold tblName.ST_CODE = 12 Then 1 else null end) AS GOOD,
    Sum (case when tblName.ST_CODE = 4 tblName.ST_CODE = 12 Then NULL else 1 end gold) AS ERROR
    OF GPI01102.tblName
    GROUP BY tblName.RAPP_SERVER, ERROR SYSTEM, GOOD.
    UNION ALL
    SELECT tblName.RAPP_SERVER, DECODE (SUBSTR (NOM_SYSTEM, 1, 3), "DET", "' OTHERS, 'DEU', ' TEACHING)

    HIGHER ',' PRC ',' OTHER ', 'PRO', 'SAYS') SYSTEM.
    Sum (case when tblName.ST_CODE = 4 Gold tblName.ST_CODE = 12 Then 1 else null end) AS GOOD,
    Sum (case when tblName.ST_CODE = 4 tblName.ST_CODE = 12 Then NULL else 1 end gold) AS ERROR
    OF GPI01102.tblName
    GROUP BY tblName.RAPP_SERVER, ERROR SYSTEM, GOOD.
    WHEN FINISHED BETWEEN: P_Start_Date AND: P_End_Date
    )
    RAPP_SERVER GROUP, SYSTEM, GOOD ERROR

    Looking at your original with abbreviated instructions BOX query 2 queries appears to be identical with the exception of the data on the second query restriction.

    SELECT tblName.RAPP_SERVER,
           DECODE(SUBSTR(tblName.NOM_SYSTEM, 1, 2), 'C0', 'CON') SYSTEM,
           SUM(CASE
                 WHEN tblName.ST_CODE = 4 OR tblName.ST_CODE = 12 THEN
                  1
                 ELSE
                  NULL
               END) AS GOOD,
           SUM(CASE
                 WHEN tblName.ST_CODE = 4 OR tblName.ST_CODE = 12 THEN
                  NULL
                 ELSE
                  1
               END) AS ERROR
      FROM GPI01102.tblName
     GROUP BY tblName.RAPP_SERVER, SYSTEM, GOOD, ERROR
    
    UNION ALL
    
    SELECT tblName.RAPP_SERVER,
           DECODE(SUBSTR(NOM_SYSTEM, 1, 3), 'DET', 'AUTRES') SYSTEM,
           SUM(CASE
                 WHEN tblName.ST_CODE = 4 OR tblName.ST_CODE = 12 THEN
                  1
                 ELSE
                  NULL
               END) AS GOOD,
           SUM(CASE
                 WHEN tblName.ST_CODE = 4 OR tblName.ST_CODE = 12 THEN
                  NULL
                 ELSE
                  1
               END) AS ERROR
      FROM GPI01102.tblName
     GROUP BY tblName.RAPP_SERVER, SYSTEM, GOOD, ERROR
     WHERE FINISHED BETWEEN :P_Start_Date AND :P_End_Date
    

    Queries are on the same table twice? If so, can we explain the differences in the results of the 2 queries? You may be able to combine DECODES and use a single query.

  • Incorrect result with SQL query

    Dear all,

    When I run the following query, it should return 55 but returning 24 instead of 55.

    Select TO_CHAR (TO_DATE (April 25, 2009 ',' MON-DD-YYYY'), 'JJ')
    -TO_CHAR (TO_DATE ('01 - MAR - 2009 "," DD-MON-YYYY '), 'DD') DAYS OF DOUBLE;

    Can help any one on this?

    Thank you

    As you use the wrong data type for arithmatic operations.

    SQL> select TO_DATE('25-APR-2009', 'DD-MON-YYYY') - TO_DATE('01-MAR-2009', 'DD-MON-YYYY')  DAYS FROM DUAL;
    
          DAYS
    ----------
            55
    

    Never compare strings as dates.

  • ORA-06504: PL/SQL: return variables of the game results or the query types do not match

    Hello!

    I have a simple object type and a proecdure in which I am trying to use it to insert into another table

    -object

    CREATE ORREPLACETYPEmt_mtg ASOBJECT

    (

    ACOL NUMBER ,

    BCOL NVARCHAR2 (100)

    );

    CREATE ORREPLACETYPEREF_MTG ASTABLEOFMt_MTG ;

    -same structure as the use of sampletbl target table in the cursor query

    create table tbl_MT_MTG

    (

    ACOL NUMBER ,

    BCOL NVARCHAR2 (100)

    );

    -procedure

    CREATE ORREPLACEINTERIORTEST_PROCEDURE1

    AS

    ref_cur sys_refcursor ;

    REFR ref_mtg ;

    BEGIN

    OPEN ref_cur FOR

    Select acol,

    BCOL

    DE sampletbl rownum<10;

    Fetch ref_cur in bulk collectintorefr;

    Insert intotbl_MT_MTG(acol,bcol)selectacol,bcol fromtable(refr);

    commit;

    CLOSE Ref_cur;

    END;

    /

    When I run this procedure fails with

    ORA-06504: PL/SQL: return variables of the game results or the query types do not match

    ORA-06512: at "TEST_PROCEDURE1", line 10

    ORA-06512: at line 2

    Any help on this please...

    Thanks to an OLD POST below

    so perfect helped me! Thank you

    Tubby

    After 5 years of more :-)

    How to store refcursor in collection How to store refcursor in collection

  • How to get the sql query result?

    Hello

    Currently I use LV2012 to connect to an Oracle database server. After the Oracle Express and Oracle ODBC driver facilities/settings made.

    I managed to use the SQL command to query the data through my command prompt window.

    Now the problem is, how to do the same task in Labview using database connectivity tools?

    I have build a VI to query as being attached, but I have no idea of what range to use to get the result of the query.

    Please help me ~ ~

    Here is a piece of code that I use to test the SQL commands, you can use the part that retrieves the results of sql.

    It is also possible to get the rear column headers, but it's for the next lesson!

    ;-)

  • Looking for a SQL query to retrieve callvariables + ECC to a RESULTS of SCRIPT EXECUTE (translation VRUS)

    Hi team,

    I'm looking for a SQL query check data (ECC + CallVariable) due to a RESULT of SCRIPT EXECUTE when you ask an external VRU with a translation route to VRU with a 'run external Script '.

    In my view, that the data are analyzed between the termination call detail + termination call Variable.

    If you have already such a SQL query I would be very grateful to have.

    Thanks and greetings

    Nick

    Omar,

    respectfully, shorten the interval of a day might not be an option for a history report ;-)

    I recommend to take a look at the following SQL query:

    DECLARE @dateFrom DATETIME, @dateTo DATETIME

    SET @dateFrom = ' 2014-01-24 00:00:00 '

    SET @dateTo = ' 2014-01-25 00:00:00 '

    SELECT

    TCV. DateTime,

    TCD. RecoveryKey,

    TCD. RouterCallKeyDay,

    TCD. RouterCallKey,

    VME. EnterpriseName AS [ECVEnterpriseName],

    TCV. ArrayIndex,

    TCV. ECCValue

    OF Termination_Call_Variable tcv

    JOIN THE

    (SELECT RouterCallKeyDay, RouterCallKey, RecoveryKey IN Termination_Call_Detail WHERE DateTime > @dateFrom AND DateTime)< @dateto)="">

    THE tcv. TCDRecoveryKey = tcd. RecoveryKey

    LEFT OUTER JOIN Expanded_Call_Variable VME ON tcv. ExpandedCallVariableID = VME. ExpandedCallVariableID

    WHERE the tcv. DateTime > @dateFrom AND tcv. DateTime<>

    With variables, you can set up your code (for example, you could write SET @dateFrom =? and let the calling application to fill in the DateTime for you).

    In addition, join two large tables with all the lines, as you did (TCD - TCV) is never a good option.

    Another aspect to consider: all the ECC is actually arrays (always), is not good to leave aside the index value (tcv. ArrayIndex).

    G.

  • the SQL query result format

    Format the results of the SQL query, currently, show as below.

    Select license_type, correspondents of license_info where license_type in ('TEM', 'TDM') order by license_type;

    TDM 1445140800000000000
    TDM 1420952400000000000
    TDM 1444363200000000000
    TDM 1445054400000000000
    TDM 1428724800000000000
    TEM 1421125200000000000
    TEM 1423890000000000000
    TEM 1449637200000000000
    TEM 1444968000000000000
    TEM 1428724800000000000

    I need formatting like below

    Expected result:

    TDM 1445140800000000000 TEM 1421125200000000000
    TDM 1420952400000000000 TEM 1423890000000000000
    TDM 1444363200000000000 TEM 1449637200000000000
    TDM 1445054400000000000 TEM 1444968000000000000
    TDM 1428724800000000000 TEM 1428724800000000000

    I have sample data only, Blue said the change in your main table request

    Try:

    Select max (f1),

    Max (F2),

    Max (F3),

    Max (F4)

    de)

    Select decode (license_type, 'TDM' license_type) f1,

    Decode (license_type, 'TDM', expiration_date) f2,

    Decode (license_type, 'TEM' license_type) f3,

    Decode (license_type, 'TEM', expiration_date) f4,

    ROW_NUMBER() over (partition by order correspondents license_type) r

    Of

    )

    R group

    ----

    Ramin Hashimzade

  • The SQL query result

    Hello

    Here's my data in the table for itm_id = 1000

    ITM_IDADD_ATRDEL_ATR
    1000a: b:c:d
    1000d
    1000d
    1000e:f:g
    1000e:f:g

    My required return is (a: b:c:d) d = a: b: c + d = a: = a +(e:f:g) b:c:d: b:c:d:e:f:g-(e:f:g) = a: b: c; d

    Output ITM_ID

    1000A: b:c:d

    can we get the output using the sql query above?

    I had tried with union but could not able to get the required result.

    Please let me know if there is no possible solution

    Thank you

    David

    I have the impression that you are looking to treat each character in 'RTA' as an individual item and then add or delete depending on whether they are an ADD_ATR or DEL_ATR.

    So something like...

    SQL > ed
    A written file afiedt.buf

    1 with t as (select 1000 as itm_id, ' a: b/c: some like add_atr, null as del_atr of all the double union)
    2 select 1000, null, would be "union double all the"
    3 select 1000, 'd', null of union double all the
    4 select 1000, 'e:f:g', null of union double all the
    5 select 1000, null, "e:f:g" of all the double union
    6 select 1234, null, "x: y: z" of all the double union
    7 select 1234, 'u: v: w', null of union double all the
    8 select 1234, null, 'u' of all the double union
    9 select 1234, "x: y", null of union double all the
    10. Select 1234, "x: z", null of union double all the
    11. Select 1234, 'p', the double null
    12            )
    13-
    14. end of test data
    15-
    16. Select itm_id
    17, cast (listagg(atr,':') within the Group (order by atr) as varchar2 (40)) output
    18 of)
    19 select itm_id
    20, atr
    21, sum (add_del)
    22 of)
    23 select itm_id
    24, coalesce(add_atr,del_atr) as full_atr
    25, decode (add_atr, null,-1, 1) as add_del
    26, regexp_substr (coalesce(add_atr,del_atr),'[^:] +', 1, level) RTA
    27, flat surface that l
    28 t
    29 to connect by level<= (length(coalesce(add_atr,del_atr)="">
    30 and itm_id = prior itm_id
    31 and coalesce(add_atr,del_atr) = prior coalesce(add_atr,del_atr)
    sys_guid() 32 and prior is not null
    33             )
    34 group by itm_id, atr
    35 having sum (add_del) > 0
    36       )
    37 * group by itm_id
    SQL > /.

    ITM_ID OUTPUT
    ---------- ----------------------------------------
    1000A: b:c:d
    1234 p:v:w:x

    that just shows how stupidly data is as it should be divided every thing in the different elements so that you can then group to determine how many of each, you have (and whether to add or subtract) and then re - combine results again in a combined string.

  • SQL Query - store the result for optimization?

    Good day experts,

    I'm looking for advice on a report. I did a lot of analytical functions to get the basic data that I have to do my report and its takes about 50 min for SQL finish. Now, with these data, I need to create 3 different reports and I can't use the SQL even since there are a lot of aggregation (example would be product group in one case and by customer in 2nd). For each of these different group garages I need another report.

    So how to create 3 reports of 1 SQL query without running the query 3 times?

    First thing that comes to mind is to store the result set in a fictitious table, and then query the table since I get the basic data are about 300 lines and then perform different garages group.

    Best regards

    Igor

    So how to create 3 reports of 1 SQL query without running the query 3 times?

    You already know the obvious answer - store data 'somewhere '.

    If any 'somewhere' depends on your needs and you have not provided ALL the.

    MV - if the query is always the same, you might use a MV and make a complete refresh when you want that new data. The data are permanent and can be queried by other sessions, but the query that accesses the data is frozen in the definition of MV.

    GTT (global temporary table) - If a NEW charge of data AND three reports will be ALWAYS executed by a single session and then data are no longer necessary so a TWG may work. The application that loads the TWG can be different for each race, but the data won't be available for a single session and ONLY for the duration of this session. So if something goes wrong and the session ends the data are missing.

    First thing that comes to mind is to store the result set in a fictitious table, and then query the table since I get the basic data are about 300 lines and then perform different garages group.

    Which is commonly called a "table of REPORT-READY." Those that are useful when data must be permanent and available for multiple sessions/users. Generally, there is a batch (for example the package procedure) that periodically refreshes / updates the data during a window of failure. Or the table can have a column (for example AS_OF) that allows it to contain multiple data sets and the update process let alone the existing data and creates a new set of data.

    If your database is about 300 lines you can consider a table report and even use it to contain multiple data sets. Then, the reports can be written to query the data by using a value AS_OF that wraps and returns the appropriate data. You don't need a window of failure since the oldest data are still available (but can be removed when you no longer need.

    If you need a set of data, you can use a partitioned table work (with only one partition) to collect the new set of data, then a SWAP PARTITION to 'swap' in the new data. Only, this "Exchange" takes a fraction of a second and avoids a window of failure. Once the swap done no matter what user query will get new data.

  • Need for SQL query to get the result.

    Region

    MonthTrx typeSummary of the resolution
    AMERICAS-13 mayAdjustmentsFix
    EMEA-13 mayAdjustmentsIncorrect
    AMERICAS-13 mayCredit memoIncorrect
    EMEA-13 mayInvoiceFix
    AMERICAS-13 mayCredit memoFix
    OFD-13 mayAdjustmentsFix
    AMERICAS-13 mayInvoiceIncorrect
    DVL-13 mayAdjustmentsFix
    DVL-13 mayAdjustmentsFix
    OFD-13 mayAdjustmentsFix

    Above my Table and here is the result required. Similarly for other regions as well. Can someone help me with the SQL query?

    RegionSummary of the resolutionSettingCredit memoInvoiceTotal general
    AMERICASFix112
    Incorrect0112

    Like this?

    SQL > select * from transaction_audit;

    MTH TRX_TYPE REGION BOARD
    -------- ------ ----------- ---------
    AMERICAS-13 may SETTINGS CORRECT
    EMEA-13 may INCORRECT ADJUSTMENT
    AMERICAS-13 may CREDIT MEMO INCORRECT
    EMEA-13 may INVOICE CORRECT
    AMERICAS-13 may CREDIT MEMO CORRECT
    OFD-13 may SETTINGS CORRECT
    AMERICAS-13 may INVOICE INCORRECTE
    LAD-13 may SETTINGS CORRECT
    LAD-13 may SETTINGS CORRECT
    OFD-13 may SETTINGS CORRECT

    10 selected lines.

    SQL > select region
    2, resolution_summary
    3, count (decode (trx_type, "ADJUSTMENTS", trx_type)) adjustments
    4, County (decode (trx_type, 'HAVING', trx_type)) credit_memo
    5, County (decode (trx_type, "BILL", trx_type)) Bill
    transaction_audit 6
    Group 7
    8 by region
    9, resolution_summary
    10 Decree
    11 by region
    12, resolution_summary
    13.

    REGION TAKE ADJUSTMENTS CREDIT_MEMO INVOICE
    -------- --------- ----------- ----------- ----------
    CORRECT THE AMERICAS 1 1 0
    0 1 1 INCORRECT AMERICAS
    EMEA CORRECT 0 0 1
    INCORRECT EMEA 1 0 0
    DAL ADDRESS 2 0 0
    OFD CORRECT 2 0 0

    6 selected lines.

    SQL >

  • How do I rotate my result of a query from a line to a SQL column?

    Hey, guys:

    Is it possible that I can rotate my result of a query from a line to a SQL column?

    It's a certain type of pivot example
    Instead of
    DEPTNO DNAME
    -------- ---------------
    10 accounting
    It would be
    DEPTNO: 10
    DNOM: ACCOUNTING

    Hello

    When you have N columns to rank 1 and you want to display in the form of 1 column on several lines, it's called Unpivoting . Here's a way to do it:

    WITH     cntr     AS
    (
         SELECT     LEVEL     AS n
         FROM     dual
         CONNECT BY     LEVEL <= 2     -- number of columns to be unpivoted
    )
    SELECT     CASE c.n
             WHEN  1  THEN  'DEPTNO'
             WHEN  2  THEN  'DNAME'
         END || ':'        AS label
    ,     CASE c.n
             WHEN  1  THEN  TO_CHAR (d.deptno)
             WHEN  2  THEN  d.dname
         END || ':'        AS val
    ,     d.deptno               -- PK, if needed
    FROM         cntr        c
    CROSS JOIN  scott.dept  d
    WHERE   deptno = 10               -- or whatever
    ;
    

    The query above works in Oracle 9.1 or more.
    From Oracle 11.1, you can also use the SELECT... Function of the UNPIVOT operator.

  • Need help to write a SQL query complex

    I have the source tabe as below

    -> SOURCE_TABLE
    NAME     CUST_ID     SVC_ST_DT     SVC_END_DT 
    TOM        1               31/08/2009      23/03/2011 
    DOCK       2               01/01/2004      31/05/2010 
    HARRY      3               28/02/2007      31/12/2009 
    I want to load as target table below
    -> TARGET_TABLE
    NAME     CUST_ID                     SVC_ST_DT      SVC_END_DT 
    TOM      1           31/08/2009      31/12/2009 
    TOM      1           01/01/2010      31/12/2010 
    TOM      1           01/01/2011      23/03/2011 
    DOCK      2           01/01/2004      31/12/2004 
    DOCK      2           01/01/2005      31/12/2005 
    DOCK      2           01/01/2006      31/12/2006 
    DOCK      2           01/01/2007      31/12/2007 
    DOCK      2           01/01/2008      31/12/2008 
    DOCK      2           01/01/2009      31/12/2009 
    DOCK      2           01/01/2010      31/05/2010 
    HARRY      3           28/02/2007      31/12/2007 
    HARRY      3           01/01/2008      31/12/2008 
    HARRY      3           01/01/2009      31/12/2009 
    Is it possible to write a SQL query that returns the data in the same way above the target table.

    Published by: AChatterjee on April 30, 2012 07:14

    Published by: AChatterjee on April 30, 2012 07:14

    Or like this...

    SQL> ed
    Wrote file afiedt.buf
    
      1  with t as (select 'TOM' as NAME, 1 as CUST_ID, date '2009-08-31' as SVC_ST_DT, date '2011-03-23' as SVC_END_DT from dual union all
      2             select 'DOCK', 2, date '2004-01-01', date '2010-05-31' from dual union all
      3             select 'HARRY', 3, date '2007-02-28', date '2009-12-31' from dual)
      4  --
      5  -- end of test data
      6  --
      7  select name, cust_id, svc_st_dt, svc_end_dt
      8  from (
      9        select name
     10              ,cust_id
     11              ,greatest(svc_st_dt, add_months(trunc(svc_st_dt,'YYYY'),yr*12)) as svc_st_dt
     12              ,least(svc_end_dt, add_months(trunc(svc_st_dt,'YYYY'),(yr+1)*12)-1) as svc_end_dt
     13        from t
     14             cross join (select rownum-1 as yr
     15                         from   dual
     16                         connect by rownum <= (select extract(year from max(svc_end_dt)) - extract(year from min(svc_st_dt)) + 1 from t)
     17                        )
     18       )
     19  where svc_st_dt <= svc_end_dt
     20* order by 2, 3
    SQL> /
    
    NAME     CUST_ID SVC_ST_DT            SVC_END_DT
    ----- ---------- -------------------- --------------------
    TOM            1 31-AUG-2009 00:00:00 31-DEC-2009 00:00:00
    TOM            1 01-JAN-2010 00:00:00 31-DEC-2010 00:00:00
    TOM            1 01-JAN-2011 00:00:00 23-MAR-2011 00:00:00
    DOCK           2 01-JAN-2004 00:00:00 31-DEC-2004 00:00:00
    DOCK           2 01-JAN-2005 00:00:00 31-DEC-2005 00:00:00
    DOCK           2 01-JAN-2006 00:00:00 31-DEC-2006 00:00:00
    DOCK           2 01-JAN-2007 00:00:00 31-DEC-2007 00:00:00
    DOCK           2 01-JAN-2008 00:00:00 31-DEC-2008 00:00:00
    DOCK           2 01-JAN-2009 00:00:00 31-DEC-2009 00:00:00
    DOCK           2 01-JAN-2010 00:00:00 31-MAY-2010 00:00:00
    HARRY          3 28-FEB-2007 00:00:00 31-DEC-2007 00:00:00
    HARRY          3 01-JAN-2008 00:00:00 31-DEC-2008 00:00:00
    HARRY          3 01-JAN-2009 00:00:00 31-DEC-2009 00:00:00
    
    13 rows selected.
    
  • How export (unload) result of the query without running query on sql developer

    Hello

    I want to know how export (unload) result of the query without running query on sql developer.

    I know this way.
    1. execution of query
    2. click on "Download" on the results tab
    http://i.stack.imgur.com/CQ4Qb.jpg


    Is he available unload a query result before the race?

    No, but you can do this ask the developer SQL change however, for other users can vote and add weight to the possible future implementation.

    Kind regards
    K.

Maybe you are looking for