Advertisement
Guest User

Untitled

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