Solved

Help with TSQL syntax

Posted on 2014-02-14
7
253 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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
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

Independent Software Vendors: 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!

Question has a verified solution.

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

Suggested Solutions

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
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.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

739 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