简体   繁体   中英

Alias of INNER JOIN referred to a column in the field list SQL

I'm really struggling about this.

SELECT t1.servizio, 
       Count(DISTINCT p.pro_id), 
       Count(DISTINCT CASE 
                        WHEN Concat(p.pro_id, p.gender) LIKE '%M' THEN p.pro_id 
                      END) AS 'M', 
       Count(DISTINCT CASE 
                        WHEN Concat(p.pro_id, p.gender) LIKE '%F' THEN p.pro_id 
                      END) AS 'F', 
       Count(DISTINCT CASE 
                        WHEN Concat(p.pro_id, p.in_provincia) LIKE '%Si' THEN 
                        p.pro_id 
                      END) AS 'prov_si', 
       Count(DISTINCT CASE 
                        WHEN Concat(p.pro_id, p.in_provincia) LIKE '%No' THEN 
                        p.pro_id 
                      END) AS 'prov_no', 
       Count(DISTINCT t1.pronto_request_id), 
       Count(t2.quote_id) 
FROM   pro p, 
       pro_request pr, 
       request_servizio t1 
       JOIN (SELECT request_servizio.servizio, 
                    Count(quote_request.quote_id) 
             FROM   request_servizio, 
                    quote_request 
             WHERE  request_servizio.pronto_request_id = 
                    quote_request.pronto_request_id 
             GROUP  BY request_servizio.servizio) AS t2 
         ON t1.servizio = t2.servizio 
WHERE  p.pro_id = pr.pro_id 
       AND pr.pronto_request_id = t1.pronto_request_id 
GROUP  BY t1.servizio

Basically, I have 4 tables involved: request_servizio , pro , pro_request and quote_request .

The problem is with the last selected column:

the error is "unknown column t2.quote_id..."

But yet t2 is the alias of the join. So why I got this error?

I tried to include the table quote_request which is the one where you can find the last attribute, but I got a wrong aggregation then. How to avoid this?

Thank you in advance!

In the JOIN, the subquery you used doesn't provide the column quote_id which means that you can not reference it from the alias t2 as it was not provided in that inner subquery in the select part. You can only use what you give from the inner query.

I would say change the query to this as you already seem to have the count you want to use.

SELECT t1.servizio, 
   Count(DISTINCT p.pro_id), 
   Count(DISTINCT CASE 
                    WHEN Concat(p.pro_id, p.gender) LIKE '%M' THEN p.pro_id 
                  END) AS 'M', 
   Count(DISTINCT CASE 
                    WHEN Concat(p.pro_id, p.gender) LIKE '%F' THEN p.pro_id 
                  END) AS 'F', 
   Count(DISTINCT CASE 
                    WHEN Concat(p.pro_id, p.in_provincia) LIKE '%Si' THEN 
                    p.pro_id 
                  END) AS 'prov_si', 
   Count(DISTINCT CASE 
                    WHEN Concat(p.pro_id, p.in_provincia) LIKE '%No' THEN 
                    p.pro_id 
                  END) AS 'prov_no', 
   Count(DISTINCT t1.pronto_request_id), 
   Count(t2.CountOfQuoteId) --edit here
FROM   pro p, 
   pro_request pr, 
   request_servizio t1 
   JOIN (SELECT request_servizio.servizio, 
                Count(quote_request.quote_id) AS CountOfQuoteId --edit here
         FROM   request_servizio, 
                quote_request 
         WHERE  request_servizio.pronto_request_id = 
                quote_request.pronto_request_id 
         GROUP  BY request_servizio.servizio) AS t2 
     ON t1.servizio = t2.servizio 
WHERE  p.pro_id = pr.pro_id 
   AND pr.pronto_request_id = t1.pronto_request_id 
GROUP  BY t1.servizio

Also you could change those joins to the newer standard, it is much cleaner and easier to use.

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