Solved

Help with TSQL syntax

Posted on 2014-02-14
7
246 Views
Last Modified: 2014-02-19
Hi All,

I need a little help with my TSAQ syntax. When I try to create the sproc below it throws an error when I try to include the last line 'WHERE lt.LevelTest_MaxNum < LevelTestDaysUsed'

How do I include a Where clause that takes a value from its own recordset using 'AS'?

Kind regards

Abbey

----------------------------------------------------

BEGIN
SELECT  lt.LevelTest_RecId
      ,      lt.LevelTest_Label
      ,      lt.LevelTest_MaxNum
      ,      (SELECT COUNT(IsNull(bb.Reg_LevelTestDaySelected,0)) FROM Tbl_Reg_Bilbao bb
                  WHERE IsNull(bb.Reg_LevelTestDaySelected,0) = lt.LevelTest_RecId) AS LevelTestDaysUsed
                  
FROM Tbl_LevelTests lt

WHERE lt.LevelTest_MaxNum < LevelTestDaysUsed

END
0
Comment
Question by:Abiel de Groot
7 Comments
 
LVL 16

Expert Comment

by:Surendra Nath
Comment Utility
you have to write it as

SELECT  lt.LevelTest_RecId
      ,      lt.LevelTest_Label
      ,      lt.LevelTest_MaxNum
      ,      (SELECT COUNT(IsNull(bb.Reg_LevelTestDaySelected,0)) FROM Tbl_Reg_Bilbao bb
                  WHERE IsNull(bb.Reg_LevelTestDaySelected,0) = lt.LevelTest_RecId) AS LevelTestDaysUsed
                  
FROM Tbl_LevelTests lt

WHERE lt.LevelTest_MaxNum < lt.LevelTestDaysUsed 

Open in new window

0
 
LVL 5

Author Comment

by:Abiel de Groot
Comment Utility
No, it still does not allow it.

In valid col name 'LevelTestDaysUsed'
0
 
LVL 5

Accepted Solution

by:
Abiel de Groot earned 0 total points
Comment Utility
I think the solution is not to use a value As a col name. Instead I have simply re-used the entire Select Count line to compare it against 'lt.LevelTest_MaxNum'

This is not as clean looking but works well.

Kind regards

Abiel de Groot
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 31

Expert Comment

by:awking00
Comment Utility
This might also work -
SELECT lt.LevelTest_RecId, lt.LevelTest_Label, lt.LevelTest_MaxNum
,COUNT(IsNull(bb.Reg_LevelTestDaySelected,0)) AS LevelTestDaysUsed
FROM Tbl_LevelTests lt, Tbl_Reg_Bilbao bb
WHERE IsNull(bb.Reg_LevelTestDaySelected,0) = lt.LevelTest_RecId
GROUP BY lt.LevelTest_RecId,lt.LevelTest_Label,lt.LevelTest_MaxNum
HAVING COUNT(IsNull(bb.Reg_LevelTestDaySelected,0)) > lt.LevelTest_MaxNum
0
 
LVL 69

Expert Comment

by:ScottPletcher
Comment Utility
Real pain for performance and to maintain if you repeat the entire subquery.

Instead, you can use OUTER APPLY (or CROSS APPLY).  For example:



SELECT  lt.LevelTest_RecId
      ,      lt.LevelTest_Label
      ,      lt.LevelTest_MaxNum
                 
FROM Tbl_LevelTests lt
OUTER APPLY (
             SELECT COUNT(IsNull(bb.Reg_LevelTestDaySelected,0)) AS LevelTestDaysUsed  
             FROM Tbl_Reg_Bilbao bb
                WHERE IsNull(bb.Reg_LevelTestDaySelected,0) = lt.LevelTest_RecId
) AS outer_apply_1
WHERE lt.LevelTest_MaxNum < outer_apply_1.LevelTestDaysUsed
0
 
LVL 75

Expert Comment

by:Anthony Perkins
Comment Utility
I think the solution is not to use a value As a col name. Instead I have simply re-used the entire Select Count line to compare it against 'lt.LevelTest_MaxNum'
Absolutely not!  That is if you care about performance.  

Scott has given you the solution using OUTER APPLY.
0
 
LVL 5

Author Closing Comment

by:Abiel de Groot
Comment Utility
I ended up answering my own question.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Viewers will learn how the fundamental information of how to create a table.
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.

763 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

6 Experts available now in Live!

Get 1:1 Help Now