Solved

Help with TSQL syntax

Posted on 2014-02-14
7
252 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
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 
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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
point in time restore in SQL server 26 42
denied execute as 13 25
SQL syntax question 6 38
Error in sql query statment. 21 43
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how the fundamental information of how to create a table.

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