• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 336
  • Last Modified:

Filter by highest value

I'm trying to find the last ship date for each customer in our database. Each customer has multiple orders in the orders table and I just need to see the last order that each customer made. Here is an example:

Imagine that I have a table like this.
CustNum              ShipDate
   123                   08-01-2011
   456                   08-01-2011
   789                   08-03-2011
   123                   08-04-2011
   123                   08-12-2011
   789                   08-09-2011

I want my query results to look like this
CustNum             ShipDate
   123                   08-12-2011
   456                   08-01-2011
   789                   08-09-2011
0
David11011
Asked:
David11011
  • 3
  • 2
  • 2
2 Solutions
 
wdosanjosCommented:
Select CustNum, Max(ShipDate) ShipDate From YourTable
   Group By CustNum
   Order By CustNum
0
 
David11011Author Commented:
Thats putting me down the right path. The only problem I'm having now is that there are more columns in the table than I posted. When I try and do a group by it says that the other columns  are not contained in the aggregate function or the group by clause. Here is a list of the columns that I need to see from the table.

CUSTNO
DAYPHONE
COMPANYNAME
STATE
CUSTOMERTYPE
STATUS
FNAME
LNAME
FAXPHONE
SHIPDATE

The problem I'm having with the group by clause is that it will return a new row if there is different data in any of the columns.

I want to group by CUSTNO and not include the other columns in the group by clause so that rows will be dropped regardless of whether or not the other columns are unique.

I was having a hard aticulating my point. I hope that I wasn't too confusing
0
 
almanderCommented:
Something like this should work.


Select CustNum,
 (SELECT Max(SUB_TAB.ShipDate) FROM  YourTable SUB_TAB WHERE SUB_TAB.CUSTOMERID = YourTable.CUSTOMERID)  LAST_SHIP_DATE,
  ShipDate ,
  *
From YourTable

   
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
wdosanjosCommented:
Got it.  Please try the following:

Select CUSTNO, DAYPHONE, COMPANYNAME, STATE, CUSTOMERTYPE, STATUS, FNAME, LNAME, FAXPHONE, SHIPDATE
  From YourTable c
       Inner Join (Select CUSTNO, Max(SHIPDATE) SHIPDATE From YourTable Group By CUSTNO) sdt
             On sdt.CUSTNO = c.CUSTNO and sdt.SHIPDATE = c.SHIPDATE

Open in new window

0
 
almanderCommented:
Select CustNum,
 (SELECT Max(SUB_TAB.ShipDate) FROM  YourTable SUB_TAB WHERE SUB_TAB.CustNum = YourTable.CustNum)  LAST_SHIP_DATE,
  ShipDate ,
  *
From YourTable
0
 
wdosanjosCommented:
Sorry. I missed the table identifier.  Here is the corrected version:

Select c.CUSTNO, c.DAYPHONE, c.COMPANYNAME, c.STATE, c.CUSTOMERTYPE, c.STATUS, c.FNAME, c.LNAME, c.FAXPHONE, c.SHIPDATE
  From YourTable c
       Inner Join (Select CUSTNO, Max(SHIPDATE) SHIPDATE From YourTable Group By CUSTNO) sdt
             On sdt.CUSTNO = c.CUSTNO and sdt.SHIPDATE = c.SHIPDATE

Open in new window

0
 
David11011Author Commented:
Thanks for all your help guys.

I realized that the customer information is consistent throughout the table so I could add all the columns in to the group by clause. Here is the query that I came up with.
select	CUSTOMERS.CUSTNO as 'CustomerNo' ,
		CUSTOMERPHONE.DAYPHONE as 'Phone', 
		CUSTOMERS.NAMEX as 'Company', 
		CUSTOMERS.[STATE] as 'ST', 
		CUSTOMERS.CUSTTYPE as 'CT', 
		CUSTOMERS.CUSTSTATUS as 'CS', 
		CUSTOMERS.FNAME as 'First Name', 
		CUSTOMERS.LNAME as 'Last Name', 
		CUSTOMERPHONE.FAXPHONE as 'Fax',
		max(macsaleUnion.SHIPDATE) as 'Last Ship Date'
from ORDERHEADER
				inner join macsaleUnion on macsaleUnion.ORDERNO = substring(ORDERHEADER.FULLORDERNO, 0, 9) 
				inner join CUSTOMERS on ORDERHEADER.CUSTEDP = CUSTOMERS.CUSTEDP 
				full outer join CUSTOMERPHONE on ORDERHEADER.CUSTEDP = CUSTOMERPHONE.CUSTEDP
				full outer join ITEMMAST on ITEMMAST.EDPNO = macsaleUnion.edpNos
where	(ITEMMAST.ITEMNO like @itemNo + '%') 
		and (FAXPHONE <> '') 
		and (FAXPHONE <> '0')
		and (FAXPHONE <> '1')
		and (ISNUMERIC(FAXPHONE) = 1)
		and (LEN(FAXPHONE) <= 10)
		and (CUSTTYPE = 'TK' or CUSTTYPE = 'CT' or CUSTTYPE = 'PC' or CUSTTYPE = 'CO')
		and (CUSTSTATUS = 'CC' or CUSTSTATUS = 'CA' or CUSTSTATUS = 'CB' or CUSTSTATUS = 'CV' or CUSTSTATUS = 'e%' or CUSTSTATUS = 'r%')
		and ((cast(ORDERHEADER.ENTRYDATE as date) >= @startDate and cast(ORDERHEADER.ENTRYDATE as date) <= @endDate) or (cast(macsaleUnion.SHIPDATE as date) >= @startDate and cast(macsaleUnion.SHIPDATE as date) < @endDate))
group by CUSTOMERS.CUSTNO, CUSTOMERPHONE.DAYPHONE, CUSTOMERS.NAMEX, CUSTOMERS.[STATE], CUSTOMERS.CUSTTYPE, CUSTOMERS.CUSTSTATUS,CUSTOMERS.FNAME,  CUSTOMERS.LNAME, CUSTOMERPHONE.FAXPHONE

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now