我现在需要根据咨询表和消费表得到一个合计报表(均为Excel),老板要求我在SQL中得到合计报表需要的所有数据:
如下是我会用到的Excel表格:
渠道表:
类型表:
消费表:
咨询表:
最终得到这张合计报表:
由于合计报表中需要的是产品类型,所以我在写代码的时候用了left join 把消费表,咨询表中的“产品”,“咨询产品”转化为“类型”
我写的太乱了就不展示了,
跪求大神们帮助一下我这个职场小渣渣!! 谢谢
一条sql不好查,可以分成多个sql啊
select b.类型,count(d.咨询产品) '上门量(含复购)',
sum(case isnull(d.说明,N'复购') when N'复购' then 0 else 1 end) '上门量(不含复购)',
sum(case isnull(d.状态,0) when N'成交' then 1 else 0 end) '成交量',
cast(case count(d.咨询产品) when 0 then 'NA' else convert(decimal(5,2),
cast(sum(case isnull(d.状态,0) when N'成交' then 1 else 0 end) as float)/count(d.咨询产品)*100)
end as varchar)+'%' '成交率',
sum(case isnull(d.说明,N'复购') when N'初购' then 1 else 0 end) '新客上门量',
sum(case isnull(d.状态,0) when N'成交'
then case isnull(d.说明,N'复购') when N'初购'
then 1 else 0 end else 0 end) '新客成交量',
case sum(case isnull(d.说明,N'复购') when N'初购' then 1 else 0 end) when 0 then 'NA'
else cast(convert(decimal(5,2),cast(sum(case isnull(d.状态,0) when N'成交' then
case isnull(d.说明,N'复购') when N'初购' then 1
else 0 end else 0 end) as float)/sum(case isnull(d.说明,N'复购') when N'初购' then 1 else 0 end)*100)as varchar)+'%' end '新客成交率',
sum(case isnull(d.说明,N'复购') when N'初购' then c.收款金额 else 0 end) '新客业绩',
--新客单价
case sum(case isnull(d.状态,0) when N'成交' then case isnull(d.说明,N'复购')
when N'初购' then 1 else 0 end else 0 end)
when 0 then 'NA'
else cast(cast(sum(case isnull(d.说明,N'复购') when N'初购' then c.收款金额 else 0 end) as float)/sum(case isnull(d.状态,0)
when N'成交' then case isnull(d.说明,N'复购') when N'初购' then 1 else 0 end else 0 end) as varchar) end '新客单价',
sum(case isnull(d.说明,N'初购') when N'初购' then 0 else 1 end) '老客上门量',
sum(case isnull(d.状态,0) when N'成交' then case isnull(d.说明,N'初购') when N'初购' then 0 else 1 end else 0 end) '老客成交量',
case sum(case isnull(d.说明,N'初购') when N'初购' then 0 else 1 end) when 0 then 'NA' else cast(convert(decimal(5,2),cast(sum(case isnull(d.状态,0) when N'成交' then case isnull(d.说明,N'初购') when N'初购' then 0 else 1 end else 0 end) as float)/sum(case isnull(d.说明,N'初购') when N'初购' then 0 else 1 end)*100) as varchar)+'%' end '老客成交率',
sum(case isnull(d.说明,N'初购') when N'初购' then 0 else c.收款金额 end) '老客业绩',
cast(case sum(case isnull(d.状态,0) when N'成交' then case isnull(d.说明,N'初购') when N'初购' then 0 else 1 end else 0 end) when 0 then 'NA' else cast(sum(case isnull(d.说明,N'初购') when N'初购' then 0 else c.收款金额 end) as float)/sum(case isnull(d.状态,0) when N'成交' then case isnull(d.说明,N'初购') when N'初购' then 0 else 1 end else 0 end) end as varchar) '老客单价',
sum(case a.渠道 when N'自营' then 1 else 0 end) '自营',
sum(case a.渠道 when N'会所' then 1 else 0 end) '会所'
from testtable_csdn180426_b b
left join testtable_csdn180426_d d on b.产品=d.咨询产品
left join testtable_csdn180426_cc c on c.客户卡号=d.客户卡号
and
c.产品=d.咨询产品
and
c.信息来源=d.信息来源
left join testtable_csdn180426_a a on d.信息来源=a.信息来源
group by b.类型
--注:这里的"不含复购"仅不含复购,包含再消费
testtable_csdn180426_a 渠道表
testtable_csdn180426_b 类型表
testtable_csdn180426_cc 消费表
testtable_csdn180426_d 咨询表
--表结构:
create table testtable_csdn180426_a (渠道 nvarchar(20),信息来源 nvarchar(20))
create table testtable_csdn180426_b (类型 nvarchar(20),产品 nvarchar(20))
create table testtable_csdn180426_c (营业日期 char(10),客户卡号 char(8),产品 nvarchar(20),收款金额 int,信息来源 nvarchar(20))
create table testtable_csdn180426_d
(说明 nvarchar(20),状态 nvarchar(20),客户卡号 char(8), 咨询日期 char(10), 咨询产品 nvarchar(20), 信息来源 nvarchar(20))