简体   繁体   中英

SQL Sybase Query Strange Behaviour

I've got 2 tables with exactly the same structure in the same Sybase database but they're separate tables.

This query works on one of the 2:

select * from table1 where 
QUOTA_FIELD >
(SELECT 
 count(ACCOUNT) FROM 
 table1 As t1
where SECTOR = t1.SECTOR
AND 
STATUS = 'QUOTA'
) 

But for the second table I have to change it to this:

select * from table2 as tref where 
QUOTA_FIELD >
(SELECT 
 count(ACCOUNT) FROM 
 table2 As t2
where tref.SECTOR = t2.SECTOR
AND 
STATUS = 'QUOTA'
) 

There's a restriction on where this will execute which means it needs to work like in the first query.

Does anyone have any ideas as to why the first might work as expected and the second wouldn't?

Since I am not yet allowed to comment, here as an answer to the question "does anyone...?":

No. I couldn't find anyone :)

This first query cannot work correctly, since it compares a column with itself (as long as the column names are all normal ASCII characters and not some similar looking UNICODE ones). Please give a proof that the result of this query is in every case the same as of query 2.

Also, the second query would normally be done like that: where SECTOR = tref.SECTOR ...

You might be looking for something like this in query #1 :

select * from table1 t2 where 
QUOTA_FIELD >
(SELECT 
 count(ACCOUNT) FROM 
 table1 As t1
where t2.SECTOR = t1.SECTOR          
AND 
t1.STATUS = 'QUOTA'
) 

This explicitly specifies that the table in subquery is joining with the table in outer query ( co-related subquery ).

If this works, use the same idea in query #2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM