Solved

Join query  select top 1 of record

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

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.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
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.

776 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