day 05 账单

-更新数据 下面的换成上面的--
update sdata.dbo.billing_data set Product_Group_Ops ='APPLE2'
where Product_Group_Ops ='OPPO'
update sdata.dbo.billing_data set Product_Group_Ops ='APPLE2'
where Product_Group_Ops ='APPLE'
update sdata.dbo.billing_data set Product_Group_Ops ='APPLE'
where Product_Group_Ops ='APPLE2'

select *from sdata.dbo.billing_data
delete from sdata.dbo.billing_data
select * from sdata.dbo.cost_detail
select * from sdata.dbo.month_rate
--日期格式转换成一致--
select cast(month as date),* from sdata.dbo.month_rate

--两个表关联 所花总费用sdata.dbo.billing_data跟费率表sdata.dbo.month_rate关联--
select from sdata.dbo.billing_data a
left join sdata.dbo.month_rate b
on a.currency = b.currency
and cast(a.Billing_Month as date) = CAST(b.month as date)

--计算每个国家的订单数--
select
country_id
,product_group_ops

--,case when service_order_creation_date ='00:00.0' then null
--else cast(service_order_creation_date as date) end
--CONVERT(varchar(20),GETDATE(),120)显示日期的格式 case when 转换00:00.0--

,CONVERT(varchar(7),case when service_order_creation_date ='00:00.0' then null
else cast(service_order_creation_date as date) end,120) mounth --别名--
,COUNT(1) num_shell --别名num_shell --
into sdata.dbo.gdanj --新建一张表--
from sdata.dbo.cost_detail
group by
country_id
,product_group_ops
,CONVERT(varchar(7),case when service_order_creation_date ='00:00.0' then null
else cast(service_order_creation_date as date) end,120)


--指定显示sdata.dbo.billing_data表里面哪些字段,然后将总钱数*费率 运行报错 先cost 将字符串类型转换成 flost类型--
--Total_Amount as doller 放在from前 b.*显示b表的所有列--

--计算汇率用美元表示多少钱--
drop table sdata.dbo.bing_deal
select Product_Group_Ops ,country_id,invoice,a.billing_month
,b.*
,CAST(a.Total_Amount as float)*CAST(b.Loc_to_USD as float) money
into sdata.dbo.bing_deal --新建一张表--
from sdata.dbo.billing_data a
left join sdata.dbo.month_rate b
on a.currency = b.currency
and cast(a.Billing_Month as date) = CAST(b.month as date)
select * from sdata.dbo.bing_deal

--某个国家在某月的账单总钱数--
drop table sdata.dbo.data_sum
select
country_id
,Product_Group_Ops
,convert (varchar(7),CAST(billing_month as date),120) month
,SUM(money) money
into sdata.dbo.data_sum
from sdata.dbo.bing_deal
group by country_id
,Product_Group_Ops
,convert (varchar(7),CAST(billing_month as date),120)

select * from sdata.dbo.data_sum
oppo
apple

--关联表 看总表-
select * from sdata.dbo.data_sum a
left join sdata.dbo.bing_deal b
on a.Country_ID = b.Country_ID
and a.month= b.Month
and a.Product_Group_Ops = b.Product_Group_Ops


--钱数 除以订单数--
select
a.country_id
,a.month
,a.Product_Group_Ops
,ISNULL(b.num_shell,0) num_shell
,case when ISNULL(b.num_shell,0) = 0 then 0
else a.money/b.num_shell end
from sdata.dbo.data_sum a
left join
sdata.dbo.gdanj b
on a.Country_ID = b.Country_ID
and a.month= b.mounth
and a.Product_Group_Ops = b.Product_Group_Ops

--计算每个国家的总数--
select country,sum(cast(Total_Amount as float) ) from sdata.dbo.billing_data
group by country

--老师的代码--

select * from sdata.dbo.month_rate
select * from sdata.dbo.cost_detail
select MAX(cast(billing_month as DATE)) from sdata.dbo.billing_data


update sdata.dbo.billing_data set product_group_ops='APPLE2'
where product_group_ops='OPPO'


update sdata.dbo.billing_data set product_group_ops='OPPO'
where product_group_ops='APPLE'


update sdata.dbo.billing_data set product_group_ops='APPLE'
where product_group_ops='APPLE2'

--单子国家品牌日期 订单数
select
country_id
,product_group_ops
--,case when service_order_creation_date ='00:00.0' then null
-- else CAST(service_order_creation_date as date) end
,CONVERT(varchar(7),case when service_order_creation_date ='00:00.0' then null
else CAST(service_order_creation_date as date) end,120) month
,COUNT(1) num_sell
into sdata.dbo.cost_detail_month_sell_num
from sdata.dbo.cost_detail
group by
country_id
,product_group_ops
,CONVERT(varchar(7),case when service_order_creation_date ='00:00.0' then null
else CAST(service_order_creation_date as date) end,120)

select * from sdata.dbo.billing_data

---汇率转换

drop table sdata.dbo.billing_data_deal
select product_group_ops,country_id,invoice,a.billing_month,cast(a.total_amount as float)*cast(b.loc_to_usd as float) money
into sdata.dbo.billing_data_deal
from sdata.dbo.billing_data a
left join sdata.dbo.month_rate b
on a.currency= b.currency
and cast(a.billing_month as DATE)= cast(b.month as DATE)


---各维度上的总钱
drop table sdata.dbo.billing_data_sum
select
country_id
,product_group_ops
,CONVERT(varchar(7),cast(billing_month as DATE),120) month
,SUM(money) money
into sdata.dbo.billing_data_sum
from
sdata.dbo.billing_data_deal
group by country_id
,product_group_ops
,CONVERT(varchar(7),cast(billing_month as DATE),120)

select
a.country_id
,a.month
,a.product_group_ops
,a.money
,isnull(b.num_sell,0) num_sell
,case when isnull(b.num_sell,0)= 0 then 0
else a.money/b.num_sell end avg_money
into sdata.dbo.avg_sell_money
from sdata.dbo.billing_data_sum a
left join
sdata.dbo.cost_detail_month_sell_num b
on a.country_id = b.country_id
and a.month =b.month
and a.product_group_ops=b.product_group_ops

---每个单子的钱
select * from sdata.dbo.cost_detail a
left join sdata.dbo.avg_sell_money b
on a.country_id=b.country_id
and a.product_group_ops =b.product_group_ops
and CONVERT(varchar(7),case when service_order_creation_date ='00:00.0' then null
else CAST(service_order_creation_date as date) end,120) =b.month
where b.avg_money is not null

原文地址:https://www.cnblogs.com/simly/p/9461041.html