Advertisement
Guest User

Untitled

a guest
Aug 9th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 1.33 KB | None | 0 0
  1. IF OBJECT_ID('tempdb.dbo.#temptable1') IS NOT NULL
  2.     DROP TABLE #temptable1
  3.  
  4. IF OBJECT_ID('tempdb.dbo.#temptable2') IS NOT NULL
  5.     DROP TABLE #temptable2
  6.  
  7. SELECT DISTINCT [Service_Date]
  8.     ,[Member_Patient_ID]
  9.     ,[Member_Name]
  10.     ,[Pharmacy_Rx_Number]
  11.     ,[Pharmacy_NPI]
  12.     ,[Pharmacy_Name]
  13.     ,[Pharmacy_Physical_State]
  14.     ,[Prescriber_NPI]
  15.     ,[Prescriber_Name]
  16.     ,[Prescriber_Primary_Specialty]
  17.     ,[Prescriber_Physical_State]
  18.     ,[Dispensable_Name]
  19.     ,[Quantity_Dispensed]
  20.     ,[Days_Supply]
  21.     ,[Patient_Paid]
  22.     ,[Plan_Paid]
  23.     ,[Claim_Number]
  24.     ,[ICN]
  25.     ,[LOB_ID]
  26.     ,[Date_Written]
  27.     ,ROW_NUMBER() OVER (
  28.         PARTITION BY [Member_Patient_ID] ORDER BY [Pharmacy_Physical_State]
  29.         ) row_num
  30. INTO #TempTable1
  31. FROM [AbcaMstrDDS].[data].[PBM_Utilization]
  32. WHERE [Service_Date] BETWEEN '20190707'
  33.         AND '20190807'
  34.     AND Member_Name <> 'Unknown'
  35. --GROUP BY [Member_Patient_ID]
  36. --  ,[Member_Name]
  37. --  ,[Pharmacy_Physical_State]
  38. --  ,[Service_Date]
  39. ORDER BY [Member_Patient_ID];
  40.  
  41. SELECT  DISTINCT [Member_Patient_ID], [Pharmacy_Physical_State]
  42. INTO #TempTable2
  43. FROM #TempTable1
  44. WHERE Member_Patient_ID = '2536328'
  45. --'2536328'
  46. --AND row_num > 2
  47. --GROUP BY Pharmacy_Physical_State
  48.  
  49. SELECT *
  50. FROM #TempTable2 t2
  51. JOIN #TempTable1 t1 on t2.[Member_Patient_ID] = t1.[Member_Patient_ID]
  52. AND t2.[Pharmacy_Physical_State] = t1.[Pharmacy_Physical_State]
  53. ORDER BY row_num
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement