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. Tough one

Tough one

Scheduled Pinned Locked Moved Database
databasehelp
3 Posts 2 Posters 23 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.
  • Richard Andrew x64R Offline
    Richard Andrew x64R Offline
    Richard Andrew x64
    wrote on last edited by
    #1

    I can't figure out the following: I have order header and detail tables with UPC codes and the order numbers they belong to. There can be many different UPC codes for each order. The Order IDs are in the header table and the UPC codes are in the detail table. I must find UPC codes that have been used for more than one order, in other words, where the same UPC code exists for two or more distinct orders. I have tried:

    SELECT OD.UPC, OH.OrderId, COUNT(DISTINCT OH.OrderId) [COUNT]
    FROM OrderDetail OD
    LEFT JOIN OrderHeader OH ON OH.OrderHeaderId = OD.OrderHeaderId
    GROUP BY OD.UPC, OH.OrderId
    HAVING COUNT(DISTINCT OH.OrderId) > 1

    But this query returns nothing and I'm not sure if it's because there are no duplicate orders or because the query is wrong. Any help will be tremendously appreciated. Thank you.

    The difficult we do right away... ...the impossible takes slightly longer.

    Richard DeemingR 1 Reply Last reply
    0
    • Richard Andrew x64R Richard Andrew x64

      I can't figure out the following: I have order header and detail tables with UPC codes and the order numbers they belong to. There can be many different UPC codes for each order. The Order IDs are in the header table and the UPC codes are in the detail table. I must find UPC codes that have been used for more than one order, in other words, where the same UPC code exists for two or more distinct orders. I have tried:

      SELECT OD.UPC, OH.OrderId, COUNT(DISTINCT OH.OrderId) [COUNT]
      FROM OrderDetail OD
      LEFT JOIN OrderHeader OH ON OH.OrderHeaderId = OD.OrderHeaderId
      GROUP BY OD.UPC, OH.OrderId
      HAVING COUNT(DISTINCT OH.OrderId) > 1

      But this query returns nothing and I'm not sure if it's because there are no duplicate orders or because the query is wrong. Any help will be tremendously appreciated. Thank you.

      The difficult we do right away... ...the impossible takes slightly longer.

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      How about:

      SELECT UPC, COUNT(DISTINCT OrderHeaderId)
      FROM OrderDetail
      GROUP BY UPC
      HAVING COUNT(DISTINCT OrderHeaderId) > 1


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      Richard Andrew x64R 1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        How about:

        SELECT UPC, COUNT(DISTINCT OrderHeaderId)
        FROM OrderDetail
        GROUP BY UPC
        HAVING COUNT(DISTINCT OrderHeaderId) > 1


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        Richard Andrew x64R Offline
        Richard Andrew x64R Offline
        Richard Andrew x64
        wrote on last edited by
        #3

        Brilliant! I guess I was overthinking it. Thanks, Richard. You saved the last few hairs I have left.

        The difficult we do right away... ...the impossible takes slightly longer.

        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