Select suppliers for each contract that have all codes required by the contract












0















I have a table of contracts that have "requirement codes" (COXA) and a table of suppliers that have "approval codes" (VNDAPP). Contracts can have any number of requirements and suppliers can have any number of approvals.



Example data:



Contract Requirement (COXA):
CONTR REQMT
7736 1
7736 10
7737 1
7737 4
7737 6
7738 5
7739 1

Supplier Approval (VNDAPP):
VNDNO REQMT
10019 1
10020 1
10020 2
10020 10
10021 1
10021 4
10021 5
10021 6

Desired Result:
CONTR VNDNO
7736 10020
7737 10021
7738 10021
7739 10019
7739 10020
7739 10021


In another question I have received a response that works when I specify the contract number in the query:



select sa.supplierid
from supplier_approval sa
where sa.approvalid IN (
select cr.requirementid
from contracts_requirement cr
where cr.contractid = 7736
)
group by sa.supplierid
having count(distinct sa.approvalid) = (
select count(*)
from contracts_requirement cr
where cr.contractid = 7736
)


The problem is I need to have matching suppliers for every contract number.



Thanks in advance!










share|improve this question





























    0















    I have a table of contracts that have "requirement codes" (COXA) and a table of suppliers that have "approval codes" (VNDAPP). Contracts can have any number of requirements and suppliers can have any number of approvals.



    Example data:



    Contract Requirement (COXA):
    CONTR REQMT
    7736 1
    7736 10
    7737 1
    7737 4
    7737 6
    7738 5
    7739 1

    Supplier Approval (VNDAPP):
    VNDNO REQMT
    10019 1
    10020 1
    10020 2
    10020 10
    10021 1
    10021 4
    10021 5
    10021 6

    Desired Result:
    CONTR VNDNO
    7736 10020
    7737 10021
    7738 10021
    7739 10019
    7739 10020
    7739 10021


    In another question I have received a response that works when I specify the contract number in the query:



    select sa.supplierid
    from supplier_approval sa
    where sa.approvalid IN (
    select cr.requirementid
    from contracts_requirement cr
    where cr.contractid = 7736
    )
    group by sa.supplierid
    having count(distinct sa.approvalid) = (
    select count(*)
    from contracts_requirement cr
    where cr.contractid = 7736
    )


    The problem is I need to have matching suppliers for every contract number.



    Thanks in advance!










    share|improve this question



























      0












      0








      0


      0






      I have a table of contracts that have "requirement codes" (COXA) and a table of suppliers that have "approval codes" (VNDAPP). Contracts can have any number of requirements and suppliers can have any number of approvals.



      Example data:



      Contract Requirement (COXA):
      CONTR REQMT
      7736 1
      7736 10
      7737 1
      7737 4
      7737 6
      7738 5
      7739 1

      Supplier Approval (VNDAPP):
      VNDNO REQMT
      10019 1
      10020 1
      10020 2
      10020 10
      10021 1
      10021 4
      10021 5
      10021 6

      Desired Result:
      CONTR VNDNO
      7736 10020
      7737 10021
      7738 10021
      7739 10019
      7739 10020
      7739 10021


      In another question I have received a response that works when I specify the contract number in the query:



      select sa.supplierid
      from supplier_approval sa
      where sa.approvalid IN (
      select cr.requirementid
      from contracts_requirement cr
      where cr.contractid = 7736
      )
      group by sa.supplierid
      having count(distinct sa.approvalid) = (
      select count(*)
      from contracts_requirement cr
      where cr.contractid = 7736
      )


      The problem is I need to have matching suppliers for every contract number.



      Thanks in advance!










      share|improve this question
















      I have a table of contracts that have "requirement codes" (COXA) and a table of suppliers that have "approval codes" (VNDAPP). Contracts can have any number of requirements and suppliers can have any number of approvals.



      Example data:



      Contract Requirement (COXA):
      CONTR REQMT
      7736 1
      7736 10
      7737 1
      7737 4
      7737 6
      7738 5
      7739 1

      Supplier Approval (VNDAPP):
      VNDNO REQMT
      10019 1
      10020 1
      10020 2
      10020 10
      10021 1
      10021 4
      10021 5
      10021 6

      Desired Result:
      CONTR VNDNO
      7736 10020
      7737 10021
      7738 10021
      7739 10019
      7739 10020
      7739 10021


      In another question I have received a response that works when I specify the contract number in the query:



      select sa.supplierid
      from supplier_approval sa
      where sa.approvalid IN (
      select cr.requirementid
      from contracts_requirement cr
      where cr.contractid = 7736
      )
      group by sa.supplierid
      having count(distinct sa.approvalid) = (
      select count(*)
      from contracts_requirement cr
      where cr.contractid = 7736
      )


      The problem is I need to have matching suppliers for every contract number.



      Thanks in advance!







      sql db2 relational-division






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 2 at 17:40









      Salman A

      184k67343441




      184k67343441










      asked Jan 2 at 14:18









      user3593083user3593083

      807




      807
























          2 Answers
          2






          active

          oldest

          votes


















          1














          You can use CROSS JOIN to generate tuples of (contracts, suppliers, contract requirement), then use LEFT JOIN to match contract requirements with supplier approvals:



          SELECT
          contract_requirement.contr,
          suppliers.vndno,
          COUNT(contract_requirement.reqmt) AS req_count,
          COUNT(supplier_approval.reqmt) AS app_count
          FROM contract_requirement
          CROSS JOIN (
          SELECT DISTINCT vndno
          FROM supplier_approval
          ) AS suppliers
          LEFT JOIN supplier_approval ON suppliers.vndno = supplier_approval.vndno AND contract_requirement.reqmt = supplier_approval.reqmt
          GROUP BY contract_requirement.contr, suppliers.vndno
          HAVING COUNT(contract_requirement.reqmt) = COUNT(supplier_approval.reqmt)





          share|improve this answer


























          • Thank you sir! This works perfectly!

            – user3593083
            Jan 2 at 15:36



















          0














          You can use a join and group by and then having to be sure that you have all requirements:



          select cr.contr
          from (select cr.*, count(*) over (partition by cr.contr) as cnt
          from contract_requirement cr
          ) cr join
          supplier_approval sa
          on sa.approvalid = cr.requirementid
          group by cr.contr, cr.cnt
          having cr.cnt = count(*)





          share|improve this answer
























          • Access Client Solutions doesn't like the "(" character before "partition".

            – user3593083
            Jan 2 at 14:57











          • @user3593083 . . . That is the syntax for window functions, which DB2 supports.

            – Gordon Linoff
            Jan 2 at 14:59











          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%2f54007951%2fselect-suppliers-for-each-contract-that-have-all-codes-required-by-the-contract%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          You can use CROSS JOIN to generate tuples of (contracts, suppliers, contract requirement), then use LEFT JOIN to match contract requirements with supplier approvals:



          SELECT
          contract_requirement.contr,
          suppliers.vndno,
          COUNT(contract_requirement.reqmt) AS req_count,
          COUNT(supplier_approval.reqmt) AS app_count
          FROM contract_requirement
          CROSS JOIN (
          SELECT DISTINCT vndno
          FROM supplier_approval
          ) AS suppliers
          LEFT JOIN supplier_approval ON suppliers.vndno = supplier_approval.vndno AND contract_requirement.reqmt = supplier_approval.reqmt
          GROUP BY contract_requirement.contr, suppliers.vndno
          HAVING COUNT(contract_requirement.reqmt) = COUNT(supplier_approval.reqmt)





          share|improve this answer


























          • Thank you sir! This works perfectly!

            – user3593083
            Jan 2 at 15:36
















          1














          You can use CROSS JOIN to generate tuples of (contracts, suppliers, contract requirement), then use LEFT JOIN to match contract requirements with supplier approvals:



          SELECT
          contract_requirement.contr,
          suppliers.vndno,
          COUNT(contract_requirement.reqmt) AS req_count,
          COUNT(supplier_approval.reqmt) AS app_count
          FROM contract_requirement
          CROSS JOIN (
          SELECT DISTINCT vndno
          FROM supplier_approval
          ) AS suppliers
          LEFT JOIN supplier_approval ON suppliers.vndno = supplier_approval.vndno AND contract_requirement.reqmt = supplier_approval.reqmt
          GROUP BY contract_requirement.contr, suppliers.vndno
          HAVING COUNT(contract_requirement.reqmt) = COUNT(supplier_approval.reqmt)





          share|improve this answer


























          • Thank you sir! This works perfectly!

            – user3593083
            Jan 2 at 15:36














          1












          1








          1







          You can use CROSS JOIN to generate tuples of (contracts, suppliers, contract requirement), then use LEFT JOIN to match contract requirements with supplier approvals:



          SELECT
          contract_requirement.contr,
          suppliers.vndno,
          COUNT(contract_requirement.reqmt) AS req_count,
          COUNT(supplier_approval.reqmt) AS app_count
          FROM contract_requirement
          CROSS JOIN (
          SELECT DISTINCT vndno
          FROM supplier_approval
          ) AS suppliers
          LEFT JOIN supplier_approval ON suppliers.vndno = supplier_approval.vndno AND contract_requirement.reqmt = supplier_approval.reqmt
          GROUP BY contract_requirement.contr, suppliers.vndno
          HAVING COUNT(contract_requirement.reqmt) = COUNT(supplier_approval.reqmt)





          share|improve this answer















          You can use CROSS JOIN to generate tuples of (contracts, suppliers, contract requirement), then use LEFT JOIN to match contract requirements with supplier approvals:



          SELECT
          contract_requirement.contr,
          suppliers.vndno,
          COUNT(contract_requirement.reqmt) AS req_count,
          COUNT(supplier_approval.reqmt) AS app_count
          FROM contract_requirement
          CROSS JOIN (
          SELECT DISTINCT vndno
          FROM supplier_approval
          ) AS suppliers
          LEFT JOIN supplier_approval ON suppliers.vndno = supplier_approval.vndno AND contract_requirement.reqmt = supplier_approval.reqmt
          GROUP BY contract_requirement.contr, suppliers.vndno
          HAVING COUNT(contract_requirement.reqmt) = COUNT(supplier_approval.reqmt)






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 2 at 14:55

























          answered Jan 2 at 14:34









          Salman ASalman A

          184k67343441




          184k67343441













          • Thank you sir! This works perfectly!

            – user3593083
            Jan 2 at 15:36



















          • Thank you sir! This works perfectly!

            – user3593083
            Jan 2 at 15:36

















          Thank you sir! This works perfectly!

          – user3593083
          Jan 2 at 15:36





          Thank you sir! This works perfectly!

          – user3593083
          Jan 2 at 15:36













          0














          You can use a join and group by and then having to be sure that you have all requirements:



          select cr.contr
          from (select cr.*, count(*) over (partition by cr.contr) as cnt
          from contract_requirement cr
          ) cr join
          supplier_approval sa
          on sa.approvalid = cr.requirementid
          group by cr.contr, cr.cnt
          having cr.cnt = count(*)





          share|improve this answer
























          • Access Client Solutions doesn't like the "(" character before "partition".

            – user3593083
            Jan 2 at 14:57











          • @user3593083 . . . That is the syntax for window functions, which DB2 supports.

            – Gordon Linoff
            Jan 2 at 14:59
















          0














          You can use a join and group by and then having to be sure that you have all requirements:



          select cr.contr
          from (select cr.*, count(*) over (partition by cr.contr) as cnt
          from contract_requirement cr
          ) cr join
          supplier_approval sa
          on sa.approvalid = cr.requirementid
          group by cr.contr, cr.cnt
          having cr.cnt = count(*)





          share|improve this answer
























          • Access Client Solutions doesn't like the "(" character before "partition".

            – user3593083
            Jan 2 at 14:57











          • @user3593083 . . . That is the syntax for window functions, which DB2 supports.

            – Gordon Linoff
            Jan 2 at 14:59














          0












          0








          0







          You can use a join and group by and then having to be sure that you have all requirements:



          select cr.contr
          from (select cr.*, count(*) over (partition by cr.contr) as cnt
          from contract_requirement cr
          ) cr join
          supplier_approval sa
          on sa.approvalid = cr.requirementid
          group by cr.contr, cr.cnt
          having cr.cnt = count(*)





          share|improve this answer













          You can use a join and group by and then having to be sure that you have all requirements:



          select cr.contr
          from (select cr.*, count(*) over (partition by cr.contr) as cnt
          from contract_requirement cr
          ) cr join
          supplier_approval sa
          on sa.approvalid = cr.requirementid
          group by cr.contr, cr.cnt
          having cr.cnt = count(*)






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 2 at 14:21









          Gordon LinoffGordon Linoff

          791k35314418




          791k35314418













          • Access Client Solutions doesn't like the "(" character before "partition".

            – user3593083
            Jan 2 at 14:57











          • @user3593083 . . . That is the syntax for window functions, which DB2 supports.

            – Gordon Linoff
            Jan 2 at 14:59



















          • Access Client Solutions doesn't like the "(" character before "partition".

            – user3593083
            Jan 2 at 14:57











          • @user3593083 . . . That is the syntax for window functions, which DB2 supports.

            – Gordon Linoff
            Jan 2 at 14:59

















          Access Client Solutions doesn't like the "(" character before "partition".

          – user3593083
          Jan 2 at 14:57





          Access Client Solutions doesn't like the "(" character before "partition".

          – user3593083
          Jan 2 at 14:57













          @user3593083 . . . That is the syntax for window functions, which DB2 supports.

          – Gordon Linoff
          Jan 2 at 14:59





          @user3593083 . . . That is the syntax for window functions, which DB2 supports.

          – Gordon Linoff
          Jan 2 at 14:59


















          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%2f54007951%2fselect-suppliers-for-each-contract-that-have-all-codes-required-by-the-contract%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

          MongoDB - Not Authorized To Execute Command

          How to fix TextFormField cause rebuild widget in Flutter

          in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith