Solved

Help with TSQL syntax

Posted on 2014-02-14
7
255 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 16

Expert Comment

by:Surendra Nath
ID: 39858570
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
ID: 39858579
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
ID: 39858621
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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 32

Expert Comment

by:awking00
ID: 39859208
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:Scott Pletcher
ID: 39860468
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
ID: 39862105
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
ID: 39869794
I ended up answering my own question.
0

Featured Post

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

615 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