I have several small questions and I wrote some simple examples to clarify them:
-
Will a nested aggregate take one value from from all groups? Like the minimal maximum of all groups? or the minimum count?
Select x, min(max(z)) From y Group by x
On the same note, is there any merit in doing the following to get the minimal count from all groups, or the second line is unnecessary?
select x, min(count(*)) select x, count(*) From y Group by x
If both are invalid, how would you do a query on all groups like taking the minimum of all maximums in each group?
-
Can you do a query inside a "from"?
Select x From y natural inner join (select z AS y from foo)
-
Is it allowed to do
from *
after agroup by
?Select x From y Group by x Having avg(x) > (select * from * where x > 1)
and if not, how would you do a query on each group after the
group by
?
Note: this isn't some live version of SQL server, just old theoretical SQL.
Best Answer
re 1)
With standard SQL the only way to do that is:
re 2)
yes you can join to a query, but you need to give the derived table an alias
That assumes that the table
y
also has a columny
- otherwise there wouldn't be two identical columns that the natural join could use.But in general you should avoid the natural join. Use an explicit join instead:
re 3)
No,
from *
is never allowed. But I have no clue what you intend to do with that. The sub-select used with>
(or<
or=
) has to return exactly one row and exactly one column so you would need something like:If the subselect returns more then one row you would need to use
ANY