Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. SQL - Finding same product in different waehouse where price is not the same

SQL - Finding same product in different waehouse where price is not the same

Scheduled Pinned Locked Moved Database
databasetutorial
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Richard Berry100
    wrote on last edited by
    #1

    Hi I need to run a query on a stock table. Each product may exist in either one or many warehouses. I am trying to find all items where the cost of the product is not the same in warehouse 01 and 02. Example Wh Prod Cost 01 A 10.00 02 A 10.00 01 B 64.00 01 C 98.00 02 C 10.00 In the example data above, I would like to get the last two rows returned. This is what I have tried, but get lots of the same product code

    SELECT stockm.warehouse, stockm.product, stockm.standard_material FROM
    (SELECT product, standard_material FROM scheme_stockm WHERE warehouse = '01') AS A,
    (SELECT product, standard_material FROM scheme_stockm WHERE warehouse = '02') AS B,
    scheme_stockm AS stockm
    WHERE (A.product = B.product AND A.standard_material <> B.standard_material)
    ORDER BY stockm.product, stockm.warehouse

    D M 2 Replies Last reply
    0
    • R Richard Berry100

      Hi I need to run a query on a stock table. Each product may exist in either one or many warehouses. I am trying to find all items where the cost of the product is not the same in warehouse 01 and 02. Example Wh Prod Cost 01 A 10.00 02 A 10.00 01 B 64.00 01 C 98.00 02 C 10.00 In the example data above, I would like to get the last two rows returned. This is what I have tried, but get lots of the same product code

      SELECT stockm.warehouse, stockm.product, stockm.standard_material FROM
      (SELECT product, standard_material FROM scheme_stockm WHERE warehouse = '01') AS A,
      (SELECT product, standard_material FROM scheme_stockm WHERE warehouse = '02') AS B,
      scheme_stockm AS stockm
      WHERE (A.product = B.product AND A.standard_material <> B.standard_material)
      ORDER BY stockm.product, stockm.warehouse

      D Offline
      D Offline
      David Mujica
      wrote on last edited by
      #2

      I think you are close ... but try this:

      SELECT A.warehouse, A.product, A.standard_material,B.warehouse, B.product, B.standard_material FROM
      (SELECT product, standard_material FROM scheme_stockm WHERE warehouse = '01') AS A,
      (SELECT product, standard_material FROM scheme_stockm WHERE warehouse = '02') AS B
      WHERE (A.product = B.product AND A.standard_material <> B.standard_material)
      ORDER BY A.product, A.warehouse

      I believe you included the scheme_stockm too many times in your original query.

      R 1 Reply Last reply
      0
      • D David Mujica

        I think you are close ... but try this:

        SELECT A.warehouse, A.product, A.standard_material,B.warehouse, B.product, B.standard_material FROM
        (SELECT product, standard_material FROM scheme_stockm WHERE warehouse = '01') AS A,
        (SELECT product, standard_material FROM scheme_stockm WHERE warehouse = '02') AS B
        WHERE (A.product = B.product AND A.standard_material <> B.standard_material)
        ORDER BY A.product, A.warehouse

        I believe you included the scheme_stockm too many times in your original query.

        R Offline
        R Offline
        Richard Berry100
        wrote on last edited by
        #3

        Thanks David I had to add warehouse in the two select statements for warehouse '01' and warehouse '02' since they were referred to in the outer SELECT as below and now it works.

        SELECT A.warehouse, A.product, A.standard_material,B.warehouse, B.product, B.standard_material FROM
        (SELECT warehouse, product, standard_material FROM scheme_stockm WHERE warehouse = '01') AS A,
        (SELECT warehouse, product, standard_material FROM scheme_stockm WHERE warehouse = '02') AS B
        WHERE (A.product = B.product AND A.standard_material <> B.standard_material)
        ORDER BY A.product, A.warehouse

        I also got mine to work by adding an additional 'join' as below on stockm.product = A.product, but I prefer yours, since the result is one record per error, whereas mine is two records per error

        SELECT stockm.warehouse, stockm.product, stockm.standard_material FROM
        (SELECT product, standard_material FROM scheme_stockm WHERE warehouse = '01') AS A,
        (SELECT product, standard_material FROM scheme_stockm WHERE warehouse = '02') AS B,
        scheme_stockm AS stockm
        WHERE (A.product = B.product AND A.standard_material <> B.standard_material AND stockm.product = A.product)
        ORDER BY stockm.product, stockm.warehouse

        D 1 Reply Last reply
        0
        • R Richard Berry100

          Thanks David I had to add warehouse in the two select statements for warehouse '01' and warehouse '02' since they were referred to in the outer SELECT as below and now it works.

          SELECT A.warehouse, A.product, A.standard_material,B.warehouse, B.product, B.standard_material FROM
          (SELECT warehouse, product, standard_material FROM scheme_stockm WHERE warehouse = '01') AS A,
          (SELECT warehouse, product, standard_material FROM scheme_stockm WHERE warehouse = '02') AS B
          WHERE (A.product = B.product AND A.standard_material <> B.standard_material)
          ORDER BY A.product, A.warehouse

          I also got mine to work by adding an additional 'join' as below on stockm.product = A.product, but I prefer yours, since the result is one record per error, whereas mine is two records per error

          SELECT stockm.warehouse, stockm.product, stockm.standard_material FROM
          (SELECT product, standard_material FROM scheme_stockm WHERE warehouse = '01') AS A,
          (SELECT product, standard_material FROM scheme_stockm WHERE warehouse = '02') AS B,
          scheme_stockm AS stockm
          WHERE (A.product = B.product AND A.standard_material <> B.standard_material AND stockm.product = A.product)
          ORDER BY stockm.product, stockm.warehouse

          D Offline
          D Offline
          David Mujica
          wrote on last edited by
          #4

          Glad it worked ouy for you. :thumbsup:

          1 Reply Last reply
          0
          • R Richard Berry100

            Hi I need to run a query on a stock table. Each product may exist in either one or many warehouses. I am trying to find all items where the cost of the product is not the same in warehouse 01 and 02. Example Wh Prod Cost 01 A 10.00 02 A 10.00 01 B 64.00 01 C 98.00 02 C 10.00 In the example data above, I would like to get the last two rows returned. This is what I have tried, but get lots of the same product code

            SELECT stockm.warehouse, stockm.product, stockm.standard_material FROM
            (SELECT product, standard_material FROM scheme_stockm WHERE warehouse = '01') AS A,
            (SELECT product, standard_material FROM scheme_stockm WHERE warehouse = '02') AS B,
            scheme_stockm AS stockm
            WHERE (A.product = B.product AND A.standard_material <> B.standard_material)
            ORDER BY stockm.product, stockm.warehouse

            M Offline
            M Offline
            Michael Potter
            wrote on last edited by
            #5

            Your query will only work if you have 2 warehouses. What happens if there are 3 or more? Try:

            SELECT
            a.warehouse,
            a.product,
            a.standard_material
            FROM
            scheme_stockm AS a
            WHERE
            EXISTS (SELECT *
            FROM scheme_stockm
            WHERE product = a.product AND
            standard_material <> a.standard_material)
            ORDER BY
            a.product,
            a.warehouse

            R 1 Reply Last reply
            0
            • M Michael Potter

              Your query will only work if you have 2 warehouses. What happens if there are 3 or more? Try:

              SELECT
              a.warehouse,
              a.product,
              a.standard_material
              FROM
              scheme_stockm AS a
              WHERE
              EXISTS (SELECT *
              FROM scheme_stockm
              WHERE product = a.product AND
              standard_material <> a.standard_material)
              ORDER BY
              a.product,
              a.warehouse

              R Offline
              R Offline
              Richard Berry100
              wrote on last edited by
              #6

              Hi Michael I had wondered about multiple warehouses :) In this particular case the product can exist in other warehouses, however the cost in the others does not need to be the same - only warehouse 01 and 02 must be the same. I did test your query, and it works perfectly! Thanks as I'm sure I'll need something like this in the future

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups