Solved

Join query  select top 1 of record

Posted on 2011-03-18
4
330 Views
Last Modified: 2012-05-11
Hi,

I have a result set that returns a column of dates with id's:

select min(CreatedDate),TrainingProviderId
FROM dbo.EmployerTrainingProvider
group by TrainingProviderId

date    id
ddd    4343
ddd    4345

...

I have another table that has many records with names of the match training providers mapping to the training provides ids, for performance reasons I would only need the first match

so to find 1 match would be

SELECT top 1 tpname from reporting.vacancy_poster_Emp_info where preferredTrainingProviderid =4343

how do I join both query's together to get a result like

date    id      name
ddd    4343  aname
ddd    4345  anothername

Darren
0
Comment
Question by:darren-w-
[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
4 Comments
 
LVL 39

Accepted Solution

by:
Pratima Pharande earned 400 total points
ID: 35164245
SELECT  dd , preferredTrainingProviderid , min(tpname) from reporting.vacancy_poster_Emp_info
inner join (

select min(CreatedDate) as dd ,TrainingProviderId
FROM dbo.EmployerTrainingProvider
group by TrainingProviderId ) X
on preferredTrainingProviderid =X.TrainingProviderId
group by dd , preferredTrainingProviderid
0
 
LVL 13

Author Comment

by:darren-w-
ID: 35164383
Thats looking good, though  I would like to get back the date and id with the name column null (not excluded) it if there is no match, how would this be done?

I may have another table I could link the null values too, if not found in this one.

D
0
 
LVL 41

Assisted Solution

by:Sharath
Sharath earned 100 total points
ID: 35167930
try this.
SELECT t1.dd, 
       t1.TrainingProviderId, 
       t2.tpname 
  FROM (  SELECT MIN(CreatedDate) dd, 
                 TrainingProviderId 
            FROM dbo.EmployerTrainingProvider 
        GROUP BY TrainingProviderId) t1 
       LEFT JOIN (  SELECT preferredTrainingProviderid, 
                           MAX(tpname) tpname 
                      FROM reporting.vacancy_poster_Emp_info 
                  GROUP BY preferredTrainingProviderid) t2 
         ON t1.TrainingProviderId = t2.preferredTrainingProviderid

Open in new window

0
 
LVL 13

Author Closing Comment

by:darren-w-
ID: 35196949
Did it slightly different in the end, but found examples useful
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

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?
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

734 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