Advertisement
Guest User

Untitled

a guest
Jul 17th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.17 KB | None | 0 0
  1. query = '''
  2. drop table ##pt
  3. select distinct inn into ##pt from OOS.dbo.ParticipantTable as partT with(nolock)
  4. where cast([FirstInsertDate] as date)>'2015-01-01' and partT.inn in {0}
  5.  
  6. select distinct
  7. nt.notificationNumber 'id_procedure',
  8. lt.ordinalNumber 'id_lot',
  9. partT.inn 'inn_ich',
  10. isnull(d.prsAdmitted,'0') 'admitted',
  11. isnull(w.prsWinner,'0') 'is_winner',
  12. nt.[EndDate] 'end_date',
  13. isnull(dct.[СonclusionDate],dct.[PublishDate]) 'date_contract',
  14. ct.regnum 'num_contract',
  15. pa.inn 'inn_postav'
  16.  
  17. from ##pt t
  18. inner join OOS.dbo.ParticipantTable as partT with(nolock) on t.inn=partT.inn
  19. inner join OOS.dbo.applicationParticipantLinkTable as apppartlT with(nolock) on apppartlT.participantLink=partT.GUID
  20. INNER JOIN OOS.dbo.applicationTable AS appT WITH (NOLOCK) ON appT.GUID=apppartlT.applicationParticipantGUIDLink
  21. INNER JOIN OOS.dbo.protocolLotTable AS plt WITH (NOLOCK) ON plt.GUID = appT.applicationGUIDLink
  22. inner join OOS.dbo.ProtocolTable  protT with(nolock) on protT.GUID = plt.protocolLotGUIDLink and ((prs_cancel IS NULL OR prs_cancel = 0) and (protT.actual is null or protT.actual=1))
  23. inner join [OOS].[dbo].[NotificationTable] nt WITH(NOLOCK) on protT.notificationNumber=nt.notificationNumber
  24. cross apply (select top 1 * from OOS.dbo.ProtocolTable  prt with(nolock) where (prs_cancel IS NULL OR prs_cancel = 0) and (protT.actual is null or protT.actual=1) and prt.notificationNumber=nt.notificationNumber order by protT.[protocolNumber]) prtt
  25. inner join [OOS].[dbo].[LotTable] lt WITH(NOLOCK) on lt.lotGUIDLink = nt.guid and isnull([lotNumber],1)=lt.ordinalNumber and prtt.id=protT.id
  26. outer apply (SELECT DISTINCT dense_rank () OVER (partition by protT.notificationNumber,plt.lotNumber order by protT.[protocolNumber] desc) id,
  27. [journalNumber]
  28. ,protT.notificationNumber
  29. ,plt.[lotNumber]
  30. ,protT.[protocolNumber]
  31. ,appt.PrsAdmitted
  32. from OOS.dbo.ProtocolTable  protT with(nolock)
  33. INNER JOIN OOS.dbo.protocolLotTable AS plt WITH (NOLOCK) ON protT.GUID = plt.protocolLotGUIDLink
  34. INNER JOIN OOS.dbo.applicationTable AS app WITH (NOLOCK) ON plt.GUID = app.applicationGUIDLink
  35. where (prs_cancel IS NULL OR prs_cancel = 0) and (protT.actual is null or protT.actual=1) and protT.notificationNumber=nt.notificationNumber and isnull(plt.[lotNumber],1)=lt.ordinalNumber and app.[journalNumber]=appT.journalNumber) d
  36. outer apply (SELECT DISTINCT dense_rank () OVER (partition by protT.notificationNumber,plt.lotNumber order by protT.[protocolNumber] desc) id,
  37. [journalNumber]
  38. ,protT.notificationNumber
  39.                    ,plt.[lotNumber]
  40. ,protT.[protocolNumber]
  41. ,appt.PrsWinner
  42. from OOS.dbo.ProtocolTable  protT with(nolock)
  43. INNER JOIN OOS.dbo.protocolLotTable AS plt WITH (NOLOCK) ON protT.GUID = plt.protocolLotGUIDLink
  44. INNER JOIN OOS.dbo.applicationTable AS app WITH (NOLOCK) ON plt.GUID = app.applicationGUIDLink
  45. where (prs_cancel IS NULL OR prs_cancel = 0) and (protT.actual is null or protT.actual=1) and app.PrsWinner=1 and protT.notificationNumber=nt.notificationNumber and isnull(plt.[lotNumber],1)=lt.ordinalNumber and app.journalNumber=appT.journalNumber) w
  46. left join [OOS].[dbo].[ContractTable] ct with(nolock) on lt.id=ct.lotid
  47. left join dicts.dbo.ContractTable dct WITH(NOLOCK) on dct.guid=ct.guid
  48. left join [OOS].[dbo].[applicationParticipantLinkTable] apa with(nolock) on apa.[applicationParticipantGUIDLink]=ct.guid
  49. left join [OOS].[dbo].[ParticipantTable] pa with(nolock) on pa.guid = apa.[participantLink]
  50. where (d.id=1 or d.id is null) and (w.id=1 or w.id is null) and partT.inn in {0}
  51.  
  52. union
  53.  
  54. select
  55. null 'id_procedure',
  56. null 'id_lot',
  57. partT.inn 'inn_participant',
  58. null 'admitted',
  59. '1' as 'is_winner',
  60. null 'end_date',
  61. isnull(dct.[СonclusionDate],dct.[PublishDate]) 'date_contract',
  62. ct.regnum 'regnum_contract',
  63. partT.inn 'inn_provider'
  64. from ##pt t
  65. inner join OOS.dbo.ParticipantTable as partT with(nolock) on t.inn=partT.inn
  66. inner join [OOS].[dbo].[applicationParticipantLinkTable] apa with(nolock) on partT.guid = apa.[participantLink]
  67. inner join oos.dbo.ContractTable ct WITH(NOLOCK) on apa.[applicationParticipantGUIDLink]=ct.guid
  68. inner join dicts.dbo.ContractTable dct WITH(NOLOCK) on dct.guid=ct.guid and dct.[ContractTypeID] in (9,40)
  69. where partT.inn in {0}
  70. '''.format(inn)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement