Solved

Join query  select top 1 of record

Posted on 2011-03-18
4
290 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-
  • 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 40

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
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…

706 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

19 Experts available now in Live!

Get 1:1 Help Now