?
Solved

Join query  select top 1 of record

Posted on 2011-03-18
4
Medium Priority
?
337 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 1600 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 400 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 Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

719 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