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

猜你喜欢

转载自www.cnblogs.com/simly/p/9461041.html
今日推荐