SQL multi-table calculation problem

The data in the mc_devappear table can calculate the number of new devices added that day, and the data stored in the launch2 table can calculate the number of all devices launched that day. Calculate the ratio of the two with a sentence of sql (number of new devices/number of all devices)

[1] First edition
with temp as(
	select  count(distinct deviceid)  as newdevnum from ana_fx_middle.mc_devappear  where first_appear_hdfspar ='20161214' and hdfs_par='20161214'
	full join
	select count(distinct deviceid) as devicenum from src_huidu_mc.launch2 where hdfs_par ='20161214'
	)
select newdevnum/devicenum from temp


defect:
AnalysisException: Syntax error in line 3: full join ^ Encountered: FULL Expected: AND, BETWEEN, DIV, GROUP, HAVING, ILIKE, IN, IREGEXP, IS, LIKE, LIMIT, NOT, OFFSET, OR, ORDER, REGEXP, RLIKE, UNION CAUSED BY: Exception: Syntax error

【2】Second Edition
with temp as(  
	select * from
		(select '1' as id,  count(distinct deviceid)  as newdevnum from ana_fx_middle.mc_devappear  where first_appear_hdfspar ='20161214' and hdfs_par='20161214') as tmp1
		left join
		(select '1' as id,count(distinct deviceid) as devicenum from src_huidu_mc.launch2 where hdfs_par ='20161214' ) as tmp2
	  on tmp1.id=tmp2.id
  )
  select newdevnum/devicenum from temp  


Defect: cannot be calculated when devicenum is 0
[3] Third Edition
with temp as(  
	select * from
		(select '1' as id,  count(distinct deviceid)  as newdevnum from ana_fx_middle.mc_devappear  where first_appear_hdfspar ='20161214' and hdfs_par='20161214') as tmp1
		left join
		(select '1' as id,count(distinct deviceid) as devicenum from src_huidu_mc.launch2 where hdfs_par ='20161214' ) as tmp2
	  on tmp1.id=tmp2.id
  )
  select (cast(newdevnum as bigint))/(if cast(devicenum as bigint)=0,1,cast(devicenum as bigint)) from temp  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326445361&siteId=291194637