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. Join a 3rd table, in which multiple records with the same id exist, but I just need 1 of them

Join a 3rd table, in which multiple records with the same id exist, but I just need 1 of them

Scheduled Pinned Locked Moved Database
question
5 Posts 2 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.
  • J Offline
    J Offline
    jkirkerx
    wrote on last edited by
    #1

    So I have 3 Tables OrderInfo - Unique records ShipRates - Unique records OrderInfo_Emails - A list of non-unique records There are 3 records in orderinfo, but I get 4 results back, because orderinfo_email has 2 records with the same orderinfo id. So I get that, why I get 4 back and not 3 Should I just drop the OrderInfo_Email Join and do something else, some other way, or is there a way to limit the results from the join?

    SELECT
    oi.OrderInfoID
    , oi.OrderDate
    , oi.OrderNum
    , oi.LoginID
    , oi.OrderStatus
    , oi.AuthorizationID
    , oi.ApprovalCode
    , oi.Shipping
    , oi.PaymentInfo
    , oi.SubTotal
    , oi.GrandTotal
    , oi.RateAPICode
    , oi.TotalNetCharge
    , srr.RateName
    , oie.MessageID
    , oie.DateSent
    , oie.SentBy
    FROM OrderInfo oi
    LEFT JOIN ORDERINFO_EMAIL oie
    ON (oie.OrderID = oi.OrderInfoID )
    LEFT JOIN Shipping_RealTime_Rates srr
    ON (srr.RateAPICode = oi.RateAPICode)
    ORDER BY oi.OrderDate DESC

    Kornfeld Eliyahu PeterK 1 Reply Last reply
    0
    • J jkirkerx

      So I have 3 Tables OrderInfo - Unique records ShipRates - Unique records OrderInfo_Emails - A list of non-unique records There are 3 records in orderinfo, but I get 4 results back, because orderinfo_email has 2 records with the same orderinfo id. So I get that, why I get 4 back and not 3 Should I just drop the OrderInfo_Email Join and do something else, some other way, or is there a way to limit the results from the join?

      SELECT
      oi.OrderInfoID
      , oi.OrderDate
      , oi.OrderNum
      , oi.LoginID
      , oi.OrderStatus
      , oi.AuthorizationID
      , oi.ApprovalCode
      , oi.Shipping
      , oi.PaymentInfo
      , oi.SubTotal
      , oi.GrandTotal
      , oi.RateAPICode
      , oi.TotalNetCharge
      , srr.RateName
      , oie.MessageID
      , oie.DateSent
      , oie.SentBy
      FROM OrderInfo oi
      LEFT JOIN ORDERINFO_EMAIL oie
      ON (oie.OrderID = oi.OrderInfoID )
      LEFT JOIN Shipping_RealTime_Rates srr
      ON (srr.RateAPICode = oi.RateAPICode)
      ORDER BY oi.OrderDate DESC

      Kornfeld Eliyahu PeterK Offline
      Kornfeld Eliyahu PeterK Offline
      Kornfeld Eliyahu Peter
      wrote on last edited by
      #2

      How do you pick the one you want? What the criteria?

      I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)

      "It never ceases to amaze me that a spacecraft launched in 1977 can be fixed remotely from Earth." ― Brian Cox

      J 1 Reply Last reply
      0
      • Kornfeld Eliyahu PeterK Kornfeld Eliyahu Peter

        How do you pick the one you want? What the criteria?

        I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)

        J Offline
        J Offline
        jkirkerx
        wrote on last edited by
        #3

        Oh, the first one is fine, It's for Abandoned Orders, in which a email is sent to the customer as a reminder. You can send multiple emails to the customer, in which the emails are logged in ORDERINFO_EMAIL, So When you load the Abandoned Orders, if a email record record exist, a little envelope icon appears.

        Kornfeld Eliyahu PeterK 1 Reply Last reply
        0
        • J jkirkerx

          Oh, the first one is fine, It's for Abandoned Orders, in which a email is sent to the customer as a reminder. You can send multiple emails to the customer, in which the emails are logged in ORDERINFO_EMAIL, So When you load the Abandoned Orders, if a email record record exist, a little envelope icon appears.

          Kornfeld Eliyahu PeterK Offline
          Kornfeld Eliyahu PeterK Offline
          Kornfeld Eliyahu Peter
          wrote on last edited by
          #4

          You may try this:

          SELECT
          oi.OrderInfoID
          , oi.OrderDate
          , oi.OrderNum
          , oi.LoginID
          , oi.OrderStatus
          , oi.AuthorizationID
          , oi.ApprovalCode
          , oi.Shipping
          , oi.PaymentInfo
          , oi.SubTotal
          , oi.GrandTotal
          , oi.RateAPICode
          , oi.TotalNetCharge
          , srr.RateName
          , (select count(*) from ORDERINFO_EMAIL oie where oie.OrderID = oi.OrderInfoID) AS email_sent
          FROM OrderInfo oi
          LEFT JOIN Shipping_RealTime_Rates srr
          ON (srr.RateAPICode = oi.RateAPICode)
          ORDER BY oi.OrderDate DESC

          I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)

          "It never ceases to amaze me that a spacecraft launched in 1977 can be fixed remotely from Earth." ― Brian Cox

          J 1 Reply Last reply
          0
          • Kornfeld Eliyahu PeterK Kornfeld Eliyahu Peter

            You may try this:

            SELECT
            oi.OrderInfoID
            , oi.OrderDate
            , oi.OrderNum
            , oi.LoginID
            , oi.OrderStatus
            , oi.AuthorizationID
            , oi.ApprovalCode
            , oi.Shipping
            , oi.PaymentInfo
            , oi.SubTotal
            , oi.GrandTotal
            , oi.RateAPICode
            , oi.TotalNetCharge
            , srr.RateName
            , (select count(*) from ORDERINFO_EMAIL oie where oie.OrderID = oi.OrderInfoID) AS email_sent
            FROM OrderInfo oi
            LEFT JOIN Shipping_RealTime_Rates srr
            ON (srr.RateAPICode = oi.RateAPICode)
            ORDER BY oi.OrderDate DESC

            I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)

            J Offline
            J Offline
            jkirkerx
            wrote on last edited by
            #5

            I was thinking the same thing to keep it simple OK, I'm going with that idea. Thanks!

            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