Solved

Multiple Max Dates Needed for SQL Join Query

Posted on 2013-01-18
2
174 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 41

Accepted Solution

by:
ralmada earned 500 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how the fundamental information of how to create a table.

617 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