Avatar of David11011
David11011Flag for United States of America

asked on 

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
Microsoft SQL Server 2008

Avatar of undefined
Last Comment
David11011
ASKER CERTIFIED SOLUTION
Avatar of wdosanjos
wdosanjos
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of David11011
David11011
Flag of United States of America image

ASKER

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
Avatar of almander
almander

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

   
Avatar of wdosanjos
wdosanjos
Flag of United States of America image

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

Avatar of almander
almander

Select CustNum,
 (SELECT Max(SUB_TAB.ShipDate) FROM  YourTable SUB_TAB WHERE SUB_TAB.CustNum = YourTable.CustNum)  LAST_SHIP_DATE,
  ShipDate ,
  *
From YourTable
SOLUTION
Avatar of wdosanjos
wdosanjos
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of David11011
David11011
Flag of United States of America image

ASKER

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

Microsoft SQL Server 2008
Microsoft SQL Server 2008

Microsoft SQL Server 2008 is a suite of relational database management system (RDBMS) products providing multi-user database access functionality.Component services include integration (SSIS), reporting (SSRS), analysis (SSAS), data quality, master data, T-SQL and performance tuning. Major improvements include the Always On technologies and support for unstructured data types.

50K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo