Filtering data where one field is above value depending on another field criteria












1















Not sure how to title this (therefore didn't get lucky when trying to search for an answer), but let's say I've got a table as follows:



Product Quality Source
P1 0.80 Provider1
P1 0.75 Provider2
P1 0.20 Provider3
P1 0.83 Provider4
P1 0.60 Provider5
P2 0.45 Provider1
P2 0.76 Provider2
P2 0.20 Provider3
P2 0.87 Provider4
P3 0.45 Provider1
P3 0.30 Provider2
P3 0.84 Provider4
P3 0.24 Provider5


and I only want to keep products that are either Provider1 or Provider3 or has a higher quality than the best of qualities of that product on Provider1 or Provider3. So I'd like to end up with:



Product Quality Source
P1 0.80 Provider1
P1 0.20 Provider2
P1 0.83 Provider4
P2 0.45 Provider1
P2 0.76 Provider2
P2 0.20 Provider3
P2 0.87 Provider4
P3 0.45 Provider1
P3 0.84 Provider4


I've got quite a bit of data so I'm looking for a smart way to do this.



Brute force would be something like:



select * from mytable m1 where m1.Source = 'Tier1' or m1.Source > (select max(m2.Quality) from mytable m2 where m2.Product = m1.Product and m2.Source = 'Tier1')



Any suggestion?



Thanks



Edit1: I had simplified the problem slightly by omitting that each product could have multiple sources, I've now amended my post.



Using Littlefoot's suggestion:



with inter as 
(
select m1.product, m1.quality, m1.source,
(select max(m2.quality) from mytable m2
where m2.product = m1.product
and m2.source in ('Provider1', 'Provider3')
) max_quality
from mytable m1
)
select product, quality, source from inter
where source in ('Provider1', 'Prodiver3')
or quality > max_quality;


I will need to see if that is fast enough when I run the request on the whole table (~hundreds of thousands of rows).










share|improve this question





























    1















    Not sure how to title this (therefore didn't get lucky when trying to search for an answer), but let's say I've got a table as follows:



    Product Quality Source
    P1 0.80 Provider1
    P1 0.75 Provider2
    P1 0.20 Provider3
    P1 0.83 Provider4
    P1 0.60 Provider5
    P2 0.45 Provider1
    P2 0.76 Provider2
    P2 0.20 Provider3
    P2 0.87 Provider4
    P3 0.45 Provider1
    P3 0.30 Provider2
    P3 0.84 Provider4
    P3 0.24 Provider5


    and I only want to keep products that are either Provider1 or Provider3 or has a higher quality than the best of qualities of that product on Provider1 or Provider3. So I'd like to end up with:



    Product Quality Source
    P1 0.80 Provider1
    P1 0.20 Provider2
    P1 0.83 Provider4
    P2 0.45 Provider1
    P2 0.76 Provider2
    P2 0.20 Provider3
    P2 0.87 Provider4
    P3 0.45 Provider1
    P3 0.84 Provider4


    I've got quite a bit of data so I'm looking for a smart way to do this.



    Brute force would be something like:



    select * from mytable m1 where m1.Source = 'Tier1' or m1.Source > (select max(m2.Quality) from mytable m2 where m2.Product = m1.Product and m2.Source = 'Tier1')



    Any suggestion?



    Thanks



    Edit1: I had simplified the problem slightly by omitting that each product could have multiple sources, I've now amended my post.



    Using Littlefoot's suggestion:



    with inter as 
    (
    select m1.product, m1.quality, m1.source,
    (select max(m2.quality) from mytable m2
    where m2.product = m1.product
    and m2.source in ('Provider1', 'Provider3')
    ) max_quality
    from mytable m1
    )
    select product, quality, source from inter
    where source in ('Provider1', 'Prodiver3')
    or quality > max_quality;


    I will need to see if that is fast enough when I run the request on the whole table (~hundreds of thousands of rows).










    share|improve this question



























      1












      1








      1








      Not sure how to title this (therefore didn't get lucky when trying to search for an answer), but let's say I've got a table as follows:



      Product Quality Source
      P1 0.80 Provider1
      P1 0.75 Provider2
      P1 0.20 Provider3
      P1 0.83 Provider4
      P1 0.60 Provider5
      P2 0.45 Provider1
      P2 0.76 Provider2
      P2 0.20 Provider3
      P2 0.87 Provider4
      P3 0.45 Provider1
      P3 0.30 Provider2
      P3 0.84 Provider4
      P3 0.24 Provider5


      and I only want to keep products that are either Provider1 or Provider3 or has a higher quality than the best of qualities of that product on Provider1 or Provider3. So I'd like to end up with:



      Product Quality Source
      P1 0.80 Provider1
      P1 0.20 Provider2
      P1 0.83 Provider4
      P2 0.45 Provider1
      P2 0.76 Provider2
      P2 0.20 Provider3
      P2 0.87 Provider4
      P3 0.45 Provider1
      P3 0.84 Provider4


      I've got quite a bit of data so I'm looking for a smart way to do this.



      Brute force would be something like:



      select * from mytable m1 where m1.Source = 'Tier1' or m1.Source > (select max(m2.Quality) from mytable m2 where m2.Product = m1.Product and m2.Source = 'Tier1')



      Any suggestion?



      Thanks



      Edit1: I had simplified the problem slightly by omitting that each product could have multiple sources, I've now amended my post.



      Using Littlefoot's suggestion:



      with inter as 
      (
      select m1.product, m1.quality, m1.source,
      (select max(m2.quality) from mytable m2
      where m2.product = m1.product
      and m2.source in ('Provider1', 'Provider3')
      ) max_quality
      from mytable m1
      )
      select product, quality, source from inter
      where source in ('Provider1', 'Prodiver3')
      or quality > max_quality;


      I will need to see if that is fast enough when I run the request on the whole table (~hundreds of thousands of rows).










      share|improve this question
















      Not sure how to title this (therefore didn't get lucky when trying to search for an answer), but let's say I've got a table as follows:



      Product Quality Source
      P1 0.80 Provider1
      P1 0.75 Provider2
      P1 0.20 Provider3
      P1 0.83 Provider4
      P1 0.60 Provider5
      P2 0.45 Provider1
      P2 0.76 Provider2
      P2 0.20 Provider3
      P2 0.87 Provider4
      P3 0.45 Provider1
      P3 0.30 Provider2
      P3 0.84 Provider4
      P3 0.24 Provider5


      and I only want to keep products that are either Provider1 or Provider3 or has a higher quality than the best of qualities of that product on Provider1 or Provider3. So I'd like to end up with:



      Product Quality Source
      P1 0.80 Provider1
      P1 0.20 Provider2
      P1 0.83 Provider4
      P2 0.45 Provider1
      P2 0.76 Provider2
      P2 0.20 Provider3
      P2 0.87 Provider4
      P3 0.45 Provider1
      P3 0.84 Provider4


      I've got quite a bit of data so I'm looking for a smart way to do this.



      Brute force would be something like:



      select * from mytable m1 where m1.Source = 'Tier1' or m1.Source > (select max(m2.Quality) from mytable m2 where m2.Product = m1.Product and m2.Source = 'Tier1')



      Any suggestion?



      Thanks



      Edit1: I had simplified the problem slightly by omitting that each product could have multiple sources, I've now amended my post.



      Using Littlefoot's suggestion:



      with inter as 
      (
      select m1.product, m1.quality, m1.source,
      (select max(m2.quality) from mytable m2
      where m2.product = m1.product
      and m2.source in ('Provider1', 'Provider3')
      ) max_quality
      from mytable m1
      )
      select product, quality, source from inter
      where source in ('Provider1', 'Prodiver3')
      or quality > max_quality;


      I will need to see if that is fast enough when I run the request on the whole table (~hundreds of thousands of rows).







      oracle






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 13 '18 at 9:12







      Will1v

















      asked Nov 12 '18 at 19:48









      Will1vWill1v

      5018




      5018
























          1 Answer
          1






          active

          oldest

          votes


















          1














          One option might be



          SQL> with test (product, quality, source) as
          2 (select 'P1', 0.8, 'Tier1' from dual union all
          3 select 'P2', 0.75, 'Tier2' from dual union all
          4 select 'P3', 0.2, 'Tier2' from dual union all
          5 select 'P4', 0.83, 'Tier2' from dual union all
          6 select 'P5', 0.6, 'Tier1' from dual
          7 ),
          8 inter as
          9 (select product, quality, source,
          10 (select max(quality) max_qual from test
          11 where source = 'Tier1'
          12 ) max_qual
          13 from test
          14 )
          15 select product, quality, source
          16 From inter
          17 where source = 'Tier1'
          18 or quality > max_qual;

          PR QUALITY SOURC
          -- ---------- -----
          P1 ,8 Tier1
          P4 ,83 Tier2
          P5 ,6 Tier1

          SQL>


          Or:



            <snip>
          8 inter as
          9 (select product, quality, source,
          10 max(case when source = 'Tier1' then quality else 0 end) over (order by null) max_qual
          11 from test
          12 )
          13 select product, quality, source
          14 from inter
          15 where source = 'Tier1'
          16 or quality > max_qual;

          PR QUALITY SOURC
          -- ---------- -----
          P1 ,8 Tier1
          P4 ,83 Tier2
          P5 ,6 Tier1

          SQL>





          share|improve this answer
























          • Thanks Littlefoot. I'm not sure I understand the first part. Why : ` with test (product, quality, source) as` ` (select 'P1', 0.8, 'Tier1' from dual union all` ` select 'P2', 0.75, 'Tier2' from dual union all` ` select 'P3', 0.2, 'Tier2' from dual union all` ` select 'P4', 0.83, 'Tier2' from dual union all` ` select 'P5', 0.6, 'Tier1' from dual` ` ),` rather than: ` with test (product, quality, source) as` ` (select product, quality, source from mytable)` (this would make sense because I actually have more than those 3 fields in mytable)

            – Will1v
            Nov 12 '18 at 20:37













          • Not sure how to make my comment above readable...

            – Will1v
            Nov 12 '18 at 20:41






          • 1





            I used CTE (a common-table-expression, the WITH factoring clause) to "simulate" a table. I used list of columns I saw in your example. As you didn't post CREATE TABLE nor INSERT INTO statements, I used CTE to get some data to be able to write a query which will return something. No problem if you have additional columns, if they don't affect the result.

            – Littlefoot
            Nov 12 '18 at 20:42











          • Thanks Littlefoot. I’ve edited my post and used your suggestion to try and answer the new version of the problem.

            – Will1v
            Nov 13 '18 at 9:21











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53269108%2ffiltering-data-where-one-field-is-above-value-depending-on-another-field-criteri%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          One option might be



          SQL> with test (product, quality, source) as
          2 (select 'P1', 0.8, 'Tier1' from dual union all
          3 select 'P2', 0.75, 'Tier2' from dual union all
          4 select 'P3', 0.2, 'Tier2' from dual union all
          5 select 'P4', 0.83, 'Tier2' from dual union all
          6 select 'P5', 0.6, 'Tier1' from dual
          7 ),
          8 inter as
          9 (select product, quality, source,
          10 (select max(quality) max_qual from test
          11 where source = 'Tier1'
          12 ) max_qual
          13 from test
          14 )
          15 select product, quality, source
          16 From inter
          17 where source = 'Tier1'
          18 or quality > max_qual;

          PR QUALITY SOURC
          -- ---------- -----
          P1 ,8 Tier1
          P4 ,83 Tier2
          P5 ,6 Tier1

          SQL>


          Or:



            <snip>
          8 inter as
          9 (select product, quality, source,
          10 max(case when source = 'Tier1' then quality else 0 end) over (order by null) max_qual
          11 from test
          12 )
          13 select product, quality, source
          14 from inter
          15 where source = 'Tier1'
          16 or quality > max_qual;

          PR QUALITY SOURC
          -- ---------- -----
          P1 ,8 Tier1
          P4 ,83 Tier2
          P5 ,6 Tier1

          SQL>





          share|improve this answer
























          • Thanks Littlefoot. I'm not sure I understand the first part. Why : ` with test (product, quality, source) as` ` (select 'P1', 0.8, 'Tier1' from dual union all` ` select 'P2', 0.75, 'Tier2' from dual union all` ` select 'P3', 0.2, 'Tier2' from dual union all` ` select 'P4', 0.83, 'Tier2' from dual union all` ` select 'P5', 0.6, 'Tier1' from dual` ` ),` rather than: ` with test (product, quality, source) as` ` (select product, quality, source from mytable)` (this would make sense because I actually have more than those 3 fields in mytable)

            – Will1v
            Nov 12 '18 at 20:37













          • Not sure how to make my comment above readable...

            – Will1v
            Nov 12 '18 at 20:41






          • 1





            I used CTE (a common-table-expression, the WITH factoring clause) to "simulate" a table. I used list of columns I saw in your example. As you didn't post CREATE TABLE nor INSERT INTO statements, I used CTE to get some data to be able to write a query which will return something. No problem if you have additional columns, if they don't affect the result.

            – Littlefoot
            Nov 12 '18 at 20:42











          • Thanks Littlefoot. I’ve edited my post and used your suggestion to try and answer the new version of the problem.

            – Will1v
            Nov 13 '18 at 9:21
















          1














          One option might be



          SQL> with test (product, quality, source) as
          2 (select 'P1', 0.8, 'Tier1' from dual union all
          3 select 'P2', 0.75, 'Tier2' from dual union all
          4 select 'P3', 0.2, 'Tier2' from dual union all
          5 select 'P4', 0.83, 'Tier2' from dual union all
          6 select 'P5', 0.6, 'Tier1' from dual
          7 ),
          8 inter as
          9 (select product, quality, source,
          10 (select max(quality) max_qual from test
          11 where source = 'Tier1'
          12 ) max_qual
          13 from test
          14 )
          15 select product, quality, source
          16 From inter
          17 where source = 'Tier1'
          18 or quality > max_qual;

          PR QUALITY SOURC
          -- ---------- -----
          P1 ,8 Tier1
          P4 ,83 Tier2
          P5 ,6 Tier1

          SQL>


          Or:



            <snip>
          8 inter as
          9 (select product, quality, source,
          10 max(case when source = 'Tier1' then quality else 0 end) over (order by null) max_qual
          11 from test
          12 )
          13 select product, quality, source
          14 from inter
          15 where source = 'Tier1'
          16 or quality > max_qual;

          PR QUALITY SOURC
          -- ---------- -----
          P1 ,8 Tier1
          P4 ,83 Tier2
          P5 ,6 Tier1

          SQL>





          share|improve this answer
























          • Thanks Littlefoot. I'm not sure I understand the first part. Why : ` with test (product, quality, source) as` ` (select 'P1', 0.8, 'Tier1' from dual union all` ` select 'P2', 0.75, 'Tier2' from dual union all` ` select 'P3', 0.2, 'Tier2' from dual union all` ` select 'P4', 0.83, 'Tier2' from dual union all` ` select 'P5', 0.6, 'Tier1' from dual` ` ),` rather than: ` with test (product, quality, source) as` ` (select product, quality, source from mytable)` (this would make sense because I actually have more than those 3 fields in mytable)

            – Will1v
            Nov 12 '18 at 20:37













          • Not sure how to make my comment above readable...

            – Will1v
            Nov 12 '18 at 20:41






          • 1





            I used CTE (a common-table-expression, the WITH factoring clause) to "simulate" a table. I used list of columns I saw in your example. As you didn't post CREATE TABLE nor INSERT INTO statements, I used CTE to get some data to be able to write a query which will return something. No problem if you have additional columns, if they don't affect the result.

            – Littlefoot
            Nov 12 '18 at 20:42











          • Thanks Littlefoot. I’ve edited my post and used your suggestion to try and answer the new version of the problem.

            – Will1v
            Nov 13 '18 at 9:21














          1












          1








          1







          One option might be



          SQL> with test (product, quality, source) as
          2 (select 'P1', 0.8, 'Tier1' from dual union all
          3 select 'P2', 0.75, 'Tier2' from dual union all
          4 select 'P3', 0.2, 'Tier2' from dual union all
          5 select 'P4', 0.83, 'Tier2' from dual union all
          6 select 'P5', 0.6, 'Tier1' from dual
          7 ),
          8 inter as
          9 (select product, quality, source,
          10 (select max(quality) max_qual from test
          11 where source = 'Tier1'
          12 ) max_qual
          13 from test
          14 )
          15 select product, quality, source
          16 From inter
          17 where source = 'Tier1'
          18 or quality > max_qual;

          PR QUALITY SOURC
          -- ---------- -----
          P1 ,8 Tier1
          P4 ,83 Tier2
          P5 ,6 Tier1

          SQL>


          Or:



            <snip>
          8 inter as
          9 (select product, quality, source,
          10 max(case when source = 'Tier1' then quality else 0 end) over (order by null) max_qual
          11 from test
          12 )
          13 select product, quality, source
          14 from inter
          15 where source = 'Tier1'
          16 or quality > max_qual;

          PR QUALITY SOURC
          -- ---------- -----
          P1 ,8 Tier1
          P4 ,83 Tier2
          P5 ,6 Tier1

          SQL>





          share|improve this answer













          One option might be



          SQL> with test (product, quality, source) as
          2 (select 'P1', 0.8, 'Tier1' from dual union all
          3 select 'P2', 0.75, 'Tier2' from dual union all
          4 select 'P3', 0.2, 'Tier2' from dual union all
          5 select 'P4', 0.83, 'Tier2' from dual union all
          6 select 'P5', 0.6, 'Tier1' from dual
          7 ),
          8 inter as
          9 (select product, quality, source,
          10 (select max(quality) max_qual from test
          11 where source = 'Tier1'
          12 ) max_qual
          13 from test
          14 )
          15 select product, quality, source
          16 From inter
          17 where source = 'Tier1'
          18 or quality > max_qual;

          PR QUALITY SOURC
          -- ---------- -----
          P1 ,8 Tier1
          P4 ,83 Tier2
          P5 ,6 Tier1

          SQL>


          Or:



            <snip>
          8 inter as
          9 (select product, quality, source,
          10 max(case when source = 'Tier1' then quality else 0 end) over (order by null) max_qual
          11 from test
          12 )
          13 select product, quality, source
          14 from inter
          15 where source = 'Tier1'
          16 or quality > max_qual;

          PR QUALITY SOURC
          -- ---------- -----
          P1 ,8 Tier1
          P4 ,83 Tier2
          P5 ,6 Tier1

          SQL>






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 12 '18 at 19:53









          LittlefootLittlefoot

          20.8k71433




          20.8k71433













          • Thanks Littlefoot. I'm not sure I understand the first part. Why : ` with test (product, quality, source) as` ` (select 'P1', 0.8, 'Tier1' from dual union all` ` select 'P2', 0.75, 'Tier2' from dual union all` ` select 'P3', 0.2, 'Tier2' from dual union all` ` select 'P4', 0.83, 'Tier2' from dual union all` ` select 'P5', 0.6, 'Tier1' from dual` ` ),` rather than: ` with test (product, quality, source) as` ` (select product, quality, source from mytable)` (this would make sense because I actually have more than those 3 fields in mytable)

            – Will1v
            Nov 12 '18 at 20:37













          • Not sure how to make my comment above readable...

            – Will1v
            Nov 12 '18 at 20:41






          • 1





            I used CTE (a common-table-expression, the WITH factoring clause) to "simulate" a table. I used list of columns I saw in your example. As you didn't post CREATE TABLE nor INSERT INTO statements, I used CTE to get some data to be able to write a query which will return something. No problem if you have additional columns, if they don't affect the result.

            – Littlefoot
            Nov 12 '18 at 20:42











          • Thanks Littlefoot. I’ve edited my post and used your suggestion to try and answer the new version of the problem.

            – Will1v
            Nov 13 '18 at 9:21



















          • Thanks Littlefoot. I'm not sure I understand the first part. Why : ` with test (product, quality, source) as` ` (select 'P1', 0.8, 'Tier1' from dual union all` ` select 'P2', 0.75, 'Tier2' from dual union all` ` select 'P3', 0.2, 'Tier2' from dual union all` ` select 'P4', 0.83, 'Tier2' from dual union all` ` select 'P5', 0.6, 'Tier1' from dual` ` ),` rather than: ` with test (product, quality, source) as` ` (select product, quality, source from mytable)` (this would make sense because I actually have more than those 3 fields in mytable)

            – Will1v
            Nov 12 '18 at 20:37













          • Not sure how to make my comment above readable...

            – Will1v
            Nov 12 '18 at 20:41






          • 1





            I used CTE (a common-table-expression, the WITH factoring clause) to "simulate" a table. I used list of columns I saw in your example. As you didn't post CREATE TABLE nor INSERT INTO statements, I used CTE to get some data to be able to write a query which will return something. No problem if you have additional columns, if they don't affect the result.

            – Littlefoot
            Nov 12 '18 at 20:42











          • Thanks Littlefoot. I’ve edited my post and used your suggestion to try and answer the new version of the problem.

            – Will1v
            Nov 13 '18 at 9:21

















          Thanks Littlefoot. I'm not sure I understand the first part. Why : ` with test (product, quality, source) as` ` (select 'P1', 0.8, 'Tier1' from dual union all` ` select 'P2', 0.75, 'Tier2' from dual union all` ` select 'P3', 0.2, 'Tier2' from dual union all` ` select 'P4', 0.83, 'Tier2' from dual union all` ` select 'P5', 0.6, 'Tier1' from dual` ` ),` rather than: ` with test (product, quality, source) as` ` (select product, quality, source from mytable)` (this would make sense because I actually have more than those 3 fields in mytable)

          – Will1v
          Nov 12 '18 at 20:37







          Thanks Littlefoot. I'm not sure I understand the first part. Why : ` with test (product, quality, source) as` ` (select 'P1', 0.8, 'Tier1' from dual union all` ` select 'P2', 0.75, 'Tier2' from dual union all` ` select 'P3', 0.2, 'Tier2' from dual union all` ` select 'P4', 0.83, 'Tier2' from dual union all` ` select 'P5', 0.6, 'Tier1' from dual` ` ),` rather than: ` with test (product, quality, source) as` ` (select product, quality, source from mytable)` (this would make sense because I actually have more than those 3 fields in mytable)

          – Will1v
          Nov 12 '18 at 20:37















          Not sure how to make my comment above readable...

          – Will1v
          Nov 12 '18 at 20:41





          Not sure how to make my comment above readable...

          – Will1v
          Nov 12 '18 at 20:41




          1




          1





          I used CTE (a common-table-expression, the WITH factoring clause) to "simulate" a table. I used list of columns I saw in your example. As you didn't post CREATE TABLE nor INSERT INTO statements, I used CTE to get some data to be able to write a query which will return something. No problem if you have additional columns, if they don't affect the result.

          – Littlefoot
          Nov 12 '18 at 20:42





          I used CTE (a common-table-expression, the WITH factoring clause) to "simulate" a table. I used list of columns I saw in your example. As you didn't post CREATE TABLE nor INSERT INTO statements, I used CTE to get some data to be able to write a query which will return something. No problem if you have additional columns, if they don't affect the result.

          – Littlefoot
          Nov 12 '18 at 20:42













          Thanks Littlefoot. I’ve edited my post and used your suggestion to try and answer the new version of the problem.

          – Will1v
          Nov 13 '18 at 9:21





          Thanks Littlefoot. I’ve edited my post and used your suggestion to try and answer the new version of the problem.

          – Will1v
          Nov 13 '18 at 9:21


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53269108%2ffiltering-data-where-one-field-is-above-value-depending-on-another-field-criteri%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Full-time equivalent

          Bicuculline

          さくらももこ