Solved

Join query  select top 1 of record

Posted on 2011-03-18
4
323 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

Free Webinar: AWS Backup & DR

Join our upcoming webinar with experts from AWS, CloudBerry Lab, and the Town of Edgartown IT to discuss best practices for simplifying online backup management and cutting costs.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

696 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