Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Multiple Max Dates Needed for SQL Join Query

Posted on 2013-01-18
2
Medium Priority
?
176 Views
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

0
Comment
Question by:Bianca
2 Comments
 
LVL 41

Accepted Solution

by:
ralmada earned 2000 total points
ID: 38793621
try this way:

SELECT  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     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'))
0
 
LVL 27

Expert Comment

by:Zberteoc
ID: 38793975
You join your table using the jobID and startdate column with a subquery made from  DataEntryView view getting the max actualstartdate per JobID:

select 
	tbl.*
from
	YourTable tbl
	inner join 
	(
		Select 
			JobID, 
			max(ActualStartDate) as ActualStartDate 
		from 
			DataEntryView dev
		group by
			JobID
	) maxdates
	ON 
		maxdates.JobID=tbl.JobID
		AND maxdates.ActualStartDate=tbl.StartDate

Open in new window

0

Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
I have a large data set and a SSIS package. How can I load this file in multi threading?
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to shrink a transaction log file down to a reasonable size.

971 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