好的,我有一个我正在尝试使用的子查询,需要回答一个复杂的问题。有一个SQL工具用于生成报告。使用此工具,只要日期位于外部select语句中,您就可以选择日期。因此,您可以使用此工具从简单的select语句中选择日期范围,例如:
select date_entered, name from opportunities
使用该工具可以正常工作。
但是,我有一个SQL要求,我需要让每一列都是它自己的SQL语句(基本上是一系列子查询)。我的问题是 - 无论如何有子查询使用初始选择中的日期?以下是我必须执行的select语句示例:
SELECT
(
SELECT
count(
opportunities.id) AS "count_for_opp_id"
FROM
opportunities
LEFT JOIN opportunities_cstm ON opportunities.id = opportunities_cstm.id_c
WHERE
opportunities.deleted = '0'
AND ((curdate() - interval 0 day) <= opportunities.date_entered)
AND opportunities_cstm.lead_category_c = 'GM Internet'
AND opportunities_cstm.lead_type_c = 'Sales'
AND opportunities_cstm.partner_id_c LIKE '41%'
OR opportunities_cstm.partner_id_c LIKE '42%'
OR opportunities_cstm.partner_id_c LIKE '43%'
OR opportunities_cstm.partner_id_c LIKE '44%'
OR opportunities_cstm.partner_id_c LIKE '45%'
OR opportunities_cstm.partner_id_c LIKE '47%'
OR opportunities_cstm.partner_id_c LIKE '48%'
OR opportunities_cstm.partner_id_c LIKE '49%'
OR opportunities_cstm.lead_sub_type_c = 'GM 3rd Party'
)AS TOT_DIV_THIRD_PARTY_SALES_LEADS,
(
SELECT
count(opportunities.id) AS "count_for_opp_id"
FROM
opportunities left
JOIN opportunities_cstm ON opportunities.id = opportunities_cstm.id_c
WHERE
opportunities.deleted = '0'
AND ((curdate() - interval 0 day) <= opportunities.date_entered)
AND opportunities_cstm.lead_category_c = 'Dealer Web'
AND opportunities_cstm.lead_type_c = 'Sales'
AND opportunities_cstm.lead_sub_type_c = 'GM Dealer Website'
OR opportunities_cstm.lead_sub_type_c = 'DMA Landing Page'
)AS TOT_DEALER_WEBSITE_DMA_SALES_LEADS,
(
SELECT
count(opportunities.id) AS "count_for_opp_id"
FROM
opportunities left
JOIN opportunities_cstm ON opportunities.id = opportunities_cstm.id_c
JOIN leads_handling_objectives
WHERE
opportunities.deleted = '0'
AND ((curdate() - interval 0 day) <= opportunities.date_entered)
AND opportunities_cstm.lead_category_c = 'GM Internet'
AND opportunities_cstm.partner_id_c LIKE '41%'
OR opportunities_cstm.partner_id_c LIKE '42%'
OR opportunities_cstm.partner_id_c LIKE '43%'
OR opportunities_cstm.partner_id_c LIKE '44%'
OR opportunities_cstm.partner_id_c LIKE '45%'
OR opportunities_cstm.partner_id_c LIKE '47%'
OR opportunities_cstm.partner_id_c LIKE '48%'
OR opportunities_cstm.partner_id_c LIKE '49%'
OR opportunities_cstm.lead_sub_type_c = 'GM 3rd Party'
AND opportunities_cstm.time_followup_c <= leads_handling_objectives.resp_time_obj
AND opportunities_cstm.make_c = leads_handling_objectives.makes_carried
AND opportunities_cstm.time_followup_c IS NOT NULL
)AS NUM_OF_LEADS_RESP_ONTIME_DIVISIONAL_THIRD_PARTY,
(
SELECT
count(opportunities.id) AS "count_for_opp_id"
FROM
opportunities left
JOIN opportunities_cstm ON opportunities.id = opportunities_cstm.id_c
JOIN leads_handling_objectives
WHERE
opportunities.deleted = '0'
AND ((curdate() - interval 0 day) <= opportunities.date_entered)
AND opportunities_cstm.lead_category_c = 'Dealer Web'
AND opportunities_cstm.lead_sub_type_c = 'GM Dealer Website'
OR opportunities_cstm.lead_sub_type_c = 'DMA Landing Page'
AND opportunities_cstm.time_followup_c <= leads_handling_objectives.resp_time_obj
AND opportunities_cstm.make_c = leads_handling_objectives.makes_carried
AND opportunities_cstm.time_followup_c IS NOT NULL
)AS NUM_OF_LEADS_RESP_ONTIME_DEALERWEBSITE_DMA
如果查看此查询,您会注意到子查询的curdate ...&lt; = date_entered部分。我需要以某种方式在select语句的外层实现这个。但是,如果我将它放在外层,我不知道如何使子查询使用这样的日期范围。
我知道这是一个复杂的问题但是 - 那里的任何人都会有任何建议吗?谢谢!
(注意:此外,这不能是一个过程或一个函数。它必须是一个select语句或一个与报表工具一起使用的视图。)
还有一个编辑 - 上面SQL的一个更简单(更小)的片段可以集中在这里:
SELECT
(SELECT
count(
opportunities.id) AS "count_for_opp_id"
FROM
opportunities
LEFT JOIN opportunities_cstm ON opportunities.id = opportunities_cstm.id_c
WHERE
opportunities.deleted = '0'
AND ((curdate() - interval 0 day) <= opportunities.date_entered)
AND opportunities_cstm.lead_category_c = 'GM Internet'
AND opportunities_cstm.lead_type_c = 'Sales'
) AS TOT_DIV_THIRD_PARTY_SALES_LEADS
从长远来看,上面这个可能更容易阅读,更容易诊断。
谢谢!
答案 0 :(得分:1)
你的(较小的)查询缺少一个FROM。放入一个会给你一个挂钩来挂起curdate()函数......
SELECT (SELECT count(opportunities.id) AS "count_for_opp_id"
FROM opportunities LEFT JOIN opportunities_cstm ON opportunities.id = opportunities_cstm.id_c
WHERE opportunities.deleted = '0' AND
((curdate() - interval 0 day) <= opportunities.date_entered) AND
opportunities_cstm.lead_category_c = 'GM Internet' AND
opportunities_cstm.lead_type_c = 'Sales') AS TOT_DIV_THIRD_PARTY_SALES_LEADS
FROM any_table
LIMIT 1;
第二个建议......
SELECT <given date>,
(SELECT count(opportunities.id) AS "count_for_opp_id"
FROM opportunities LEFT JOIN opportunities_cstm ON opportunities.id = opportunities_cstm.id_c
WHERE opportunities.deleted = '0' AND
(<given date> <= opportunities.date_entered AND
opportunities_cstm.lead_category_c = 'GM Internet' AND
opportunities_cstm.lead_type_c = 'Sales') AS TOT_DIV_THIRD_PARTY_SALES_LEADS
FROM any_table
LIMIT 1;
给定日期可以是文字或变量,也可以出现在any_table中;它会很好地进入子查询。