[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 316
  • Last Modified:

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?
0
StuartK
Asked:
StuartK
1 Solution
 
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
luaniCommented:
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

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now