Help needed on complex sql resolution

Hi all

I have a table such as indicated and wanted to insert these records in both tables and get the number of each type...
CREATE TABLE RETURN_DEV
(
  RCODE       VARCHAR2(4 BYTE),
  RNUMBER     VARCHAR2(16 BYTE),
  RDESC       VARCHAR2(40 BYTE),
  RDATE_TIME  VARCHAR2(16 BYTE),
  RQUANTITY   VARCHAR2(20 BYTE)
)


Insert into RETURN_DEV    (RCODE, RNUMBER, RDESC, RDATE_TIME, RQUANTITY)
 Values    ('0987', '919341035404586', 'POS:00003:RETR', '20100112112324', '1');
Insert into RETURN_DEV    (RCODE, RNUMBER, RDESC, RDATE_TIME, RQUANTITY)
 Values    ('0910', '929341035404586', 'POS:00003:REMR', '20100102112324', '1');
Insert into RETURN_DEV    (RCODE, RNUMBER, RDESC, RDATE_TIME, RQUANTITY)
 Values    ('0914', '939341035404586', 'POS:00003:REMR', '20100111112324', '1');
Insert into RETURN_DEV    (RCODE, RNUMBER, RDESC, RDATE_TIME, RQUANTITY)
 Values    ('0997', '357991027440360', 'POS:00003: WEXC', '20100102112324', '1');
Insert into RETURN_DEV    (RCODE, RNUMBER, RDESC, RDATE_TIME, RQUANTITY)
 Values    ('0997', '347991027440360', 'POS:00003: WEXC', '20100102112324', '1');
Insert into RETURN_DEV    (RCODE, RNUMBER, RDESC, RDATE_TIME, RQUANTITY)
 Values    ('0987', '949341035404586', 'POS:00003:SAXP', '20100512112324', '1');
Insert into RETURN_DEV    (RCODE, RNUMBER, RDESC, RDATE_TIME, RQUANTITY)
 Values    ('0956', '949341035404586', 'POS:00003:RETR', '20100212112324', '1');
Insert into RETURN_DEV    (RCODE, RNUMBER, RDESC, RDATE_TIME, RQUANTITY)
 Values    ('0968', '949341035404586', 'POS:00003:SAXP', '20100312112324', '1');
Insert into RETURN_DEV    (RCODE, RNUMBER, RDESC, RDATE_TIME, RQUANTITY)
 Values    ('0967', '997991027440360', 'POS:00003: USXP', '20060102112324', '-1');
Insert into RETURN_DEV    (RCODE, RNUMBER, RDESC, RDATE_TIME, RQUANTITY)
 Values    ('0967', '997991027440360', 'POS:00003: USXP', '20070102112324', '1');
Insert into RETURN_DEV    (RCODE, RNUMBER, RDESC, RDATE_TIME, RQUANTITY)
 Values    ('0967', '997991027440360', 'POS:00003: USXP', '20080102112324', '-1');
COMMIT;

Now my Algorithm go like this 

step 1: Load 'RET' type into Retail_Table and Count 'RET' type
If the RCODE  is not in [0910, 0914, 0997] and the RDESC begins with "POS:00003" and RQUANTITY =+1 load these as 'RET' type

Step2: Load   'REM' type type into Retail_Table and Count 'REM' type
If the RCODE  is in [0910, 0914] and RQUANTITY = +1, load these as 'REM' type

Step3: Load  'WEX' type into Retail_Table and Count 'WEX' type

If the RCODE  = 0997 and RQUANTITY = +1, load these as 'WEX' type

For me main problem starts here....

Step4: Load'RET' INTO  type  and 'RETXP' into retail_exceptions, and update the count of 'RET' Type and count 'RETXP' type

If the RCODE  is not in [0910, 0914, 0997] and the RDESC  begins with "POS:00003" 
and  if there is more than one record fitting that criteria in the file for the RNUMBER and the newest record chronologically (RDATE_TIME) has RQUANTITY = +1, 
then load the latest record as Retail Returns and load all other records into retail_exceptions table.

Step5: Load Retail Returns Exceptions as 'RETXP', update the count of Retail Returns  and add the count of exceptions

If the RCODE  is not in [0910, 0914, 0997] and the RDESC  begins with "POS:00003" 
and if there is more than one record fitting that criteria  for this RNUMBER and the newest record chronologically (RDATE_TIME)  has RQUANTITY = -1, 
then load all records into to the Retail exceptions table
I asssume we can get the account once the algorithm sucssefully inserts the date in the tables.
Both the table has the same structure as shown..

CREATE TABLE Retail_Table
(
  RCODE       VARCHAR2(4 BYTE),
  RNUMBER     VARCHAR2(16 BYTE),
  RDESC       VARCHAR2(40 BYTE),
  RDATE_TIME  VARCHAR2(16 BYTE),
  RTYPE   VARCHAR2(20 BYTE)
)


CREATE TABLE Retail_Table_Exceptions
(
  RCODE       VARCHAR2(4 BYTE),
  RNUMBER     VARCHAR2(16 BYTE),
  RDESC       VARCHAR2(40 BYTE),
  RDATE_TIME  VARCHAR2(16 BYTE),
  RTYPE   VARCHAR2(20 BYTE)
)
Thanks in advance...

Published by: Rede on October 19, 2010 16:20

Hello

Sorry, I don't understand still.

Rede says:
Thanks Frank.I thought to explain clearly, but it became huge, then I'll be more clear and precise...

I expect the output as shown below

Output Sample data of Retail Table....

RDESC            RQUANTITY     RNUMBER     TRANSACTION_TYPE

POS:00003: USXP     -1     997991027440360     RET
POS:00003: WEXC     1     347991027440360     WEX
POS:00003: WEXC     1     357991027440360     WEX
POS:00003:REMR     1     929341035404586     REM
POS:00003:REMR     1     939341035404586     REM

out put sample data of Retail_Exception Table

RDESC            RQUANTITY     RNUMBER     TRANSACTION_TYPE
POS:00003: USXP     -1     997991027440360     RETXP
POS:00003: USXP     1     997991027440360     RETXP
POS:00003:RETR     1     949341035404586     RETXP
POS:00003:SAXP     1     949341035404586     RETXP

CREATE orders TABLE for the retail and retail_exception do not have a rquantity column. If they have this column?
Are the other columns (rcode and rdate_time) of retail and retail_exception supposed to be NULL at this point?

What does each line in these tables?
Why there are 9 rows in both tables combined, instead of 11?

What i want is individaul counts of all trasaction types.If we can get the counts at time of DML querry it will be good.
But i want to keep them in a local variable thats the reason i want to do it in a seperate sql querry.

as shown below  

Transaction_Type    Count
RETXP                    4
WEX                      2
RET                       2

Please explain how you get these results.
Why is the number 2 when transaction_type = "RET"? Why not 1?
Why don't you want a line to transaction_type = "REM"?

These are all numeric values only. since i am getting these values from a external table i kept them as Varchar's.
But i will change them accordingly.
What i am trying to do is i grouping the items on the different conditions given below

1. When RCODE In ('0997')            And Rquantity = '1'   Then 'WEX'

2. When RCODE In ('0910', '0914') And Rquantity = '1'    Then 'REM'

3. When RCODE Not  In ('0997', '0910', '0914')   And RDesc Like 'POS:00003%'
And Count ( * ) Over (Partition By RNUMBER) = 1 Then  'RET'

its easy till above...but if the third condition has more than 1 record and if the top most record has 

4.    Rquantity = '-1' then categorise all the records  'RETXP' of that type   ( that satisfies the third condition)   else if top most record
5.    Rquantity = '1' then first record should be categorised as 'RET' and the remaining of that type as 'RETXP'.

top most is the record that is sorted by date for a given  RNumber.

What is the "first record"? Is it the same as the 'highest '?

Please give specific examples of how you get the desired results of the sample data that you posted.

For example, how do you get the output line:

RDESC               RQUANTITY  RNUMBER           TRANSACTION_TYPE
---------------   ---------  ---------------  ----------------
POS:00003: USXP       -1          997991027440360  RET

This line of output appears to be the 'top most' line for rnumber = '997991027440360':

RCODE RNUMBER          RDESC              RDATE_TIME       RQUANTITY
----- ---------------- ------------------ ---------------- ---------
0967  997991027440360  POS:00003: USXP    20060102112324   -1
0967  997991027440360  POS:00003: USXP    20070102112324   1
0967  997991027440360  POS:00003: USXP    20080102112324   -1

Rule 4 does not apply to this rnumber? Isn't the line with rdate_time = '20060102112324' the line ' top more? It is not rquantity = '-1'?

You can do the simple CASE expression (and probably faster) some of the functions of calculation only once, in a subquery, like this:

WITH     got_analytics     AS
(
     SELECT     rd.*
     ,     COUNT (*)           OVER ( PARTITION BY  rnumber)     AS rnumber_cnt
     ,     FIRST_VALUE (rquantity) OVER ( PARTITION BY  rnumber
                                ORDER BY          rdate_time
                              )                    AS top_rquantity
     ,     ROW_NUMBER ()          OVER ( PARTITION BY  rnumber
                                ORDER BY          rdate_time
                              )                    AS seq_number
     FROM     return_dev     rd
)
SELECT     rdesc
,      rquantity
,      rnumber
,     CASE
            WHEN  rcode      IN ('0997')
            AND   rquantity      = '1'               THEN     'WEX'
            WHEN  rcode           IN ('0910', '0914')
            AND   rquantity      = '1'                THEN     'REM'
            WHEN  rcode          IN ('0997', '0910', '0914')
            AND   rdesc      NOT LIKE 'POS:00003%'     THEN     NULL
            WHEN  rnumber_cnt     = 1               THEN     'RET'
         WHEN  top_rquantity     = '-1'               THEN     'RETXP'
         WHEN  top_rquantity     != '1'               THEN     NULL
         WHEN  seq_number     = 1               THEN     'RET'
                                   ELSE     'RETXP'
     END     AS transaction_type
FROM     got_analytics
;

I know that's not what you want, but I think that it is a step in the right direction.

Tags: Database

Similar Questions

  • HELP: Need to code SQL to bulk move

    I use Oracle 11.2, there are two tables tb_base, tb_hist as follows:

    tb_base (number (5) id, varchar2 (16) tag, the timestamp pavers (6));
    tb_hist (id number (5), histTs timestamp (6), timestamp (6) the cobblestones, tag varchar2 (16));

    tb_base 1, "beard", 20/12/2012
    1, 'bbb', 08/12/2012
    1, "ccc", 12/30/2012
    2, 'aaa', 11/29/2012
    2, 'vvv', 28/11/2012

    All I need is to move the record iteration with the same id from tb_base to tb_hist as a result:

    tb_base 1, "ccc", 12/30/2012
    2, 'aaa', 12/29/2012

    tb_hist 1, "beard", 20/12/2012, 20/12/2012
    1, "bbb", 2012/12/08, 08/12/2012
    2, "vvv", 2012/11/28, 28/11/2012

    Any suggestion on the SQL to perform this task?

    Thank you

    Copy the historical records of TB_HIST:

    INSERT INTO tb_hist
    (id
    ,histts
    ,setts
    ,tag)
    SELECT id,
           setts,
           setts,
           tag
    FROM   tb_base a
    WHERE EXISTS (SELECT 1
                  FROM  (SELECT id,
                                MAX(setts) OVER (PARTITION BY id) max_setts
                         FROM   tb_base) b
                  WHERE  b.id         = a.id
                  AND    b.max_setts != a.setts)
    

    Remove the historical records of TB_BASE:

    DELETE tb_base a
    WHERE EXISTS (SELECT 1
                  FROM  (SELECT id,
                                MAX(setts) OVER (PARTITION BY id) max_setts
                         FROM   tb_base) b
                  WHERE  b.id         = a.id
                  AND    b.max_setts != a.setts)
    
  • [help needed] Several insert - SQL statement

    Hello

    This is my table:

    {code}

    create table test

    (

    Identification number,

    DS varchar (6).

    start_date date

    );

    {code}

    There is data in the table.

    {code}

    Insert test values (1, 'DS1982', TO_DATE ('01.03.2014 ',' DD.)) MM YYYY'));

    Insert test values (2, 'DS1982', TO_DATE ('01.04.2014 ',' DD.)) MM YYYY'));

    Insert test values (3, 'DS1982', TO_DATE ('01.05.2014 ',' DD.)) MM YYYY'));

    Insert test values (4, 'DS1982', TO_DATE ('01.06.2014 ',' DD.)) MM YYYY'));

    Insert test values (1, 'DS1999', TO_DATE ('11.04.2014 ',' DD.)) MM YYYY'));

    Insert test values (2, 'DS1999', TO_DATE ('11.05.2014 ',' DD.)) MM YYYY'));

    Insert test values (3, 'DS1999', TO_DATE ('11.06.2014 ',' DD.)) MM YYYY'));

    Insert test values (4, 'DS1999', TO_DATE ('11.07.2014 ',' DD.)) MM YYYY'));

    Insert test values (1, 'DS0009', TO_DATE ('21.01.2014 ',' DD.)) MM YYYY'));

    Insert test values (2, 'DS0009', TO_DATE ('21.02.2014 ',' DD.)) MM YYYY'));

    Insert test values (3, 'DS0009', TO_DATE ('21.03.2014 ',' DD.)) MM YYYY'));

    {code}

    I have to query all records from the table that has:

    Max (start_date) whitin ID and DS culoms.

    So, I have to insert in the record in the table for each DS to condition:

    Each record will be inserted with incremented id whithin DS

    each record inserted must be "newer" than previous 1 month.

    Integration goes up to to_char (sysdate, mm) = to_char (start_date, 'mm').

    Resoult for DS1982 has so (records to insert):

    Insert test values (5, 'DS1982', TO_DATE ('01.07.2014 ',' DD.)) MM YYYY'));

    Insert test values (6, 'DS1982', TO_DATE ('01.08.2014 ',' DD.)) MM YYYY'));

    Insert test values (7, 'DS1982', TO_DATE ('01.09.2014 ',' DD.)) MM YYYY'));

    Insert test values (8, 'DS1982', TO_DATE ('01.10.2014 ',' DD.)) MM YYYY'));

    Insert test values (9, 'DS1982', TO_DATE ('01.11.2014 ',' DD.)) MM YYYY'));

    Insert test values (10, 'DS1982', TO_DATE ('01.12.2014 ',' DD.)) MM YYYY'));

    Insert test values (11, 'DS1982', TO_DATE ('01.01.2015 ',' DD.)) MM YYYY'));

    Insert test values (12, 'DS1982', TO_DATE ('01.02.2015 ',' DD.)) MM YYYY'));

    Insert test values (13, 'DS1982', TO_DATE ('01.03.2015 ',' DD.)) MM YYYY'));

    Insert test values (14, 'DS1982', TO_DATE ('01.04.2015 ',' DD.)) MM YYYY'));

    all together for DS1982

    Insert test values (1, 'DS1982', TO_DATE ('01.03.2014 ',' DD.)) MM YYYY'));

    Insert test values (2, 'DS1982', TO_DATE ('01.04.2014 ',' DD.)) MM YYYY'));

    Insert test values (3, 'DS1982', TO_DATE ('01.05.2014 ',' DD.)) MM YYYY'));

    Insert test values (4, 'DS1982', TO_DATE ('01.06.2014 ',' DD.)) MM YYYY'));

    Insert test values (5, 'DS1982', TO_DATE ('01.07.2014 ',' DD.)) MM YYYY'));

    Insert test values (6, 'DS1982', TO_DATE ('01.08.2014 ',' DD.)) MM YYYY'));

    Insert test values (7, 'DS1982', TO_DATE ('01.09.2014 ',' DD.)) MM YYYY'));

    Insert test values (8, 'DS1982', TO_DATE ('01.10.2014 ',' DD.)) MM YYYY'));

    Insert test values (9, 'DS1982', TO_DATE ('01.11.2014 ',' DD.)) MM YYYY'));

    Insert test values (10, 'DS1982', TO_DATE ('01.12.2014 ',' DD.)) MM YYYY'));

    Insert test values (11, 'DS1982', TO_DATE ('01.01.2015 ',' DD.)) MM YYYY'));

    Insert test values (12, 'DS1982', TO_DATE ('01.02.2015 ',' DD.)) MM YYYY'));

    Insert test values (13, 'DS1982', TO_DATE ('01.03.2015 ',' DD.)) MM YYYY'));

    Insert test values (14, 'DS1982', TO_DATE ('01.04.2015 ',' DD.)) MM YYYY'));

    I hope someone can help me with this problem!

    Thank you!

    This...

    SELECT the id + id LEVEL,

    DS,

    Start_date ADD_MONTHS (start_date, Level)

    Of

    (SELECT ds,

    Max (ID) id,

    Date_début Max (start_date)

    OF the test

    GROUP BY ds)

    CONNECT BY LEVEL<=>

    AND PREREQUISITES ds = ds

    AND PRIOR DBMS_RANDOM. VALUE IS NOT NULL.

    Thank you

    Ann

  • I use Windows Vista and that you need to download SQL Server 2005 Express Service Pack 4. I get an error code of 64 C. Please tell me what to do?

    I use Windows Vista and that you need to download SQL Server 2005 Express Service Pack 4. I get an error code 64 C help, please.

    Hello

    I suggest you to try the steps below and check if it helps.

    Method 1: Try to reset components to update Windows from the following link, which would help you to install the updates of windows. http://support.Microsoft.com/kb/971058

    Method 2: If the problem persists, then try to set up your computer in a clean boot state, and check to see if the same problem happens.

    By setting your boot system minimum state helps determine if third-party applications or startup items are causing the problem.

    How to troubleshoot a problem by performing a clean boot in Windows Vista or Windows 7:
    http://support.Microsoft.com/kb/929135

    Note: After the boot minimum troubleshooting step, follow step 7 in the link provided to return the computer to a Normal startup mode.

    Method 3: Also try to install the updates manually from the link below.

    http://www.Microsoft.com/download/en/details.aspx?displaylang=en&ID=184

    You can also consult the following link for more information:

    Troubleshoot problems with installing updates.

    http://Windows.Microsoft.com/en-us/Windows-Vista/troubleshoot-problems-with-installing-updates

    Hope this information is useful.

  • Unable to play the video due to the incompatible program and need to change the resolution

    Hi, I just bought BBC Get Active in French.  I live in France and I have Windows 8.1.  Video on this CD does not work, or not the speech, and he tells me that the program is incompatible. Having said that, also, I need to change the resolution of screen 1600 x 900, that only about 800 x 600 but when I do the screen becomes too small and the program returns to a large screen at 800 x 600.  He mentions the help of Vista or XP but as Windows 8.1 is more advanced I expect him to be just as compatible.  I changed the country of origin of the CD in the UK and hoped that that would solve the problem but no luck.

    Does anyone have a solution for me please?  I'm a child of 74 years, and not quite abreast with the technical jargon so its simplest form would be appreciated.

    Meg

    Original title: BBC GET INTO WINDOWS FRENCH INCOMPATIBILITY 8.1?

    Sunday,

    Hello again, just to make it count that I finally solved myself the problem by reinstalling QuickTime 6 in version 7, but I think the problem was to DO NOT to install it via the program itself, but to install itself as a separate installation, I think I read this somewhere in the course of my research, but at this point I could not remember which way I had installed it.  Hope this will help someone else in the future.

    Kind regards

    Meg

  • Help needed on the export of the Photo App

    I'm a photo hobbyest with thousands of photos stored in my Mac using the Photos app. I already saved them on Time Machine and iCloud. However, I try to upload some on an external hard drive but can't.  The Photo app continues to CHANGE the resolution of my images when I export them.  Here is what happens...

    • I have uploaded a JPEG of 5 MB in the Photo app earlier this year.
    • Now, I'm ready to take a copy of this picture on my external hard drive.
    • If I just move the original photo to the Photo app on my counter top, the resolution GOES from 5MB to 2.1 MB.
    • If I export 'The Original unmodified' I lose all the changes that I made in the Photo app.
    • If I export the 'High' quality jpeg image, it DROPS my resolution of 2.1 MB.
    • If I export the photo to the "Maximum" quality jpeg, it INCREASES my resolution to 14.3 MB!
    • I found NO WAY to download my original photo size!

    Question... How to export or copy an image from within the app of the Photo on an external hard drive with the resolution of the EXACT SAME as it is?

    Thank you!

    You first need to understand the resolution - the file size is not resolution - the resolution concerns the dimensions in pixels of the image - file to a JPEG file size is determined by the compression you choose - compression of the image does not change the dimensions in pixels of all - compression removes the donnΘes form the pictures and all JPEG in theory is inferior finial an uncompressed image as TIFF - but for practically all the uses, it does not matter

    When you export a modified version of the photo you create a new file that contains the image and the size of the files is determined by the selections you make and has no resolution at all to the original file

    Photos has limited control over file size - ir you want a file size, then export a copy of very high quality (maximum or even a TIFF uncompressed) and use another program that gives you more control to compress in the JPEG compression you want

    But at the end of the day the size of the file is really no different and it is very likely that the big JPEG will give you the same results as an uncompressed TIFF

    LN

  • When I right click of my mouse I do not get the properties option. I need to increase the resolution to fit a bigger screen.

    When I right click of my mouse I do not get the properties option. I need to increase the resolution to fit a bigger screen.

    It's not real estate in Vista customize. Then look at the last option.

  • Please can someone help, need password CNU9497P2C

    I have the same problem and its driving me crazy... I need the BIOS password because I can't access to this

    [number of Series edited by Moderator]

    Please can someone help, need password

    HP MINI CQ10

    Sweb try.

    e9lovox27e

    3rd letter tiny L.

    4th and 6th letter lowercase o.

    Use this code to go into the BIOS.

    Disable all passwords that are enabled.

    If demand for CURRENT password using this code.

    Request NEW password just press ENTER.

    If asked to hit just to CHECK password to enter.

    Save and exit.

    REO

    I must inform you that these services are not endorsed by HP, and that HP is not responsible for any damages that may occur to your system using these services. Please be aware that you do so at your own risk.

  • Need a logic sql to insert from one table to another

    I have a table ( TABLEA ) as follows:

    type_id level

    -------------

    1       7

    2       4

    I would like to insert above values in another table ( TABLEB ) through DML so it looks like so:

    seq_id type_id name    order level

    -----------------------------------

    1     1       display  1     7

    2     1       header   2    

    3     1       detail   3    

    4     2       display  1     4

    5          2          header    2

    6         2           detail      3   

    Looks like a very complex sql is necessary, for which I have no knowledge of :-).

    Hello

    97a5a09d-76e1-4fc7-8113-80b4705be7f4 wrote:

    I have a table ( TABLEA ) as follows:

    type_id level 
    -------------
    
    1       7 
    2       4 
    

    I would like to insert above values in another table ( TABLEB ) through DML so it looks like so:

    seq_id type_id name    order level 
    -----------------------------------
    
    1     1       display  1     7 
    2     1       header   2     
    3     1       detail   3
    4     2       display  1     4
    5          2          header    2
    6         2           detail      3    
    

    Looks like a very complex sql is necessary, for which I have no knowledge of :-).

    So, for each line in tablea, you want to INSERT 3 rows in tableb, is that at the time?

    One way to do that is to tablea to join of a table (or the result set) with 3 rows, like cntr below:

    INSERT INTO tableb (seq_id type_id, row_name, row_order, lvl)

    WITH cntr AS

    (

    SELECT LEVEL AS row_order

    OF the double

    CONNECT BY LEVEL<=>

    )

    SELECT ROW_NUMBER () OVER (ORDER BY a.type_id

    c.row_order

    ) - or sequence_name. NEXTVAL

    a.type_id

    Case c.row_order

    WHEN 1 THEN 'display '.

    WHEN 2 THEN 'head '.

    WHEN 3 THEN «details»

    END

    c.row_order

    Case c.row_order

    WHEN 1 THEN a.lvl

    END

    FROM tablea a

    CROSS JOIN cntr c

    ;

    If you would care to post CREATE TABLE and INSERT for your tables (as it exists before the UPDATE), then I could test this.

    NAME is not a very good column name, but it is nowhere near as bad as the ORDER or LEVEL.  I called ROW_NAME, ROW_ORDER and LVL columns instead.

  • Please, help me to query SQL Construct.


    Hi Experts,

    Could you please help me to query SQL Construct.  Please find the details

    HOSTNAME HOSTTYPE DEM R1 R2
    RS123 P ABC 24.5 265,5

    RS123 P CYC 24.5 265,5

    RS123 P ADDS 24.5 265,5

    RS123 P ADE 24.5 265,5

    RS123 P SRC 24.5 265,5

    EXPECTED RESULTS

    HOSTNAME      HOSTTYPE                              MNE                                                                      R1          R2
    RS123 P ABC, CYC, ADD, ADE, CBC 24.5 265,5

    Concerning

    See you soon

    with t as)

    Select "RS123' hostname 'P' hosttype, 'ABC' dem, 24.5 r1, r2 265,5 Union double all the

    Select 'RS123', 'P', "CYC", 24.5, 265,5 double Union all

    Select 'RS123', 'P', 'ADD', 24.5, 265,5 double Union all

    Select "RS123', 'P', 'ADE', 24.5, 265,5 double Union all

    Select 'RS123', 'P', 'SRC', 24.5, 265,5 double

    )

    Select the host name,

    HostType,

    RTrim (XMLAGG (XmlElement(e,MNE,','). (Extract ('//Text ()')), ',') DEM,.

    R1,

    R2

    t

    Group hostname,

    HostType,

    R1,

    R2

    /

    HOSTN H DEM R1 R2
    ----- - -------------------- ---------- ----------
    RS123 P ABC, CBC, ADE, ADD, CYC 24.5 265,5

    SQL >

    SY.

  • help on editing a SQL

    DELETE FROM my_table t1
        WHERE rowid NOT IN ( SELECT min(rowid) 
                             FROM my_table t2
                             GROUP BY key_value1 );
    I want to delete duplicates of records in MY_TABLE and table itself contains about 45 million documents. KEY_VALUE1 is whole and indexed.

    Can someone help me change this SQL to make it faster and more efficient?

    Appreciated.

    For very large delete operations, it is generally faster to insert values to keep in another table. Then truncate the original table and rename or reinsert the maintained values.

    example not tested

    insert into t1_temp
    select * from my_table t1
    WHERE rowid IN ( SELECT min(rowid)
                             FROM my_table t2
                             GROUP BY key_value1 );
    
    truncate table t1;
    
    insert into t1 select * from t1_temp;
    
    truncate table t1_temp;
    

    Be aware that a truncate command will do an implicit validation. So do not run without good test.

    Published by: Sven w. March 9, 2011 19:21

  • 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.
    
  • Need help: complex SQL statement

    Select segment3, 0 "other income."
    , 0 "Sales Services".
    , 0 "staff costs".
    , 0 "other staff costs".
    , 0 'Deputy '.
    , 0 'trip '.
    , 0 "collaborator".
    , 0 'training '.
    , 0 'capital '.
    , nvl (round (sum (nvl(PERIOD_ACTIVITY_A,0) + nvl(PERIOD_ACTIVITY_E,0) + nvl(PRDOPEN_A,0) + nvl(PRDOPEN_E,0)), 0), 0) "Overhead"
    , 0 "financial expenses".
    of PAN. SNP_OP_DETAIL@OFA
    where segment3 = x.new_cc and period_name = p_period
    and segment2 in (select acc_t from the CRP_REPORT_ACCT_DETAIL_T where group_id = 10)
    Group of segment3

    Please, I do not understand select segment3, 0 'other income'... There is no column called 0, what is the 0s in the select statement?

    Secondly, I don't understand PAN. SNP_OP_DETAIL@OFA

    Means says:
    Select segment3, 0 "other income."
    , 0 "Sales Services".
    , 0 "staff costs".
    , 0 "other staff costs".
    , 0 'Deputy '.
    , 0 'trip '.
    , 0 "collaborator".
    , 0 'training '.
    , 0 'capital '.
    , nvl (round (sum (nvl(PERIOD_ACTIVITY_A,0) + nvl(PERIOD_ACTIVITY_E,0) + nvl(PRDOPEN_A,0) + nvl(PRDOPEN_E,0)), 0), 0) "Overhead"
    , 0 "financial expenses".
    of PAN. SNP_OP_DETAIL@OFA
    where segment3 = x.new_cc and period_name = p_period
    and segment2 in (select acc_t from the CRP_REPORT_ACCT_DETAIL_T where group_id = 10)
    Group of segment3

    Please, I do not understand select segment3, 0 'other income'... There is no column called 0, what is the 0s in the select statement?

    0 'other income' means to select a literal value of 0 (zero) with an alias of "other income". double quotes preserve the space and upper/lower case in the alias. This alias (in space) will be difficult to use because of the so called space later.

    Secondly, I don't understand PAN. SNP_OP_DETAIL@OFA

    PAN is probably a schema, SNP_OP_DETAIL a table or view and @OFA there is a link of database indicating the table in a different database

  • Help Needed in SQL query

    Hi all

    Required clarification of Oracle sql

    Example table:

    EmpNo empname mgr_id hire_date deptno salary Job

    7788 SCOTT ANALYST 7566 3000 APRIL 19, 87 20
    7902 7566 3000 3 DECEMBER ANALYST FORD 81 20
    7934 MILLER COMMITTED JANUARY 7782 1300 23 82 10
    7900 7698 DECEMBER 3, CLERK JAMES 81 950 30
    7369 SMITH COMMITTED 7902 17 DECEMBER 20 800 80
    7876 ADAMS 7788 1100 23 MAY CLERK 87 20


    Need of ' single / one ' sql for this statement of the obligation:

    There will be 2 dropdown boxes (1 - work list, 2nd - empno) in the form in which the next set of results is expected

    (1) when the user selects the value of the 1st drop down box (employment) as "ANALYST", leaving the second dropdown not selected, the expected result is 2 (No. lines for this work)
    (2) when the user selects the value of the 1st drop-down list (job) as "ANALYST" and the value in the 2nd drop-down box like 7902, the expected result is 1 (no lines for this job and empno)

    SQLs I tried on my side (given below) gave the expected result and do please help me fix this

    Select count (1) in the scott.emp where job = 'ANALYST' and (empno =: empno or empno is null);

    Please help for this requirement. Any help is deeply appreciated.

    Thank you
    Zakari

    1007144 wrote:

    SQLs I tried on my side (given below) gave the expected result and do please help me fix this

    Select count (1) in the scott.emp where job = 'ANALYST' and (empno =: empno or empno is null);

    You check "IS NULL" on EMPNO clolumn. You should check on the value passed: EMPNO. It should be

    select count(1) from scott.emp where job='ANALYST' and ( empno = :empno or :empno is null ) ;
    

    Published by: JAC on May 21, 2013 19:32
    Hope that the: value of EMPLOYMENT is required...

  • Need help in writing the SQL CASE or DECODE statement

    Hi experts,

    I need to write a SQL select order_num, cntry_cde, prod_id and Qty by joining order_num on PROD_ORDER and PROD_ORDER_TXT.

    Here is my sample data

    PROD ORDER_
    order_num Prod_id Qty cntry_cde
    100 US A1 5
    101 US 10 A2
    102 IN A3 4
    103 TO THE A4 9
    104. IN A5 3

    PROD ORDER_TXT_
    cntry_cde Prod_id order_num
    100 US A1
    101 US A2
    102 NZ A3
    103 TO THE A4
    A5 104

    Here's the requirement,

    (1) if the cntry_cde in PROD_ORDER is the same as cntry_cde in PROD_ORDER_TXT then select PROD_ORDER.cntry_cde (orders 100, 101, 103)
    (2) if they are different, choose PROD_ORDER_TXT (order 102, to THE NZ <>) country code
    (3) if they are different and PROD_ORDER_TXT.cntry_cde is null, I can't use it like cntry_cde in my report (order 104). It happenend just because of bad data to the source.
    I can't avoid it. Then just use the cntry_cde of PROD_ORDER

    Expected results
    100 US A1 5
    101 US 10 A2
    102 NZ A3 4 - changed in NZ
    103 TO THE A4 9
    104. IN A5 3 - IN retained as PROD_ORDER_TXT.cntry_cde is null


    instructions for creation and insertion of sample table are lower than

    create the table prod_order
    (number of order_num,
    cntry_cde CHAR (2),
    prod_id VARCHAR2 (6).
    number of quantity)

    create the table prod_order_txt
    (number of order_num,
    cntry_cde CHAR (2),
    prod_id VARCHAR2 (6))

    insert into prod_order values (100, 'WE', 'A1', 5);
    insert into prod_order values (101, 'WE', 'A2', 1);
    insert into prod_order values (102, ' to THE ', 'A3', 4);
    insert into prod_order values (103, ' to THE ', 'A4', 9);
    insert into prod_order values (104, 'IN', 'A5', 3);



    insert into prod_order_txt values (100, 'WE', 'A1');
    insert into prod_order_txt values (101, 'WE', 'A2');
    insert into prod_order_txt values (102, 'NZ', 'A3');
    insert into prod_order_txt values (103, ' to THE ', 'A4');
    insert into prod_order_txt values (104, 'A5', NULL);

    commit;

    Thank you for your help in advance

    Published by: manon March 28, 2012 13:39

Maybe you are looking for