Solved

Help with TSQL syntax

Posted on 2014-02-14
7
248 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
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
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Solution for warm standby SQL server 20 38
SQL Server - Slabs 9 37
How to revise an Access 2003 query into a SQL Server 2008 SQL statement? 1 24
TSQL previous 5 24
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

910 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

20 Experts available now in Live!

Get 1:1 Help Now