How to select priority records

Hi gurus,

Here's the scenario of my problem. Records can be present/absent both in the table as follows.
ELEMENT ID            ELEMENT DETAILS                    SOURCE  

167               Some Details from S1-1          S1
167               Some Details from S1-2          S1
167               Some Details from S1-3          S1


167               Some Details from S2-1          S2
167               Some Details from S2-2          S2
167               Some Details from S2-3          S2

167               Some Details from S3-1          S3
167               Some Details from S3-2          S3
167               Some Details from S3-3          S3

                OR records can be present as follows

ELEMENT ID            ELEMENT DETAILS                    SOURCE  


167               Some Details from S2-1          S2
167               Some Details from S2-2          S2
167               Some Details from S2-3          S2

167               Some Details from S3-1          S3
167               Some Details from S3-2          S3
167               Some Details from S3-3          S3

             OR  records can be present as follows

ELEMENT ID            ELEMENT DETAILS                    SOURCE  

167               Some Details from S1-1          S1
167               Some Details from S1-2          S1
167               Some Details from S1-3          S1



167               Some Details from S3-1          S3
167               Some Details from S3-2          S3
167               Some Details from S3-3          S3

and so on ...
The result I want

If all the RECORDS in sources (S1, S2, S3) are present and to SELECT only data source S1
IF S2 and S3 RECORDS present and then SELECT only data S2.
IF S3 RECORDS present data and then SELECT S3
IF S1 and S3 RECORDS present data only SELECT S1
IF S1 and S2 RECORDS then present S1 only SELECT data

for example, S1 is on the highest priority, as appropriate and then S2 and S3 last.

How to write a select based on above logic.

Structure table data and the sample are less than
create table element_priority(element_id number,element_details varchar2(100),source varchar2(100) )      
       
insert into element_priority
select * from (
WITH T1 AS(
   SELECT    167, 'Some Details from S1-1',  'S1' from dual
   union
   SELECT    167, 'Some  Details from S1-2',  'S1' from dual
   union
   SELECT    167, 'Some Details from S1-3',  'S1' from dual
   union
   SELECT    167, 'Some Details from S2-1',   'S2' from dual
   union
   SELECT    167, 'Some Details from S2-2',   'S2' from dual
   union
   SELECT    167, 'Some Details from S2-3',   'S2' from dual
   union
    SELECT    167, 'Some Details from S3-1',   'S3' from dual
   union
   SELECT    167, 'Some Details from S3-2',   'S3' from dual
   union
   SELECT    167, 'Some Details from S3-3',   'S3' from dual
   )
   select * from   t1
)
Any help would be very appreciated

Thanks in advance

Use an analytic RANK(), DENSE_RANK() as ROW_NUMBER() according to what is required - perhaps DENSE_RANK or RANK what you have said and the possibility of having several #1 rankings in the same partition.

Not sure if the precise logic is clear, but something like this:

SQL> WITH T1(element_id, element_details, source) AS(
  2     SELECT    167, 'Some Details from S1-1',  'S1' from dual
  3     union
  4     SELECT    167, 'Some  Details from S1-2',  'S1' from dual
  5     union
  6     SELECT    167, 'Some Details from S1-3',  'S1' from dual
  7     union
  8     SELECT    167, 'Some Details from S2-1',   'S2' from dual
  9     union
 10     SELECT    167, 'Some Details from S2-2',   'S2' from dual
 11     union
 12     SELECT    167, 'Some Details from S2-3',   'S2' from dual
 13     union
 14      SELECT    167, 'Some Details from S3-1',   'S3' from dual
 15     union
 16     SELECT    167, 'Some Details from S3-2',   'S3' from dual
 17     union
 18     SELECT    167, 'Some Details from S3-3',   'S3' from dual
 19     )
 20  select element_id
 21  ,      element_details
 22  ,      source
 23  from   (select element_id
 24          ,      element_details
 25          ,      source
 26          ,      dense_rank() over (partition by element_id
 27                                    order by decode(source, 'S1',1,'S2',2,'S3',3)) rn            

 28          from   t1)
 29  where  rn = 1;

ELEMENT_ID ELEMENT_DETAILS         SO
---------- ----------------------- --
       167 Some  Details from S1-2 S1
       167 Some Details from S1-1  S1
       167 Some Details from S1-3  S1

Tags: Database

Similar Questions

  • How to select a record in one table to manipulate data in a database?

    Hello community,

    Using 32-bit Labview 2015.

    I created a user interface that runs a query and retrieves my table from a sql database.

    I want to be able to manage only one record at a time by selecting the record in my table and then manipulate the data according to the needs.

    Whenever the user runs a query, I want as the first record in the table to be selected / highlighted.

    I want an indicator that is currently active.

    Then a click of a button, to be able to scroll the actively selected record.

    Once I have the selected record, I want a way to write a query to edit or delete the record.

    Is attached a photo of my query to select all of my table and connect data in my table (results).

    Attached a photo of my painting showing the timeline of my sql database.

    What is the best way to go on a record selection in a table and the modification of data with a query?

    Any help would be greatly appreciated.

    Thank you

    I guess that's not a table, but multi-column listbox.

    Right click, select--> highlight whole line selection Mode

    The value of the multicolumn listbox is the row index - you can write a local variable to highlight the line, the event structure to handle the user clicking on, etc.

    If you enable the property editable ListBox cells, you can allow users to edit the items of the listbox.

    If you want to get the data in row, you hint REF table and work with the array of strings in row - convert it to cluster, etc..

    I don't know, what type of data, it is, how you structure the database looks like, I don't even know if you use the wrapper to work with the database, I can't write queries for you.

    Something like Update Tablica Set Serial = '5' where ID = '22';

  • How to select the record in the mulitple rows based on values in another field?

    Hello

    Sorry for the bad title, really don't know how to explain it

    Have the following data:
     
    Cost_center       Activity_type
    1005009401     CLBR0
    1005009401     CLBR1
    1005009401     TLBR0
    1005009401     TLBR1
    1005009401     VEH00
    
    1005009402     CLBR3
    1005009402     CLBR4
    1005009402     TLBR5
    1005009402     TLBR6
    1005009402     VEH07
    
    1005009901     CE000
    1005009901     CLBR0
    1005009901     CLBR1
    1005009901     TLBR0
    1005009901     TLBR1
    1005009901     VEH01
    I need to return a list of cost (with its associated activity types) centers have no activity types of CLBR0 and CLBR1 and TLBR0 and TLBR1 and VEH01. So in the above data, cost center, 1005009401, and 1005009402 will be returned.

    Any thoughts?

    Thank you.

    Published by: dgouin on August 29, 2012 11:42 - added several examples of data.

    Published by: dgouin on August 29, 2012 11:45

    Published by: dgouin on August 29, 2012 11:49

    Sorta kludgey, but functional:

    WITH ccs AS
      (
      SELECT '1005009401' AS CC, 'CLBR0' AS activity_type FROM dual
      UNION ALL
      SELECT '1005009401' AS CC, 'CLBR1' AS activity_type FROM dual
      UNION ALL
      SELECT '1005009401' AS CC, 'TLBR0' AS activity_type FROM dual
      UNION ALL
      SELECT '1005009401' AS CC, 'TLBR1' AS activity_type FROM dual
      UNION ALL
      SELECT '1005009401' AS CC, 'VEH00' AS activity_type FROM dual
      UNION ALL
      SELECT '1005009901' AS CC, 'CE000' AS activity_type FROM dual
      UNION ALL
      SELECT '1005009901' AS CC, 'CLBR0' AS activity_type FROM dual
      UNION ALL
      SELECT '1005009901' AS CC, 'CLBR1' AS activity_type FROM dual
      UNION ALL
      SELECT '1005009901' AS CC, 'TLBR0' AS activity_type FROM dual
      UNION ALL
      SELECT '1005009901' AS CC, 'TLBR1' AS activity_type FROM dual
      UNION ALL
      SELECT '1005009901' AS CC, 'VEH01' AS activity_type FROM dual
      )
    SELECT cc, activity_type
    FROM   ccs c1
    WHERE  5 != (SELECT COUNT(DISTINCT activity_type)
                 FROM   ccs c2
                 WHERE  activity_type IN ('CLBR0','CLBR1','TLBR0','TLBR1','VEH01')
                 AND    c1.cc = c2.cc
                );
    
    CC         ACTIVITY_TYPE
    ---------- -------------
    1005009401 CLBR0
    1005009401 CLBR1
    1005009401 TLBR0
    1005009401 TLBR1
    1005009401 VEH00  
    
  • How to select the values for each check box in a group of records

    Hello

    I have a requirement in form 10g. In this form there are 10 records are display each record has a checkbox is there if I click on the check box save number one and number three and make some changes in the text field (field adjustment is the number data type) and then finally, I want to see the total a total amount called field.

    In this, my question is how to select specific to a group of records records? and finally these selected records are inserted in a table.
    Because I am not able to read these records at a time.
    Is there any table to define a record group to fetch each of them individually for example Rec [1] Rec [2]... like this if yes please suggest me the steps how to do this.

    Thanks in advance
    Prasanna

    Published by: user10315107 on December 17, 2008 23:44

    OK, so you want to shoe the total amount in the form itself (I guess that somewhere under the dashboard lines?).

    You can do this easily using formulat elements:

    1 create a new item in your block where the field amount is places, set "section of the database" on the 'No', 'calculation mode' to the 'formula' and the 'formula' himself to something like:

    CASE WHEN :BLOCK.CHECKBOXITEM=CHECKVALUE THEN :BLOCK.AMOUNT+:BLOCK.ADJUSTMENT ELSE 0 END;
    

    This formula returns 0 if the checkboxitem is not checked, otherwise the sum of amount and adjustment (of course you can adjust the names of elements and the value for 'Checked')
    2. place the element in the layout, if you wish.
    3. set the property "Query all Records" to "true" for your block elements, this is necessary for the calculation to work
    3 create a control block to keep summary article in a, "Single Document" set to 'True '.
    4. place a new element in this control block, set 'Calcuation mode' to 'Summary', 'Summary block' to your block elements, 'Summary point' in newly created formula section in the block elements
    and function of synthesis for the "sum".
    5. place the element in the layout

    She's.

  • How to select records in the duplicate and two copies lines no rows

    Hello

    I'm working on Oracle with the following details
    Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - 64 bit Production
    With partitioning, OLAP and Data Mining options

    I need help to choose one of the duplicate records and a few duplicate not based on condition. I wrote the script that only gives me one of the record list duplicate, but I'm confused how select duplicate duplicates.

    My condition is to select all records where TARGET TABLE AND TARGET_COLUMN IS NOT NULL. I need to select unique records of this this condition based on these attributes only Anwar. _

    _ From the SOURCE

    TARGET_TABLE TARGET_COLUMN SRC_COLUMN
    -----
    --------------------
    -----
    SRC_EXTERNAL SUBJECT_AREA SUBJECT_AREA
    FISCAL_YEAR FISCAL_YEAR SRC_EXTERNAL
    PARTY_ID_ORG PARTY_ALT_ID PARTY_ID
    PARTY_ID_NEW PARTY_ALT_ID PARTY_ID
    ESTABLISHMENT_NBR_ORG W2_EMPLOYER ESTABLISHMENT_NBR
    ESTABLISHMENT_NBR_NEW W2_EMPLOYER ESTABLISHMENT_NBR
    EMPLOYMENT_CODE_ORG W2_EMPLOYER EMPLOYMENT_CODE
    EMPLOYMENT_CODE_NEW W2_EMPLOYER EMPLOYMENT_CODE
    SICK_PAY_IND_ORG W2_EMPLOYER SICK_PAY_IND
    SICK_PAY_IND_NEW W2_EMPLOYER SICK_PAY_IND
    AGENT_IND_CODE
    AGENT_FEIN
    EMPLOYER_NAME
    EMPLOYER_ADDRESS
    CLIENT_ADDRESS
    CITY
    STATE
    ZIP

    _ REQUIRED TARGET

    TARGET_TABLE TARGET_COLUMN SRC_COLUMN

    -----
    --------------------
    -----
    SICK_PAY_IND_NEW W2_EMPLOYER SICK_PAY_IND
    EMPLOYMENT_CODE_NEW W2_EMPLOYER EMPLOYMENT_CODE
    ESTABLISHMENT_NBR_NEW W2_EMPLOYER ESTABLISHMENT_NBR
    PARTY_ID_NEW PARTY_ALT_ID PARTY_ID
    SRC_EXTERNAL SUBJECT_AREA SUBJECT_AREA
    FISCAL_YEAR FISCAL_YEAR SRC_EXTERNAL

    MY QUERY_ +.


    SELECT NOMFICHIER_SRC, TARGET_TABLE, SRC_COLUMN, TARGET_COLUMN
    OF ITS_STAGE. STG_EDW_METADATA X
    WHERE TARGET_TABLE IS NOT NULL AND TARGET_COLUMN IS NOT NULL
    AND ROWID & gt; (SELECT MIN (ROWID)
    OF ITS_STAGE. STG_EDW_METADATA Y
    WHERE TARGET_TABLE IS NOT NULL AND TARGET_COLUMN IS NOT NULL
    AND Y.TARGET_TABLE = X.TARGET_TABLE
    AND Y.TARGET_COLUMN = X.TARGET_COLUMN)



    MY RESULTS+ _
    TARGET_TABLE TARGET_COLUMN SRC_COLUMN

    -----
    --------------------
    -----
    SICK_PAY_IND_NEW W2_EMPLOYER SICK_PAY_IND
    EMPLOYMENT_CODE_NEW W2_EMPLOYER EMPLOYMENT_CODE
    ESTABLISHMENT_NBR_NEW W2_EMPLOYER ESTABLISHMENT_NBR
    PARTY_ID_NEW PARTY_ALT_ID PARTY_ID

    Can some Oracle expert please help me solve this problem.

    Thanks in advance

    Rajesh

    Published by: user532468 on 15 Sep, 2008 14:32

    With the help of

    AND ROWID * > * (SELECT MIN (ROWID)

    you select duplicates. Just change of

    AND ROWID * = * (SELECT MIN (ROWID)

    This will select everyring but duplicates. However, it is not effective. All you need is:

    SELECT SRC_FILENAME,SRC_COLUMN,TARGET_TABLE, TARGET_COLUMN
    FROM (SELECT DISTINCT * FROM  ITS_STAGE.STG_EDW_METADATA X
    WHERE TARGET_TABLE IS NOT NULL AND TARGET_COLUMN IS NOT NULL)
    

    SY.

  • How to select record Datestamp Maximum

    It's data.

    The number Type Datestamp
    IM10134 001 was 10413 Open 2010-07-19 07:51:43.000
    IM10134 001 has 10424 Status Change-2010-08-02 09:45:27.000
    IM10134 001 A 10425 provider external assignment 2010-08-02 09:45:27.000
    IM10134 001 A 10426 update 2010-08-02 09:47:49.000

    I want results for the following fields. So the result is

    The number Type Datestamp
    IM10134 001 A 10426 update 2010-08-02 09:47:49.000

    How to select this folder only

    Kindly help me.

    You can try:

    SELECT x.number,y.thenumber,y.type,x.date_stamp
    FROM   (SELECT number,MAX(datestamp) date_stamp FROM table_name GROUP BY number) x,
    table_name y
    WHERE x.number = y.number
    AND x.date_stamp = y.datestamp;
    /
    
  • How to download and record a song on Firefox?

    When I want to download a song, it goes through flash player only, please tell me how to download and record a song on my phone using Firefox.

                              I will be thankful to you.
    

    long press flash and select Save audio

  • How to get the record with a tank of extra space

    Hi I have a value which have several entries with the same value but some record have more space, how to know which record


    Ex:


    'abc1234 '.

    'abc1234 '.

    'abc1234 '.


    I want to know 'abc1234' record


    in this table, I have more than 50 million records.

    Maybe this:

    SELECT s FROM T WHERE s LIKE '% '.

  • By selecting certain records by using the check box

    Hello

    I have a similar problem mentioned in this thread Re: select specific records using the checkbox I got if I have only one primary key value in the work table. I don't know how to handle it when I have several primary keys of the table I want to use for filtering?

    Thank you.

    Hello

    You can use rowid
    First report

    SELECT APEX_ITEM.CHECKBOX(1,e.rowid) chk,
    e.*
    FROM emp e
    

    Use even submit after processes.
    And target page report

    SELECT e.*
    FROM emp e
    WHERE EXISTS(
    SELECT 1
    FROM apex_collections c
    WHERE c.collection_name = 'P46COL'
    AND c.c001 = e.rowid)
    

    Kind regards
    Jari

    Published by: jarola on October 14, 2010 10:50

  • How to select valid dates only

    If I have a table with a column varchar2 as below, how to select records that contain only valid dates? As there is no 2010-04-31, so I need only to bring on 2010-04-30, which is a valid date.

    Select '2010-04-30'd from the double. "
    Union of all the
    Select '2010-04-31'd from the double. "

    I'm on Oracle 10.2

    Published by: Rinne on April 23, 2010 11:15

    Hello

    Tubby wrote:
    An option using SQL only, although something like that, I tend to go with the solution of francs to a user defined function. Because if you store dates as strings, you're likely to have more than just 1 kind of "bad" data in the string.

    If you try a pure-SQL solution, make sure that it does not raise an error when d is an absolute stupidity (for example, when d = "TOTAL GARBAGE").
    Do something like this:

    WHERE   CASE
              WHEN  d     IS NULL
              THEN  'Missing'
              WHEN  TRANSLATE ( d
                              , '012345678'
                        , '999999999'
                        ) != '9999-99-99'
              THEN  'Invalid format or non-numeric'
              WHEN  SUBSTR (d, 1, 4)     = '0000'     -- or test for implausible years, e.g. "< '1900'"
              THEN  'Invalid year'
              WHEN  SUBSTR (d, 6, 2)     NOT BETWEEN  '01'
                                                AND          '12'
              THEN  'Invalid month'
              WHEN  SUBSTR (d, 9, 2)     NOT BETWEEN  '01'
                                                AND          TO_CHAR ( LAST_DAY ( TO_DATE ( SUBSTR (d, 1, 7)     -- no need for "|| '01'"
                                                                          , 'YYYY-MM'
                                                        )
                                                 )
                                            , 'DD'
                                            )
              THEN  'Invalid day'
              WHEN  d     BETWEEN '1582-10-05'     -- If necessary
                         AND     '1582-10-14'
              THEN  'Julian-Gregorian calendar change'
              ELSE  'Okay'
         END     = 'Okay'
    

    In my opinion, it intercepts all possible errors in common dates of the time.

    Published by: Frank Kulash, April 23, 2010 12:57

  • How to create a record of pl/sql in oracle

    Oracle Forms 6i

    Hai All

    My question how to create a record with a cursor for loop.

    While I'm generating my if statement elsif only some of my files are not completely recovered, so I generate using the cursor for loop pls tell how to do

    My code is

    declare
    t_in dail_att.intime%type;
    t_out dail_att.intime%type;
    t_code dail_att.barcode%type;
    dail_att.attend_date%type V_DATE;

    cursor cur_test is
    Select respondent, outtime, barcode, dail_att attend_date;
    -in t_in, t_out, t_code, dail_att V_DATE;

    Start

    go_block ('TEST_SRI');
    PREMIER_ENREGISTREMENT;
    LOOP

    If: bartime between 0145 and then 0630
    Update dail_att set = outtime: bartime where barcode =: bar code
    and ATTEND_DATE =: BARDATE-1 and intimate are zero and outtime is not null;


    elsif: bartime between 0630 and 0900 or: bartime between 1130 and 1230 or
    : bartime between 1700 and 1800 and t_in is null then

    insert into dail_att(barcode,intime,attend_date)
    values(:Barcode,:Bartime,:bardate);

    elsif: bartime > 1645 and t_in is not null and t_out is null then
    Update dail_att set = outtime: bartime where barcode =: bar code
    and ATTEND_DATE =: BARDATE and respondent are not null and outtime has the value null.
    on the other

    Update dail_att set = outtime: bartime where barcode =: bar code
    and ATTEND_DATE =: BARDATE and respondent are not null and outtime has the value null.

    end if;
    WHEN THE OUTPUT: SYSTEM. LAST_RECORD = "TRUE" OR: BAR CODE IS NULL;
    NEXT_RECORD;
    END LOOP;
    forms_ddl ('commit');
    exception
    while others then
    forms_ddl ('rollback');
    message(SQLERRM|| dbms_error_Text);
    message(SQLERRM|| dbms_error_Text);
    End;


    Pls tell me if I need to create a folder or other...

    Concerning

    Srikkanth.M

    You can run a loop throgh query records of Pentecost an implicit cursor (which automatically manages the open cursor, fetch and next record):

      for rec in (select intime,outtime,barcode,attend_date from dail_att; )
      loop
    
           t_in :=rec.intime;
           t_out :=rec.outtime;
           t_code :=rec.barcode;
           t_date := attend_date;
    
        /*
    
             PUT YOUR CODE HERE ... (if ..then... elsif... logic)
             you can avoid setting variables 'cause you can directly use the implicit cursor variables: rec.intime etc.
    
         */
    
      end loop;
    END;
    

    In any case, I suggest you check the where the condition of your block... because if not all data is collected it should be a problem in your application... well, even the cursor (if it is based on the same query) will return less data than expected.

    Luca

  • LOBs and how to retrieve the record inserted with EMPTY_BLOb()

    Hi guys


    How to retrieve the records inserted with EMPTY_BLOb() instead of NULL value.


    Best greetings
    A.G.

    There must be a better way, but these work:

    SELECT... from tableX where length (lob_column) = 0;
    SELECT... from tableX where dbms_lob.getlength (lob_column) = 0;

    Concerning
    Jonathan Lewis
    http://jonathanlewis.WordPress.com
    http://www.jlcomp.demon.co.UK

    "All experts it is a equal and opposite expert."
    Clarke

  • How to select all of the people who are not in any team?

    Hello

    I have a nice SQL expression that gives me accordingly all the "AGENTS" (people) working in a TEAM (a team)

    Select one. "" AGENT_ID. "
    a."NAME" | ' ' || a.' first NAME' 'Name',
    c.libelle 'team ', he said.
    a."DATE_EMBAUCHE" "Hire Date"
    a."DATE_DEBAUCHE" «Date debauchery. "
    of the OBSERVATORY. "" AGENT. "
    b Observatoire.equipe_agents,
    c Observatoire.Equipe
    where a.agent_id = b.agent_id
    and b.equipe_id = c.equipe_id
    order by name

    Now, how to select all the agents who do NOT work in any 'TEAM' (team)?

    I tried but could not succeed!

    Thank you for your help.

    Christian

    Dave solutions use an outer join ((+) in the joints), which should bring back all the records of the agent, even if they do not exist in the equipe_agents table.

    Another approach would be to use a statement NOT EXISTS or NOT IN, for example

    SELECT   *
    FROM     agents a
    WHERE    NOT EXISTS (  SELECT   1
                           FROM     equipe_agents ea
                           WHERE    ea.agent_id = a.agent_id
                         )
    
  • How stop imagies to record traffic.

    How stop imagies to record traffic. I have only 200 MB per month. This is 38.0.1 version.
    Sorry for my languige? I'm not American.

  • How to select entire groups or to multiple recipients in MAIL, rather than one at a time?

    How to select entire groups or to multiple recipients in MAIL, rather than one at a time?

    One way is to use the app to create a group with the intended recipients. Then when composing the email just type the group name in the To: field.

Maybe you are looking for