on 01-09-2015 2:12 PM
Hi,
I've writen a query like
select 1
from table x
join table y on x.id = y.id
group by y.col1, x.col2, x.col3
now I would expect Sybase to return 1 row containing 1 field containing the value 1 for each group. Insteed only one row is returned containing 1 field with the value 1.
Is this expected in Sybase?
If I change the select to contain the actual grouped columns then I get 22000+ results...
select y.col1, x.col2, x.col3
from table x
join table y on x.id = y.id
group by y.col1, x.col2, x.col3
The ability to write queries with GROUP BY that do not include all the columns from the GROUP BY in the SELECT is a TSQL extension of the ANSI standard, as is a SELECT clause without any aggregates. As the docs say, the results can sometimes be hard to understand.
Substitute an aggregate for the simple literal and I think you will get a result more like what you were expecting (which truthfully doesn't seem very useful to me).
select
max(1)
from sysobjects x
join sysindexes y on x.id = y.id
group by y.indid, x.id, x.name
go
ANSI would have you write the query with every column from the GROUP BY in the SELECT
select
y.indid, x.id, x.name, max(1)
from sysobjects x
join sysindexes y on x.id = y.id
group by y.indid, x.id, x.name
go
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
89 | |
10 | |
9 | |
9 | |
9 | |
6 | |
6 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.