Multiple Max Dates Needed for SQL Join Query

Posted on 2013-01-18
Last Modified: 2013-02-04
I have a main table DataEntryView that I need joined with other tables that contain the same JobID but have multiple records per JobID, I only want the Max(ActualStartDate) joined to the main DataEntryView. I thought I got it but my view comes up with 'Unsupported Data Type' when I run this query.

SELECT     TOP (100) PERCENT dbo.DataEntryView.ctJobID, dbo.DataEntryView.JobNum, dbo.DataEntryView.Part, dbo.DataEntryView.Contractor, 
                      dbo.DataEntryView.Owner, dbo.DataEntryView.Sales_Initial, dbo.DataEntryView.Sales_Amount, dbo.DataEntryView.DesEng_SalesPerHour, 
                      dbo.DataEntryView.loggedin, dbo.DataEntryView.Customer_Expected_Delivery, Bid_LF_Table.Bid_LF
FROM         dbo.DataEntryView LEFT OUTER JOIN
                          (SELECT     TOP (1) Actual_Start_Date AS Bid_LF, JobID
                            FROM          dbo.DesEngSchedule AS DesEngSchedule_1
                            WHERE      (Department LIKE 'Custom Design Engineering') AND (TaskName LIKE '02%')
                            ORDER BY Bid_LF) AS Bid_LF_Table ON dbo.DataEntryView.ctJobID = Bid_LF_Table.JobID
WHERE     (dbo.DataEntryView.loggedin IS NOT NULL) AND (dbo.DataEntryView.Finalized_Sales >= GETDATE() - 7 OR
                      dbo.DataEntryView.Finalized_Sales IS NULL) AND (dbo.DataEntryView.bidresultdate IS NULL) AND (dbo.DataEntryView.Lost_Sales_Date IS NULL) AND 
                      (dbo.DataEntryView.Sales_Initial NOT LIKE 'PD') AND (dbo.DataEntryView.Sales_Initial NOT LIKE 'CS')

Open in new window

Question by:Bianca
LVL 41

Accepted Solution

ralmada earned 500 total points
Comment Utility
try this way:

SELECT  dbo.DataEntryView.ctJobID,
FROM    dbo.DataEntryView
      SELECT     Max(Actual_Start_Date) AS Bid_LF, JobID
      FROM          dbo.DesEngSchedule AS DesEngSchedule_1
        WHERE      Department LIKE 'Custom Design Engineering%' AND TaskName LIKE '02%'
      group by JobID
) AS Bid_LF_Table ON dbo.DataEntryView.ctJobID = Bid_LF_Table.JobID

WHERE     (dbo.DataEntryView.loggedin IS NOT NULL)  AND
      (dbo.DataEntryView.Finalized_Sales >= GETDATE() - 7 OR dbo.DataEntryView.Finalized_Sales IS NULL) AND
      (dbo.DataEntryView.bidresultdate IS NULL) AND
      (dbo.DataEntryView.Lost_Sales_Date IS NULL) AND
        (dbo.DataEntryView.Sales_Initial not in ('PD', 'CS'))
LVL 26

Expert Comment

Comment Utility
You join your table using the jobID and startdate column with a subquery made from  DataEntryView view getting the max actualstartdate per JobID:

	YourTable tbl
	inner join 
			max(ActualStartDate) as ActualStartDate 
			DataEntryView dev
		group by
	) maxdates
		AND maxdates.ActualStartDate=tbl.StartDate

Open in new window


Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now