oracle data processing sql

--Insert into
z_nm_site_ent(site_ent_id,ent_name) select site_ent_id,shop_name from z_nm_test --This part

of the data is directly inserted into the main body and the store, but it needs to be associated and does not exist in the main table the
select *
  from Z_NM_SHOP_ALL a
where exists (select 1
          from (select count(1), shop_name
                  from Z_NM_SHOP_ALL
                 group by shop_name
                having(count(1) = 1)) b where a.shop_name=b.shop_name) and zc_name is null
               
- - This part of the data has duplicate names, so the main body can only insert one (query the number of data with duplicate name)
select count(1), shop_name from Z_NM_SHOP_ALL where zc_name is null group by shop_name having(count(1)>1)
     -- The stores corresponding to these names need to be associated
    
-- zc_name is not empty
     select count(1),zc_name from Z_NM_SHOP_ALL where zc_name is not null group by zc_name
      -- also associate the subject ID


-- replace the null characters in the time field
update z_nm_evment_all set EV_TIME=replace(EV_TIME,' ','')
where length(EV_TIME)>10 and EV_TIME like '% %' and length(EV_TIME)<12


-- oracle judge
select case
when
length(EV_TIME)>10
then
to_date(EV_TIME,'yyyy-MM-dd hh24:mi:ss' )




--splicing , the data without date defaults to the 1st of a month
update z_nm_evment_all set EV_TIME=EV_TIME || '/1' where length(EV_TIME)=6


--Query the number of times the specified string appears in this field
SELECT LENGTHB( TRANSLATE(EV_TIME,'/'|| EV_TIME,'/')) FROM z_nm_evment_all;

update z_nm_evment_all set  EV_TIME=EV_TIME || '/1' where LENGTHB(TRANSLATE(EV_TIME,'/'|| EV_TIME,'/'))!=2;


-- 评论插入sql

insert into z_nm_evaluation(EV_ID,SHOP_ID,EV_TIME,EV_USER,EV_CONTENT,IS_BAD,GOOD_ID)
select EV_ID,SHOP_ID,
case when
length(EV_TIME)>10
then
to_date(EV_TIME,'yyyy-MM-dd hh24:mi:ss')
else
to_date(EV_TIME,'yyyy-MM-dd')
end
,EV_USER,EV_CONTENT,IS_BAD,GOOD_ID from z_nm_evment_all


--- 两表关联 批量修改sql
update z_nm_site_ent a set a.ssgss=(select b.ssgss from z_nm_site_ent_info b where b.ent_name=a.ent_name and rownum =1)
where a.ent_name in (select b.ent_name from  z_nm_site_ent_info b)


Modify the duplicate data in the same table, update the data field whose field is not empty to another data field whose field is empty, z_nm_shop_id is the identification intermediate table of duplicate data (select shop_id from nm_shop group by shop_id having(count(1 )>1) For example, the shop_id of the repeated data is the same)

-- modify the repeated data field of the shop, and then keep an

update nm_shop w
   set w.ev_num =
       (select ev_num
          from nm_shop b
         where exists
         (select 1 from z_nm_shop_id h where h.shop_id = b.shop_id)
           and ev_num is not null
           and rownum = 1)
where w.ev_num is null
and exists (select 1 from z_nm_shop_id n where n.shop_id=w.shop_id)

--- batch modification operation, and only execute one

update a_a_nm_shop_45 a set a.type_code=
(select b.shop_sub_type from nm_shop_type b where a.shop_id=b.shop_id and rownum=1)






--Delete duplicate
delete from z_reg_bus_ent a
where rowid!=(select max(rowid) from z_reg_bus_ent b where a.ent_name=b.ent_name) --Query

time period data
select * from nm_evaluation where to_char(to_date(ev_time),'yyyy -mm-dd') like '%2017-04%' --Insert




comment data
insert into nm_evaluation
select ev_id, shop_id,to_date(ev_time,'yyyy-mm-dd'),ev_user,to_number(ev_num),ev_content,is_bad ,add_time,ev_title,good_id from a_a_nm_evment_45 --Statistical



comments select
a.shop_id as shop ID, a.shop_name as shop name,
a.platform_code as platform ID, a.shop_address as shop address, a.shop_url as url,
a. ev_num as total number of reviews, a.bad_ev_num as total number of bad reviews,
(select count(1) from nm_evaluation b where b.shop_id=a.shop_id and to_char(to_date(ev_time),'yyyy-mm-dd')  like '%2017-01%') as 啊1月总评论数,
(select count(1) from nm_evaluation b where b.shop_id=a.shop_id and b.is_bad='1' and to_char(to_date(ev_time),'yyyy-mm-dd')  like '%2017-01%') as 啊1月差评论数,
(select count(1) from nm_evaluation b where b.shop_id=a.shop_id and to_char(to_date(ev_time),'yyyy-mm-dd')  like '%2017-02%') as 啊2月总评论数,
(select count(1) from nm_evaluation b where b.shop_id=a.shop_id and b.is_bad='1' and to_char(to_date(ev_time),'yyyy-mm-dd')  like '%2017-02%') as 啊2月差评论数,
(select count(1) from nm_evaluation b where b.shop_id=a.shop_id and to_char(to_date(ev_time),'yyyy-mm-dd')  like '%2017-03%') as ah total comments in March,
(select count(1) from nm_evaluation b where b.shop_id=a.shop_id and b.is_bad='1' and to_char(to_date(ev_time),'yyyy-mm-dd')  like '%2017-03%') as 啊3月差评论数,
(select count(1) from nm_evaluation b where b.shop_id=a.shop_id and (to_char(to_date(ev_time),'yyyy-mm-dd')  like '%2017-04%' or to_char(to_date(ev_time),'yyyy-mm-dd')  like '%2017-05%' )) as 啊45月总评论数,
(select count(1) from nm_evaluation b where b.shop_id=a.shop_id and b.is_bad='1' and (to_char(to_date(ev_time),'yyyy-mm-dd')  like '%2017-04%' or to_char(to_date(ev_time),'yyyy-mm-dd')  like '%2017-05%' )) as 啊45月差评论数

from aa_nm_shop_1_5 a

Guess you like

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