on 01-12-2015 7:44 AM
Hi,
I'd want to use "AS" to create a temp table for the result of a subquery. But the query can not be executed.How can I fix this error?
Do I need to create a view ?
select
c_count, count(*) as custdist
from (
select
c_custkey,
count(o_orderkey)
from
customer left outer join orders on
c_custkey = o_custkey
and o_comment not like '%special%requests%'
group by
c_custkey
)as c_orders (c_custkey,c_count)
group by
c_count
order by
custdist desc,
c_count desc;
Many thanks in advance!
Naomy
Message was edited by: Tom Flanagan
Hi Naomy,
Please remove "(c_custkey,c_count)" after c_orders.
Best regards,
Wenjun
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Naomy,
An alternative to using sub-selects is to use the WITH statement. Your SQL could be rewritten as follows:
WITH c_orders AS
(
SELECT c_custkey,
count(o_orderkey) c_count
FROM customer LEFT OUTER JOIN orders
ON c_custkey = o_custkey
AND o_comment NOT LIKE '%special%requests%'
GROUP BY c_custkey)
SELECT c_count,
count(*) AS custdist
FROM c_orders
GROUP BY c_count
ORDER BY custdist DESC,
c_count DESC;
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I still need c_count as an alias for count(o_orderkey).Based on Wenjun's solution,I correct the SQL statement and it work now.
select
c_count, count(*) as custdist
from (
select
c_custkey,
count(o_orderkey) as c_count
from
customer left outer join orders on
c_custkey = o_custkey
and o_comment not like '%special%requests%'
group by
c_custkey
)as c_orders
group by
c_count
order by
custdist desc,
c_count desc;
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
81 | |
11 | |
8 | |
8 | |
6 | |
6 | |
6 | |
6 | |
5 | |
5 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.