我试图将这两个SQL查询连接在一起。我的数据位于https://policevideorequests.cartodb.com/tables/seattle_police_govqa_audit_trails,它有一个Postgresql SQL API。
SELECT
t1.customer_id, t1.c,
t2.customer_name, t2.customer_email, t2.customer_email_domain
FROM
(SELECT
a.customer_id, count(a.customer_id) as c
FROM
(SELECT customer_id, reference_no
FROM seattle_police_govqa_audit_trails
WHERE customer_id NOT IN (5, 0, -1)
GROUP BY customer_id, reference_no) a
GROUP BY
a.customer_id
ORDER BY
count(a.customer_id) DESC) t1
INNER JOIN
(SELECT DISTINCT
customer_id,
INITCAP(LOWER(SUBSTRING(new_value FROM 'Dear (.*?):</div>'))) as customer_name,
LOWER(SUBSTRING(new_value FROM 'login:<b>(.*?)</b>')) as customer_email,
LOWER(SUBSTRING(new_value FROM 'login:<b>.*?@(.*?)</b>')) as customer_email_domain
FROM
seattle_police_govqa_audit_trails
WHERE
SUBSTRING(new_value FROM 'Dear (.*?):</div>') IS NOT NULL) t2 ON t1.customer_id = t2.customer_id
ORDER BY
t1.c DESC
SELECT DISTINCT
t1.new_value as requester_type, t2.customer_id
FROM
(SELECT
reference_no, new_value
FROM
seattle_police_govqa_audit_trails
WHERE
action_desc = 154) t1
INNER JOIN
(SELECT
reference_no, customer_id
FROM
seattle_police_govqa_audit_trails
WHERE
customer_id NOT IN (0, -1, 5)) t2 ON t1.reference_no = t2.reference_no
我尝试加入这两个人:
SELECT t1.customer_id,t3.requester_typer,t1.c,t2.customer_name,t2.customer_email,t2.customer_email_domain,t2.customer_email_domain_tld FROM (SELECT a.customer_id,count(a.customer_id) as c FROM (SELECT customer_id, reference_no FROM seattle_police_govqa_audit_trails WHERE customer_id NOT IN (5,0,-1) GROUP BY customer_id,reference_no) a GROUP BY a.customer_id ORDER BY count(a.customer_id) DESC) t1
INNER JOIN (SELECT DISTINCT customer_id,INITCAP(LOWER(SUBSTRING(new_value FROM 'Dear (.*?):</div>'))) as customer_name,LOWER(SUBSTRING(new_value FROM 'login:<b>(.*?)</b>')) as customer_email, LOWER(SUBSTRING(new_value FROM 'login:<b>.*?@(.*?)</b>')) as customer_email_domain, LOWER(SUBSTRING(new_value FROM 'login:<b>.*?@.*?\.(.*?)</b>')) as customer_email_domain_tld FROM seattle_police_govqa_audit_trails WHERE SUBSTRING(new_value FROM 'Dear (.*?):</div>') IS NOT NULL) t2
ON t1.customer_id = t2.customer_id ORDER BY t1.c DESC
INNER JOIN (SELECT DISTINCT t1.new_value as requester_type,t2.customer_id FROM (SELECT reference_no,new_value FROM seattle_police_govqa_audit_trails WHERE action_desc = 154) t1
INNER JOIN (SELECT reference_no,customer_id FROM seattle_police_govqa_audit_trails WHERE customer_id NOT IN (0,-1,5)) t2
ON t1.reference_no = t2.reference_no) as t3
ON t2.customer_id = t3.customer_id
我在INNER&#34;
附近收到错误&#34;语法错误答案 0 :(得分:3)
SQL查询中的问题是您试图将ORDER BY
保留在中间位置。必须将ORDER BY
子句一直移动到查询的后面,因为排序适用于整个查询,而不是其部分。
答案 1 :(得分:1)
试试这个:
SELECT t1.customer_id,t3.requester_typer,t1.c,t2.customer_name,t2.customer_email,t2.customer_email_domain,t2.customer_email_domain_tld FROM (SELECT a.customer_id,count(a.customer_id) as c FROM (SELECT customer_id, reference_no FROM seattle_police_govqa_audit_trails WHERE customer_id NOT IN (5,0,-1) GROUP BY customer_id,reference_no) a GROUP BY a.customer_id ORDER BY count(a.customer_id) DESC) t1
INNER JOIN (SELECT DISTINCT customer_id,INITCAP(LOWER(SUBSTRING(new_value FROM 'Dear (.*?):</div>'))) as customer_name,LOWER(SUBSTRING(new_value FROM 'login:<b>(.*?)</b>')) as customer_email, LOWER(SUBSTRING(new_value FROM 'login:<b>.*?@(.*?)</b>')) as customer_email_domain, LOWER(SUBSTRING(new_value FROM 'login:<b>.*?@.*?\.(.*?)</b>')) as customer_email_domain_tld FROM seattle_police_govqa_audit_trails WHERE SUBSTRING(new_value FROM 'Dear (.*?):</div>') IS NOT NULL) t2
ON t1.customer_id = t2.customer_id
INNER JOIN (SELECT DISTINCT t1.new_value as requester_type,t2.customer_id FROM (SELECT reference_no,new_value FROM seattle_police_govqa_audit_trails WHERE action_desc = 154) t1
INNER JOIN (SELECT reference_no,customer_id FROM seattle_police_govqa_audit_trails WHERE customer_id NOT IN (0,-1,5)) t2
ON t1.reference_no = t2.reference_no) as t3
ON t2.customer_id = t3.customer_id
ORDER BY t1.c DESC