我正在使用此查询遇到很多问题。它似乎没有正确计算行数,而且它缺少很多。
基本上,我在这里使用了2个表。一个用于leads
,另一个用于assignments
。每个潜在客户都可以多次分配,因此它可以在assignments
表中有多行。 dealerships
表仅用于确保它不计算某些经销商的行数。
我想在这里做的只是计算来自每个网站的潜在客户数量,并按日,周,月等细分。
以下是查询中使用的每个表的结构:
--
-- Table structure for table `assignments`
--
CREATE TABLE `assignments` (
`id` int(11) NOT NULL auto_increment,
`id_dealership` int(11) NOT NULL,
`id_lead` int(11) NOT NULL,
`date_assigned` int(11) NOT NULL,
`website` varchar(255) NOT NULL default '',
`make` varchar(255) NOT NULL default '',
`model` varchar(255) NOT NULL default '',
`ip_address` varchar(255) NOT NULL default '',
`is_reassign` varchar(255) NOT NULL default 'no',
`manual_or_auto` varchar(255) NOT NULL default 'N/A',
`assigned_by` varchar(255) NOT NULL default 'N/A',
PRIMARY KEY (`id`),
KEY `id_dealership` (`id_dealership`),
KEY `date_assigned` (`date_assigned`),
KEY `id_lead` (`id_lead`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=26987 ;
--
-- Table structure for table `dealerships`
--
CREATE TABLE `dealerships` (
`id` int(11) NOT NULL auto_increment,
`province` varchar(255) NOT NULL default '',
`city` varchar(255) NOT NULL default '',
`name` varchar(255) NOT NULL,
`email` text NOT NULL,
`email_subject` varchar(255) NOT NULL default 'Car Lead',
`type` varchar(255) NOT NULL,
`make` varchar(255) NOT NULL,
`leads` int(11) NOT NULL default '0',
`status` varchar(255) NOT NULL,
`low_notif` int(11) NOT NULL,
`reassign_id` int(11) NOT NULL,
`reassign_days` int(11) NOT NULL,
`salesman` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=190 ;
--
-- Table structure for table `leads`
--
CREATE TABLE `leads` (
`id` int(11) NOT NULL auto_increment,
`id_inf` int(11) NOT NULL,
`name_first` varchar(255) NOT NULL,
`name_last` varchar(255) NOT NULL,
`email` varchar(255) NOT NULL,
`phone_home` varchar(255) NOT NULL,
`phone_cell` varchar(255) NOT NULL,
`phone_work` varchar(255) NOT NULL,
`postcode` varchar(255) NOT NULL,
`website` varchar(255) NOT NULL,
`address` varchar(255) NOT NULL,
`province` varchar(255) NOT NULL,
`employer` varchar(255) NOT NULL,
`city` varchar(255) NOT NULL,
`rentorown` varchar(255) NOT NULL,
`emp_months` varchar(255) NOT NULL,
`emp_years` varchar(255) NOT NULL,
`sin` varchar(255) NOT NULL,
`occupation` varchar(255) NOT NULL,
`monthly_income` varchar(255) NOT NULL,
`bankruptcy` varchar(255) NOT NULL,
`tradein` varchar(255) NOT NULL,
`cosign` varchar(255) NOT NULL,
`monthly_payment` varchar(255) NOT NULL,
`residence_years` varchar(255) NOT NULL,
`residence_months` varchar(255) NOT NULL,
`birthday` varchar(255) NOT NULL,
`make` varchar(255) NOT NULL,
`model` varchar(255) NOT NULL,
`date_added` int(11) NOT NULL,
`ip_address` varchar(255) NOT NULL default '',
`time_to_call` varchar(255) NOT NULL,
`referrer` varchar(255) NOT NULL default '',
`source` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
KEY `id_infusionsoft` (`id_infusionsoft`),
KEY `date_added` (`date_added`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=22488 ;
以下是我正在使用的查询:
select
l.website,
sum(case when date_format(from_unixtime(COALESCE(a.date_assigned, l.date_added)), '%Y-%m-%d') = date_format(now(), '%Y-%m-%d') then 1 else 0 end) AS c_day,
sum(case when date_format(from_unixtime(COALESCE(a.date_assigned, l.date_added)), '%Y-%m-%d') = date_format(now() - interval 1 day, '%Y-%m-%d') then 1 else 0 end) AS c_yesterday,
sum(case when date_format(from_unixtime(COALESCE(a.date_assigned, l.date_added)), '%Y-%m-%d') = date_format(now() - interval 2 day, '%Y-%m-%d') then 1 else 0 end) AS c_2_days,
sum(case when date_format(from_unixtime(COALESCE(a.date_assigned, l.date_added)), '%Y-%m-%d') = date_format(now() - interval 3 day, '%Y-%m-%d') then 1 else 0 end) AS c_3_days,
sum(case when date_format(from_unixtime(COALESCE(a.date_assigned, l.date_added)), '%Y-%m-%d') = date_format(now() - interval 4 day, '%Y-%m-%d') then 1 else 0 end) AS c_4_days,
sum(case when date_format(from_unixtime(COALESCE(a.date_assigned, l.date_added)), '%Y-%m-%d') = date_format(now() - interval 5 day, '%Y-%m-%d') then 1 else 0 end) AS c_5_days,
sum(case when date_format(from_unixtime(COALESCE(a.date_assigned, l.date_added)), '%Y-%m-%d') = date_format(now() - interval 6 day, '%Y-%m-%d') then 1 else 0 end) AS c_6_days,
sum(case when date_format(from_unixtime(COALESCE(a.date_assigned, l.date_added)), '%Y-%m-%d') = date_format(now() - interval 7 day, '%Y-%m-%d') then 1 else 0 end) AS c_7_days,
sum(case when YEARWEEK(FROM_UNIXTIME(COALESCE(a.date_assigned, l.date_added))) = YEARWEEK(CURDATE()) then 1 else 0 end) AS c_week,
sum(case when date_format(from_unixtime(COALESCE(a.date_assigned, l.date_added)), '%Y-%m')= date_format(now(), '%Y-%m') then 1 else 0 end) AS c_month,
sum(case when date_format(from_unixtime(COALESCE(a.date_assigned, l.date_added)), '%Y-%m')= date_format(now() - interval 1 month, '%Y-%m') then 1 else 0 end) AS c_last_month,
sum(case when date_format(from_unixtime(COALESCE(a.date_assigned, l.date_added)), '%Y-%m')= date_format(now() - interval 2 month, '%Y-%m') then 1 else 0 end) AS c_2_months,
sum(case when date_format(from_unixtime(COALESCE(a.date_assigned, l.date_added)), '%Y-%m')= date_format(now() - interval 3 month, '%Y-%m') then 1 else 0 end) AS c_3_months,
sum(case when date_format(from_unixtime(COALESCE(a.date_assigned, l.date_added)), '%Y-%m')= date_format(now() - interval 4 month, '%Y-%m') then 1 else 0 end) AS c_4_months,
sum(case when date_format(from_unixtime(COALESCE(a.date_assigned, l.date_added)), '%Y-%m')= date_format(now() - interval 5 month, '%Y-%m') then 1 else 0 end) AS c_5_months,
sum(case when date_format(from_unixtime(COALESCE(a.date_assigned, l.date_added)), '%Y-%m')= date_format(now() - interval 6 month, '%Y-%m') then 1 else 0 end) AS c_6_months,
sum(case when date_format(from_unixtime(COALESCE(a.date_assigned, l.date_added)), '%Y')= date_format(now(), '%Y') then 1 else 0 end) AS c_year
from `leads` as l
left join `assignments` as a on (a.id_lead = l.id)
left join `dealerships` as d on (d.id = a.id_dealership)
where a.is_reassign='no' and a.id_dealership not in ('65', '77', '89', '138', '175', '177')
group by l.website
order by l.website asc
有一点需要注意的是,有时导致date_added
表中缺少leads
字段,但导致date_assigned
的{{1}}存在反之亦然。
为什么我的查询无法正常运行?任何帮助将不胜感激。