Guest User

Untitled

a guest
Jan 19th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. use gemhV2;
  2. set @interval=13;
  3. set @marketId=1089;
  4. drop table if exists gemhs;
  5. #get all companies with their main market
  6. create temporary table gemhs
  7. select distinct gemhnumber,if(main=1,market,null) mainMarket from marketCompanyCpa where market=@marketId ;
  8.  
  9. #get the latest status change for all the companies for the dates we want
  10. drop table if exists maxStatus;
  11. create temporary table maxStatus
  12. select max(id) id,gemhnumber from StatusChange
  13. where date <=current_date - interval @`interval` month
  14. group by gemhnumber;
  15.  
  16. create index maxStatus_gemh_index
  17. on maxStatus (gemhnumber);
  18.  
  19. #from the companies we have from table gemhs get the actives on the dates we want
  20. drop table if exists gemhStatus;
  21. create temporary table gemhStatus
  22. select sc.*,g.mainMarket from gemhs g join maxStatus sc1 on g.gemhnumber=sc1.gemhnumber join StatusChange sc on sc.id=sc1.id and ((date<=current_date - interval @`interval` month
  23. and currentStatus in ('Ενεργή','Προεγγραφή'))
  24. or(
  25. month(date)=month(current_date - interval @`interval` month) and year(date)=year(current_date - interval @`interval` month)
  26. and currentStatus not in ('Ενεργή','Προεγγραφή')
  27. ));
  28.  
  29. drop table if exists fullData;
  30. create temporary table fullData
  31. select m.*,gs.currentStatus,gs.date,gs.mainMarket
  32. from gemhStatus gs join Main m on gs.gemhnumber=m.gemhnumber;
  33.  
  34. select * from fullData
  35. ;
  36.  
  37.  
  38.  
  39. #------- below are not tested------------------
  40. select f.mainMarket,mcc.*,M.* from
  41. fullData f join marketCompanyCpa mcc on f.gemhnumber=mcc.gemhnumber
  42. join Markets M on mcc.market = M.id
  43. join Markets m2 on m2.id=@marketId and m2.type=M.type
  44.  
  45. group by gemhnumber,market
  46.  
  47. ;
  48.  
  49. select
  50. sum(if(currentStatus in ('Ενεργή','Προεγγραφή'),1,0))`count actives`
  51. ,sum(if(mainMarket=@marketId,1,0))
  52. ,sum(if(market=@marketId,1,0))
  53. ,count(*),f.gemhnumber
  54. from
  55. fullData f join marketCompanyCpa mcc on f.gemhnumber=mcc.gemhnumber
  56. join Markets M on mcc.market = M.id
  57. join Markets m2 on m2.id=@marketId and m2.type=M.type
  58. group by f.gemhnumber
  59. ;
Add Comment
Please, Sign In to add comment