我有两个表格应用程序和潜在客户
申请表
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列
提前致谢
答案 0 :(得分: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
答案 1 :(得分: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;
答案 2 :(得分:0)
SELECT DISTINCT a.submitDate,
(SELECT count(*) FROM applications GROUP BY submitDate) as App,
(SELECT count(*) FROM leads GROUP BY submitDate) as Leads
唯一需要注意的是在必要时包含一堆where
,包括在子查询中。