Nested SQL

Hi, I'm trying to do this.

Have two tables Tbl_ServiceLevels, Tbl_AlarmServiceAgreements

Tbl_ServiceLevels has fields ServiceLevel and RenewalDate

Tbl_AlarmServiceAgreements has many fields we want to return including a ServiceLevel field

We want to get the ServiceLevel field for the max Renewal Date
-Then use that to return all the relevant fields from Tbl_ServiceLevels where fields ServiceLevel = that ServiceLevel string name found above.

Can do this in two different sql statements easy enough, but we plan on running thousands of times, and therefore want to optimise time.

Here are the two sql statements we want to nest

SELECT     dbo.Tbl_ServiceLevels.*
FROM         dbo.Tbl_ServiceLevels
WHERE     (ServiceLevel = '???')

and

SELECT   MAX(RenewalDate) AS Expr1, ServiceLevel
FROM         Tbl_AlarmServiceAgreements
WHERE     (AlarmRef = 311)
GROUP BY ServiceLevel
ORDER BY Expr1 DESC

Can anyone guide in how to nest?
StuartKAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

HainKurtSr. System AnalystCommented:
try this
SELECT     dbo.Tbl_ServiceLevels.*, MAX_RenewalDate
FROM         dbo.Tbl_ServiceLevels s, 
(
SELECT   MAX(RenewalDate) AS MAX_RenewalDate, ServiceLevel
FROM         Tbl_AlarmServiceAgreements
WHERE     (AlarmRef = 311)
GROUP BY ServiceLevel
) sa
WHERE     s.ServiceLevel = sa.ServiceLevel

Open in new window

0
HainKurtSr. System AnalystCommented:
or using left join
SELECT     dbo.Tbl_ServiceLevels.*, MAX_RenewalDate
FROM         dbo.Tbl_ServiceLevels s left join 
(
SELECT   MAX(RenewalDate) AS MAX_RenewalDate, ServiceLevel
FROM         Tbl_AlarmServiceAgreements
WHERE     (AlarmRef = 311)
GROUP BY ServiceLevel
) on s.ServiceLevel = sa.ServiceLevel

Open in new window

0
HainKurtSr. System AnalystCommented:
oops, forgot alias sa
SELECT s.*, sa.MAX_RenewalDate
  FROM dbo.Tbl_ServiceLevels s left join 
(
SELECT MAX(RenewalDate) AS MAX_RenewalDate, ServiceLevel
  FROM Tbl_AlarmServiceAgreements
 WHERE (AlarmRef = 311)
 GROUP BY ServiceLevel
) sa on s.ServiceLevel = sa.ServiceLevel

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Luan JubicaProject ManagerCommented:
SELECT     dbo.Tbl_ServiceLevels.*,Expr1
FROM         dbo.Tbl_ServiceLevels
(SELECT   MAX(RenewalDate) AS Expr1, ServiceLevel
FROM         Tbl_AlarmServiceAgreements
WHERE     (AlarmRef = 311)
GROUP BY ServiceLevel
ORDER BY Expr1 DESC) sub

WHERE     (sub.ServiceLevel = '???') and sub.ServiceLevel =ServiceLevels.ServiceLevel
0
Anthony PerkinsCommented:
>>Zones: MS SQL Server, Microsoft Access Database, .NET<<
Are you looking for a solution for MS Access or SQL Server?
0
StuartKAuthor Commented:
Thanking all for their superb posts!

acperkins : Please explain where it should go, because that is not obvious to me, i would like to know. Was thinking SQL and therefore database orientated, but please explain cause I would like to know for the future!
0
Anthony PerkinsCommented:
>>Please explain where it should go<<
If you are using MS Access than post there.  Posting in MS SQL Server is not appropriate as the language (T-SQL) is not compatible with the SQL dialect used in MS Access.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.