0

我有两个表应用程序和潜在客户

申请表

submitDate | fName | lName
2010-11-15    joe    smith
2010-11-16    joe    smith
2010-11-15    joe    smith

线索表

submitDate | click | state
2010-11-15    1      ca
2010-11-16    1      ca
2010-11-16    1      ca
2010-11-15    1      ca
2010-11-15    1      ca
2010-11-15    1      ca
2010-11-15    1      ca
2010-11-15    1      ca

我想要一个查询来返回这个结果

submitDate | application | clicks | percent
2010-11-15    2            6          33%
2010-11-16    1            2          50%

我尝试了以下

SELECT `submitDate` , count(`submitDate`)  AS 'comp',(select sum(`click`) from `leads`

 WHERE `submitDate` between '2010-11-15' AND '2010-11-16' group by `submitDate`)as

 'clicks'  from `applications` WHERE `submitDate` between '2010-11-15' AND

 '2010-11-16' group by `submitDate`

这返回一个错误

#1242 - Subquery returns more than 1 row

并尝试了以下

SELECT `leads`.`submitDate` , count(`leads`.`submitDate`) 
 AS 'application',sum(`click`) as 'clicks'  
from `applications`,`leads` WHERE `leads`.`submitDate` between 
'2010-11-15' AND '2010-11-16' group by `leads`.`submitDate`

这将返回以下结果

submitDate | application | clicks
2010-11-15    60           60
2010-11-16    6            6

潜在客户表有 22 列 / 20 = 2010-11-15 和 2 2010-11-16 应用表与上表相同

我很抱歉冗长的解释,但要清楚

任何想法?

以及添加划分应用程序/潜在客户的新字段百分比,因此结果将有 4 列

提前致谢

4

3 回答 3

0
select d.date, coalesce(a.count, 0) as applications, coalesce(b.count, 0) as clicks
from 
(
    select date from applications
    union
    select date from leads
) d
left outer join (
    select date, count(*) as count
    from applications 
    group by date
) a on d.date = a.date
left outer join (
    select date, count(*) as count
    from leads
    group by date
) b on d.date = b.date
于 2010-11-17T16:23:02.203 回答
0
select a.submitdate
, count(a.submitdate)
, (select sum(clicks) from leads where submitdate = a.submitdate) 
, count(a.submitdate)/(select sum(clicks) from leads where submitdate = a.submitdate) percent
from applications a
group by a.submitdate;
于 2010-11-17T16:29:48.153 回答
0
SELECT DISTINCT a.submitDate,
  (SELECT count(*) FROM applications GROUP BY submitDate) as App,
  (SELECT count(*) FROM leads        GROUP BY submitDate) as Leads

唯一需要注意的是在必要时包含一堆where's,包括在子查询中。

于 2010-11-17T16:33:55.810 回答