Advertisement
Guest User

Untitled

a guest
Jul 17th, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.88 KB | None | 0 0
  1. import pymysql.cursors
  2. import pandas as pd
  3. import pymssql
  4.  
  5. connection = pymssql.connect(server='virt135.aetp.nn',
  6.                              user='AETP\\popova.a',
  7.                              password='WiW7RWfbQW',
  8.                              charset='cp1251')
  9.  
  10.  
  11. cursor = connection.cursor()
  12. query = '''
  13. drop table ##pt
  14. select distinct top 2 inn into ##pt from OOS.dbo.ParticipantTable as partT with(nolock) where cast([FirstInsertDate] as date)>'2015-01-01'
  15.  
  16. select distinct
  17. nt.notificationNumber 'id_procedure',
  18. lt.ordinalNumber 'id_lot',
  19. partT.inn 'inn_ich',
  20. isnull(d.prsAdmitted,'0') 'admitted',
  21. isnull(w.prsWinner,'0') 'is_winner',
  22. nt.[EndDate] 'end_date',
  23. isnull(dct.[СonclusionDate],dct.[PublishDate]) 'date_contract',
  24. ct.regnum 'num_contract',
  25. pa.inn 'inn_postav'
  26.  
  27. from ##pt t
  28. inner join OOS.dbo.ParticipantTable as partT with(nolock) on t.inn=partT.inn
  29. inner join OOS.dbo.applicationParticipantLinkTable as apppartlT with(nolock) on apppartlT.participantLink=partT.GUID
  30. INNER JOIN OOS.dbo.applicationTable AS appT WITH (NOLOCK) ON appT.GUID=apppartlT.applicationParticipantGUIDLink
  31. INNER JOIN OOS.dbo.protocolLotTable AS plt WITH (NOLOCK) ON plt.GUID = appT.applicationGUIDLink
  32. 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))
  33. inner join [OOS].[dbo].[NotificationTable] nt WITH(NOLOCK) on protT.notificationNumber=nt.notificationNumber
  34. 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
  35. inner join [OOS].[dbo].[LotTable] lt WITH(NOLOCK) on lt.lotGUIDLink = nt.guid and isnull([lotNumber],1)=lt.ordinalNumber and prtt.id=protT.id
  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.PrsAdmitted
  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 protT.notificationNumber=nt.notificationNumber and isnull(plt.[lotNumber],1)=lt.ordinalNumber and app.[journalNumber]=appT.journalNumber) d
  46. outer apply (SELECT DISTINCT dense_rank () OVER (partition by protT.notificationNumber,plt.lotNumber order by protT.[protocolNumber] desc) id,
  47. [journalNumber]
  48. ,protT.notificationNumber
  49.                    ,plt.[lotNumber]
  50. ,protT.[protocolNumber]
  51. ,appt.PrsWinner
  52. from OOS.dbo.ProtocolTable  protT with(nolock)
  53. INNER JOIN OOS.dbo.protocolLotTable AS plt WITH (NOLOCK) ON protT.GUID = plt.protocolLotGUIDLink
  54. INNER JOIN OOS.dbo.applicationTable AS app WITH (NOLOCK) ON plt.GUID = app.applicationGUIDLink
  55. 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
  56. left join [OOS].[dbo].[ContractTable] ct with(nolock) on lt.id=ct.lotid
  57. left join dicts.dbo.ContractTable dct WITH(NOLOCK) on dct.guid=ct.guid
  58. left join [OOS].[dbo].[applicationParticipantLinkTable] apa with(nolock) on apa.[applicationParticipantGUIDLink]=ct.guid
  59. left join [OOS].[dbo].[ParticipantTable] pa with(nolock) on pa.guid = apa.[participantLink]
  60. where (d.id=1 or d.id is null) and (w.id=1 or w.id is null)
  61. order by nt.notificationNumber
  62. '''
  63. data = pd.read_sql(query,connection) #filter(lambda x: x in printable, query),conn)
  64. cursor.close()
  65. data.head()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement