Solved

Multiple Max Dates Needed for SQL Join Query

Posted on 2013-01-18
2
173 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

752 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