Forum Stats

  • 3,769,695 Users
  • 2,253,009 Discussions
  • 7,875,155 Comments

Discussions

I'm having trouble filtering display

User_AC3QE
User_AC3QE Member Posts: 34 Red Ribbon
edited Mar 17, 2021 9:14PM in SQL & PL/SQL

I'm having trouble trying not to display the line 'NOT ISOLATED' when there is more than one item in the "ds_motivo" column, with the same code in the "cd_setor" column and the same date in the "dt_referencia" column



Best Answer

  • mathguy
    mathguy Member Posts: 10,157 Blue Diamond
    Accepted Answer

    OK, I think I understand.

    Assuming your existing query works without error, and produces the output you posted: You want to show rows for pairs of (DT_REFERENCIA, CD_SETOR), but with the following conditions: If DS_MOTIVO is anything other than 'NOT ISOLATED', include the row in the output. However, if DS_MOTIVO is 'NOT ISOLATED', then don't include the row unless there are no other rows for the same (DT_REFERENCIA, CD_SETOR) with DS_MOTIVO different from 'NOT ISOLATED'.

    If this is correct: Find the main SELECT clause in your existing query, and add the following column (new - it doesn't exist in your code yet):

    ..., count(case when ds_motivo != 'NOT ISOLATED' then 1 end) 
           over (partition by dt_referencia, cd_setor) as ct_ds_motivo
    


    Then make your entire query - including this extra column - into a subquery in the WITH clause. I assume you understand how to do that - if you don't, write back.

    In the final query, select again all the columns you need, FROM this last subquery in the WITH clause, and add

    where ct_ds_motivo = 0 or ds_motivo != 'NOT ISOLATED'
    


    First try to make this query work. When it does, look again at your data, and/or think about it and discuss it with your users. Can DS_MOTIVO ever be NULL? If it can, how should that be handled? What I suggested above handles it in one particular way, which may or may not be what you need. (Of course, if the column is NOT NULL, then this is moot.)

    User_AC3QE

Answers

  • mathguy
    mathguy Member Posts: 10,157 Blue Diamond

    You jumped to the code too soon. First describe in detail the problem you are having - the real-life, business issue you are trying to solve.

    What "dates" are you referring to? (Do you mean "data", not "dates"?)

    What do you mean by "remove items"? "Remove" is not a technical term in SQL, so you must explain what you mean. Same with "item".

    And don't say "read the code, you will understand". For one thing, the code DOESN'T do what it is supposed to do (otherwise you wouldn't ask a question here in the first place). So it is not possible to guess what it was supposed to do, when it doesn't even do that. Second, the code may try the wrong approach anyway. And third, it is your job to tell us exactly what the problem is - not to ask us to make the effort to guess the problem from the code.

  • Frank Kulash
    Frank Kulash Member, Moderator Posts: 41,229 Red Diamond
    edited Feb 11, 2021 7:20PM

    Hi, @User_AC3QE

    Whenever you have a question, please post a little sample data in a usable form (such as CREATE TABLE and INSERT statements, relevant columns only), so the people who want to help you car re-create the problem and test their ideas. Also post the exact results you want from the given data, and an explanation (in general terms) of you get those results from that data.

    Simplify the problem, if possible. For example, if your query involves five tables, but you already know how to join them, and the problem only concerns excluding some rows from the result set, then perhaps you can post CREATE TABLE and INSERT statements for a single table that looks like that result set. Just make sure the simplified version shows what the real problem is.

    Always say which version of Oracle you're using (e.g. 12.2.0.1.0).

    See: How to Ask Questions in Developer Community Spaces - oracle-tech

  • User_AC3QE
    User_AC3QE Member Posts: 34 Red Ribbon


    it would be like filtering the select, I can't generate create table similar at the moment

    oracle 12c

  • Frank Kulash
    Frank Kulash Member, Moderator Posts: 41,229 Red Diamond

    Hi,

    I can't generate create table similar at the moment

    No problem; I'm not in a hurry. I'll try to help you whenever you can generate the CREATE TABLE and INSERT statements, and post the other things needed.

    When you do post them, post them in a new reply; don't just edit your original message. That way it will be easier for people to follow the thread, and harder for them to miss any changes.

  • mathguy
    mathguy Member Posts: 10,157 Blue Diamond
    Accepted Answer

    OK, I think I understand.

    Assuming your existing query works without error, and produces the output you posted: You want to show rows for pairs of (DT_REFERENCIA, CD_SETOR), but with the following conditions: If DS_MOTIVO is anything other than 'NOT ISOLATED', include the row in the output. However, if DS_MOTIVO is 'NOT ISOLATED', then don't include the row unless there are no other rows for the same (DT_REFERENCIA, CD_SETOR) with DS_MOTIVO different from 'NOT ISOLATED'.

    If this is correct: Find the main SELECT clause in your existing query, and add the following column (new - it doesn't exist in your code yet):

    ..., count(case when ds_motivo != 'NOT ISOLATED' then 1 end) 
           over (partition by dt_referencia, cd_setor) as ct_ds_motivo
    


    Then make your entire query - including this extra column - into a subquery in the WITH clause. I assume you understand how to do that - if you don't, write back.

    In the final query, select again all the columns you need, FROM this last subquery in the WITH clause, and add

    where ct_ds_motivo = 0 or ds_motivo != 'NOT ISOLATED'
    


    First try to make this query work. When it does, look again at your data, and/or think about it and discuss it with your users. Can DS_MOTIVO ever be NULL? If it can, how should that be handled? What I suggested above handles it in one particular way, which may or may not be what you need. (Of course, if the column is NOT NULL, then this is moot.)

    User_AC3QE