Advertisement
Guest User

Untitled

a guest
Apr 20th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 2.66 KB | None | 0 0
  1. select
  2.     d.id
  3. from
  4.     portfolio as p
  5.     inner join debt as d on p.id = d.r_portfolio_id
  6.     inner join person as per on d.parent_id = per.id
  7.  
  8. -- u -- пользователи
  9.     inner join(
  10.         select wt.r_debt_id
  11.         from work_task wt left join users u on u.id = wt.r_user_id
  12.                         left join department dep on dep.dep = u.r_department_id
  13.         where
  14.             dep.name like '%Колл%' or
  15.             dep.name like '%Хард%' or
  16.             dep.name like '%УВВиИП%' or
  17.             dep.name like '%УВВ%' or
  18.             dep.name = 'УИП по СПБ и ЛО' or
  19.             dep.name = 'Межрегиональное взыскание' or
  20.             (u.id = -1 or
  21.             u.f like '%Не установлен%') and -- без закрепления
  22.             u.id not in (42,76) -- не Галицына, не Полосухин
  23.     ) u on u.r_debt_id = d.id
  24.  
  25. -- pk -- Контактность
  26.     inner join (
  27.         select distinct cl.r_debt_id as r_debt_id
  28.         from contact_log as cl
  29.             inner join debt as d on cl.r_debt_id =d.id
  30.             inner join portfolio as p on d.r_portfolio_id = p.id
  31.         where
  32.             ((p.parent_id in(82,80) and cast(p.sign_date as date)>'2016-01-01')
  33.                 or (p.parent_id in(49) and cast(p.sign_date as date) > '2016-09-01' ))
  34.             and cl.result>100000000
  35.             and (substring(cast(cl.result as varchar ),8,1)!= '9'
  36.                 or substring(cast(cl.result as varchar ),8,1)!= '6'
  37.                 or substring(cast(cl.result as varchar ),8,1)!= '7')
  38.     ) pk on pk.r_debt_id = d.id
  39.  
  40. -- ph -- телефоны
  41.     inner join (
  42.         select parent_id, id, number n
  43.         from phone
  44.         where
  45.             typ in (1, 2)
  46.             and status !=3
  47.             and (
  48.                 len(number) = 11
  49.                 or len(number2) = 11
  50.             )
  51.             and block_flag = 0
  52.             and (
  53.                 SUBSTRING(number, 1, 1) in (7,8)
  54.                 or SUBSTRING(number2, 1, 1) in (7,8)
  55.             )
  56.             and (
  57.                 SUBSTRING(number, 2, 3) != 800
  58.                 or SUBSTRING(number2, 2, 3) != 800
  59.             )
  60.             )ph on
  61.                 ph.parent_id = per.id
  62.  
  63. where
  64.     d.debt_sum >= 1000
  65.     and (
  66.         (p.parent_id in (80, 82) and --Сбер, Сетелм
  67.         cast(p.sign_date as date) between '20160101' and '20171231') or --2016год
  68.         (p.parent_id = 49 and -- БалтБанк
  69.         cast(p.sign_date as date) between '20160901' and cast(getdate() as date)) -->
  70.         )
  71.     and d.[status] not in (6,7,8,9,10,14,17)
  72.  
  73. -- Список отказников за посл 2 мес.   
  74.     and d.id not in (
  75.         select cl.r_debt_id
  76.         from contact_log as cl
  77.         where cl.result>100000000 and
  78.             substring(cast(cl.result as varchar ),8,2)= '05'
  79.             and cast(cl.dt as date) > dateadd(m,-2,cast(getdate() as date))
  80.         group by cl.r_debt_id
  81.     )
  82.  
  83. -- обещания > getdate()
  84.     and d.id not in (
  85.         select parent_id
  86.         from debt_promise
  87.         where prom_date>cast(getdate() as date)
  88.         group by parent_id
  89.     )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement