473,418 Members | 2,020 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,418 software developers and data experts.

Get the greater of the numbers in sql seelect statement

I have a select statement

Expand|Select|Wrap|Line Numbers
  1. SELECT C.CustomerID, D.DispatchNumber
  2. FROM tblCustomer C LEFT JOIN tblDispatch D ON C.CustomerID = D.CustomerID
  3. 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(DispatchNumber) 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:
Expand|Select|Wrap|Line Numbers
  1. CustomerID         DispatchID
  2. ---------------------------------
  3. 1                    1
  4. ---------------------------------
  5. 1                    2
  6. ---------------------------------
  7. 2                    1
  8. ---------------------------------
  9. 2                    2
  10. ---------------------------------
  11. 2                    3
  12. ---------------------------------
  13. 3                    1
Oct 5 '10 #1
7 1482
Oralloy
988 Expert 512MB
Try

Expand|Select|Wrap|Line Numbers
  1. SELECT C.CustomerID, MAX(D.DispatchNumber)
  2.   FROM tblCustomer C LEFT JOIN tblDispatch D ON C.CustomerID = D.CustomerID
  3. GROUP BY C.CustomerID
  4.  
Oct 5 '10 #2
Thanks, it works.
Oct 5 '10 #3
Oralloy
988 Expert 512MB
Glad to help.
Oct 5 '10 #4
I am trying to add one more field which is a uniqueidentifier

Expand|Select|Wrap|Line Numbers
  1. SELECT C.CustomerID, MAX(D.DispatchNumber), D.DispatchID
  2.   FROM tblCustomer C LEFT JOIN tblDispatch D ON C.CustomerID = D.CustomerID 
  3. GROUP BY C.CustomerID
and again all the rows are showing up for that customer(repetition). How can I make it show the row that has Max Dispatch Number.I just want to see only 1 row with the DispatchID.
Oct 7 '10 #5
Oralloy
988 Expert 512MB
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:

Expand|Select|Wrap|Line Numbers
  1.  
  2. SELECT C.CustomerID, MAX(D.DispatchNumber), MAX(D.DispatchID) 
  3.   FROM tblCustomer C LEFT JOIN tblDispatch D ON C.CustomerID = D.CustomerID 
  4. GROUP BY C.CustomerID 
  5.  
If it's not unique, you might want to do something like this:
Expand|Select|Wrap|Line Numbers
  1.  
  2. SELECT C.CustomerID, D.DispatchID, MAX(D.DispatchNumber)
  3.   FROM tblCustomer C LEFT JOIN tblDispatch D ON C.CustomerID = D.CustomerID 
  4. GROUP BY C.CustomerID, D.DispatchID 
  5.  
Oct 12 '10 #6
NeoPa
32,556 Expert Mod 16PB
Oralloy, I don't think that does it, as the record with the max(DispatchNumber) 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.
Oct 14 '10 #7
Oralloy
988 Expert 512MB
I agree. In retrospect, I may very well have misunderstood the requirement.

Perhaps he wants the tbdDispatch records associated with the MAX(DispatchNumber)? I dunno....
Oct 14 '10 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: mark | last post by:
Access 2000: I creating a report that has a record source built by the user who selects the WHERE values. An example is: SELECT * FROM CHARGELOG WHERE STDATE Between #10/27/2003# And...
11
by: John | last post by:
Hi, I encountered a strange problem while debugging C code for a Windows-based application in LabWindows CVI V5.5, which led me to write the test code below. I tried this code with a different...
89
by: purifier | last post by:
The problem is to write a program in 'C' to find the greatest of 2 given numbers... Easy? huh here's the catch do not use 'if' or any conditional statements if u want it to be a little more...
33
by: patrick_woflian | last post by:
hey guys, im just writing a basic calculation at the moment, before building on it for an A-Level piece of work. i can add/divide etc... two numbers together yet i am having a major problem with...
11
by: balakrishnan.dinesh | last post by:
hi frnds, Im having two 20digit numbers, But while comparing those it is giiving wrong ouput in javascript. for example here is my code, my secanrio is , ~ If first 20 digit number is...
2
by: Nate | last post by:
When using the following statement, I am encountering a problem: <% If rs.Fields(0) = "Lead 30 Day" THEN %> <% IF rs.Fields(2) "89" THEN %> Pass <% ELSE %> FAIL <% END IF%> <% End If %>
4
by: beginers of c | last post by:
hai friends see i have to get numbers in this manner 3 4 5 6 7 8 9 ok.And also my numbers should not be larger than 9.so for each number the condition has to be checked if it's great than 9 ,...
12
by: electric916 | last post by:
I have a homework assignment i Am totally confused on. I started with a basic code to determine if a number is prime or not, but need guidance from here. I will post assignment details then what I...
5
by: help4me | last post by:
I'm trying to write an if statement to see if the answer I just entered is larger than the answer on the previous page. But let's say the 1st number is 50 and the 2nd is 9. The alert comes up...
5
by: anumliaqat | last post by:
hello!! i hav a problem.my program is giving correct outputs for some inputs but wrong results for others. for example if u give (4 2 2)&(2 1 2) to it,it shows all results correct....
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.