Solved

Help with an SQL Query

Posted on 2009-07-14
5
207 Views
Last Modified: 2012-05-07
I have 2 tables, Codes and Targets.  They have a common key, Code.  I am trying to put together a simple query to put the targets identity into Codes as a foreign key.

This is using SQL Server 2008.

Select Code, Target from Codes as C
Select Top 1 id, Code from Targets as T where T.Code=C.Code Order By T.FromDate Desc

Those are the 2 queries that I need to join to get something along the lines of
Update Codes set Target=(SELECT TOP 1 T.ID From Targets as T WHERE T.Code=Codes.Code ORDER BY T.FromDate DESC)

How can I properly form this query?
0
Comment
Question by:GeoffSutton
5 Comments
 
LVL 32

Expert Comment

by:awking00
ID: 24849815
Can you provide the relevant table structures with some sample data and the expected result?
0
 
LVL 17

Expert Comment

by:pssandhu
ID: 24849822
something like this:
UPDATE CODES

SET    Target = ID

FROM   Codes c INNER JOIN Targets t

      ON c.Code = t.Code

Open in new window

0
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 500 total points
ID: 24849853
update C
SET Target = t1. ID
FROM Codes c
INNER join (SELECT Code, MAX(FromDate) MaxDate from Targets GROUP BY Code ) T on t.Code = c.code
INNER JOIN Targets t1 on t.code  = t1.code and t.MaxDate = t1.FromDate
0
 
LVL 10

Author Closing Comment

by:GeoffSutton
ID: 31603268
Thanks.  I knew it was a relatively simple query, just couldn't put the pieces together.  It was quick and did exactly what I needed.
0
 
LVL 14

Expert Comment

by:shru_0409
ID: 24850074
Select Code, Target
from Codes as C, (Select Top 1 id, Code from Targets Order By T.FromDate Desc
 )  t
where T.Code=C.Code
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

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

910 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

18 Experts available now in Live!

Get 1:1 Help Now