Get the greater of the numbers in sql seelect statement

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sasasasa
    New Member
    • Apr 2010
    • 29

    Get the greater of the numbers in sql seelect statement

    I have a select statement

    Code:
    SELECT C.CustomerID, D.DispatchNumber
    FROM tblCustomer C LEFT JOIN tblDispatch D ON C.CustomerID = D.CustomerID
    WHERE D.DispatchNumber ????(is bigger)
    I don't know what to write in the statement to get the bigger dispatch number to show with customer ID because there are repetitions for dispatch number for a particular customer. I don't need the Max(DispatchNum ber) because it gives me the highest of all the dispatch numbers. How can I get the highest of the dispatch number for a customer.
    My Dispatch table looks like:
    Code:
    CustomerID         DispatchID
    ---------------------------------
    1                    1
    ---------------------------------
    1                    2
    ---------------------------------
    2                    1
    ---------------------------------
    2                    2
    ---------------------------------
    2                    3
    ---------------------------------
    3                    1
    Last edited by NeoPa; Oct 14 '10, 03:49 PM. Reason: Please use the [code] tags provided.
  • Oralloy
    Recognized Expert Contributor
    • Jun 2010
    • 988

    #2
    Try

    Code:
    SELECT C.CustomerID, MAX(D.DispatchNumber)
      FROM tblCustomer C LEFT JOIN tblDispatch D ON C.CustomerID = D.CustomerID
    GROUP BY C.CustomerID

    Comment

    • sasasasa
      New Member
      • Apr 2010
      • 29

      #3
      Thanks, it works.

      Comment

      • Oralloy
        Recognized Expert Contributor
        • Jun 2010
        • 988

        #4
        Glad to help.

        Comment

        • sasasasa
          New Member
          • Apr 2010
          • 29

          #5
          I am trying to add one more field which is a uniqueidentifie r

          Code:
          SELECT C.CustomerID, MAX(D.DispatchNumber), D.DispatchID
            FROM tblCustomer C LEFT JOIN tblDispatch D ON C.CustomerID = D.CustomerID 
          GROUP BY C.CustomerID
          and again all the rows are showing up for that customer(repeti tion). How can I make it show the row that has Max Dispatch Number.I just want to see only 1 row with the DispatchID.
          Last edited by NeoPa; Oct 14 '10, 03:50 PM. Reason: Please use the [code] tags provided.

          Comment

          • Oralloy
            Recognized Expert Contributor
            • Jun 2010
            • 988

            #6
            Sorry for the delay, I've been sick the last five days. Ugh!

            Is D.DispatchID unique? If it is, you can use the MAX function on it, too:

            Code:
            SELECT C.CustomerID, MAX(D.DispatchNumber), MAX(D.DispatchID) 
              FROM tblCustomer C LEFT JOIN tblDispatch D ON C.CustomerID = D.CustomerID 
            GROUP BY C.CustomerID
            If it's not unique, you might want to do something like this:
            Code:
            SELECT C.CustomerID, D.DispatchID, MAX(D.DispatchNumber)
              FROM tblCustomer C LEFT JOIN tblDispatch D ON C.CustomerID = D.CustomerID 
            GROUP BY C.CustomerID, D.DispatchID

            Comment

            • NeoPa
              Recognized Expert Moderator MVP
              • Oct 2006
              • 32636

              #7
              Oralloy, I don't think that does it, as the record with the max(DispatchNum ber) may be different from the one with the Max(DispatchID) .

              The real difficulty is that the question is asked so poorly. Even now, after a bit of prompting, the question still isn't clear (and the example data quite useless). If we can get a clear question then I'm sure we can provide a suitable answer.

              Comment

              • Oralloy
                Recognized Expert Contributor
                • Jun 2010
                • 988

                #8
                I agree. In retrospect, I may very well have misunderstood the requirement.

                Perhaps he wants the tbdDispatch records associated with the MAX(DispatchNum ber)? I dunno....

                Comment

                Working...