473,397 Members | 2,068 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Filter Scalar Subquery

18
I've spent most of the day trying to research but still not able to find the answer. When trying to run the code below, I get an error message saying that "new_user_name" doesn't exist. The code below run correctly without the last filter (WHERE new_user_name = 'a'). Similar code work just fine using MySQL but I can't understand why SQL Server return an error message. Any thought on this?

Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.   id,
  3.   first_name,
  4.   (SELECT user_name FROM table2 WHERE table2.user_id = users.id) as new_user_name
  5. FROM users
  6. WHERE new_user_name = 'a'
  7.  
May 2 '11 #1
3 2221
gpl
152 100+
new_user_name cannot be referenced in the outer query as it is an alias for the subquery

why not just use a join?
May 2 '11 #2
daonho
18
The actual subquery is function, which uses the "coalesce" function to return a comma separated list of organizations belong to that user. Therefore, it is not really possible to perform an inner join.
May 2 '11 #3
gpl
152 100+
ok - I dont have SQL server available to me, but you would have to do something like this:
Expand|Select|Wrap|Line Numbers
  1. SELECT 
  2.   id, 
  3.   first_name, 
  4.   (SELECT user_name FROM table2 WHERE table2.user_id = users.id) as new_user_name 
  5. FROM users 
  6. WHERE (SELECT user_name FROM table2 WHERE table2.user_id = users.id) = 'a' 
  7.  
May 3 '11 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Bob Stearns | last post by:
As part of what I want in a report, I want the associated row from t2, if there is one, with the largest date in a range based on the base row from t1. I attempted the following: left outer join...
5
by: margospencer | last post by:
Does db2 support this subquery: select b.time_ssno, sum(b.time_hours) as hours95, (select sum(a.time_hours) from p0230.pyrltime_time_v1 a where time_date between '2005-01-01' and '2005-03-31'...
5
by: Bob Stearns | last post by:
When I run the following query with the two sections commented out, the response time is between 1 an 2 seconds; with the first indicated section enabled, the response goes up to 15 seconds even...
3
by: dhowell | last post by:
In reading some of the posts on this group, it appears as though it is not strait forward at all to filter a form, which has subforms, by criteria which are either on subforms or span more than one...
22
by: Kevin Murphy | last post by:
I'm using PG 7.4.3 on Mac OS X. I am disappointed with the performance of queries like 'select foo from bar where baz in (subquery)', or updates like 'update bar set foo = 2 where baz in...
0
by: lordofhyphens | last post by:
I'm trying to form a correlated subquery (right now, it's MS Access running to linked tables in MS SQL, although I have tried to make the same query against the MS SQL tables directly). The goal...
3
by: jcf378 | last post by:
Hi all-- Does anyone have any insight as to how I might create a search form that allows a user to select criteria based on any related table in the whole database. The search form I have now only...
1
by: jcf378 | last post by:
Hi all-- Does anyone have any insight as to how I might create a search form that allows a user to select criteria based on any related table in the whole database. The search form I have now only...
1
by: Sanjeev | last post by:
Hi Gurus, I have table (CallByCall) with following Data. Input : CallID Event DNIS UserID Time SeqID MobNo -----------------------------------------------------------------------------...
2
by: nshishir | last post by:
In oracle, there is a performance improvement if scalar subqueries are used instead of joins. Does this hold good for Db2 (8.2) too?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.