Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;WITH
- FilteredLogDate AS
- (
- SELECT eowhe.LogDate
- , eowhe.EnterpriseOrderEntryWorkflowHistoryEventId [eventid]
- , ROW_NUMBER() OVER (PARTITION BY eowhe.logdate order by eowhe.logdate desc) AS FILTERLD
- , eoe.EnterpriseOrderEntryId [entryid]
- from [RxKey].[dbo].[EnterpriseOrderEntryWorkflowHistoryEvent] eowhe
- LEFT OUTER JOIN [RxKey].[dbo].EnterpriseOrderEntry eoe on eowhe.EnterpriseOrderEntryId = eoe.EnterpriseOrderEntryId
- where Description like '''Packing'' stage completed%'
- and cast (eowhe.LogDate as date) = (SELECT DATEADD(DAY, CASE (DATEPART(WEEKDAY, GETDATE()) + @@DATEFIRST) % 7
- WHEN 1 THEN -2 --Sunday, get friday by shipby
- WHEN 2 THEN -3 --Monday, get friday by shipby
- ELSE -1
- END, DATEDIFF(DAY, 0, GETDATE())))
- ) Select * INTO #TEMPTABLE from FilteredLogDate where FILTERLD = 1
- SELECT *
- INTO #STSHIPMENTTEMP
- FROM
- (
- SELECT
- ISNULL(NULLIF(CAST(Customer.Identifier as varchar(25)),''),'||') as 'Patient ID',
- ISNULL(NULLIF(CAST(Customer.LegacyCustomerID as varchar(25)),''),'||') as 'HUB Patient #',
- ISNULL(NULLIF(CAST(pat.FirstName as varchar(50)),''),'||') 'Patient First Name',
- ISNULL(NULLIF(CAST(pat.LastName as varchar(50)),''),'||') as 'Patient Last Name',
- ISNULL(NULLIF(CAST(CustomerAddress.Address1 as varchar(50)),''),'||') as 'Patient Address1',
- ISNULL(NULLIF(CAST(CustomerAddress.Address2 as varchar(50)),''),'||') as 'Patient Address2',
- ISNULL(NULLIF(CAST(CustomerAddress.City as varchar(50)),''),'||') as 'Patient City',
- ISNULL(NULLIF(CAST(CustomerAddress.State as varchar(2)),''),'||') as 'Patient State',
- ISNULL(NULLIF(CAST(CustomerAddress.ZipCode as varchar(9)),''),'||') as 'Patient Zip',
- ISNULL(NULLIF(CAST((SELECT TOP 1 PhoneNumber FROM [RxKey].[dbo].CustomerPhoneNumber with (nolock) WHERE phonenumbertype = 1 AND CustomerId = Customer.CustomerId) as varchar(10)),''),'||') as 'Home Phone',
- ISNULL(NULLIF(CAST((SELECT TOP 1 PhoneNumber FROM [RxKey].[dbo].CustomerPhoneNumber with (nolock) WHERE phonenumbertype = 3 AND CustomerId = Customer.CustomerId) as numeric(25)),''),'||') as 'Mobile Phone',
- ISNULL(NULLIF(CAST((SELECT TOP 1 PhoneNumber FROM [RxKey].[dbo].CustomerPhoneNumber with (nolock) WHERE phonenumbertype = 2 AND CustomerId = Customer.CustomerId) as varchar(25)),''),'||') as 'Work Phone',
- CASE
- WHEN ISNULL(NULLIF(CAST(Customer.Gender as varchar(1)),''),'||') = 0 THEN 'U'
- WHEN ISNULL(NULLIF(CAST(Customer.Gender as varchar(1)),''),'||') = 1 THEN 'M'
- WHEN ISNULL(NULLIF(CAST(Customer.Gender as varchar(1)),''),'||') = 2 THEN 'F'
- END AS 'Patient Gender',
- ISNULL(NULLIF(CAST(CONVERT (Varchar, pat.DateOfBirth, 112) as varchar(25)),''),'||') as 'Patient DOB',
- ISNULL(NULLIF(CAST(EnterpriseOrder.DestinationAddress1 as varchar(50)),''),'||') as 'Patient Ship Address1',
- ISNULL(NULLIF(CAST(EnterpriseOrder.DestinationAddress2 as varchar(50)),''),'||') as 'Patient Ship Address2',
- ISNULL(NULLIF(CAST(EnterpriseOrder.DestinationCity as varchar(50)),''),'||') as 'Patient Ship City',
- ISNULL(NULLIF(CAST(EnterpriseOrder.DestinationState as varchar(2)),''),'||') as 'Patient Ship State',
- ISNULL(NULLIF(CAST(EnterpriseOrder.DestinationZip as varchar(9)),''),'||') as 'Patient Ship Zip',
- ISNULL(NULLIF(CAST(rdc.DiseaseDescription as numeric(25)),''),'||') as 'Primary Diagnosis Code',
- ISNULL(NULLIF(CAST(Prescriber.FirstName as Varchar(50)),''),'||') as 'Prescriber First Name',
- ISNULL(NULLIF(CAST(Prescriber.LastName as Varchar(50)),''),'||') as 'Prescriber Last Name',
- ISNULL(NULLIF(CAST(PrescriberAddress.Address1 as Varchar(50)),''),'||') as 'Prescriber Address1',
- ISNULL(NULLIF(CAST(PrescriberAddress.Address2 as Varchar(50)),''),'||') as 'Prescriber Address2',
- ISNULL(NULLIF(CAST(PrescriberAddress.City as Varchar(50)),''),'||') as 'Prescriber City',
- ISNULL(NULLIF(CAST(PrescriberAddress.State as Varchar(2)),''),'||') as 'Prescriber State',
- ISNULL(NULLIF(CAST(PrescriberAddress.Zip as Varchar(9)),''),'||') as 'Prescriber Zip',
- ISNULL(NULLIF(CAST(PrescriberAddress.PhoneNumber as Varchar(9)),''),'||') as 'Prescriber Phone',
- ISNULL(NULLIF(CAST(PrescriberAddress.FaxNumber as numeric(10)),''),'||') as 'Prescriber Fax',
- ISNULL(NULLIF(CAST(Prescriber.NpiNbr as varchar(30)),''),'||') as 'Prescriber NPI',
- ISNULL(NULLIF(CAST(Drugidentifier.Identifier as varchar(11)),''),'||') as 'NDC#',
- ISNULL(NULLIF(CAST(Drug.Description as varchar(50)),''),'||') as 'Product Description',
- ISNULL(NULLIF(CAST(Refill.DispensedQuantity as numeric(3)),''),'||') as 'Quantity Dispensed',
- ISNULL(NULLIF(CAST(Refill.DaysSupply as numeric(3)),''),'||') as 'Days Supply',
- ISNULL(NULLIF(CAST(PrimaryPlan.PlanName as varchar(50)),''),'||') as 'Program DispenseType',
- ISNULL(NULLIF(CAST(temp.LogDate as date(8)),''),'||') as 'Ship Date',
- '' as ' '
- FROM [RxKey].[dbo].EnterpriseOrder with (nolock)
- LEFT OUTER JOIN [RxKey].[dbo].RefillDestinationType DestinationTypeChild with (nolock) on DestinationTypeChild.Type = EnterpriseOrder.DeliveryMethod
- LEFT OUTER JOIN [RxKey].[dbo].refilldestinationtype DestinationTypeParent with (nolock) on DestinationTypeParent.Type = DestinationTypeChild.ParentId
- LEFT OUTER JOIN [RxKey].[dbo].Customer with (nolock) on Customer.CustomerId = EnterpriseOrder.CustomerId
- LEFT OUTER JOIN [RxKey].[dbo].CustomerAddress with (nolock) on CustomerAddress.CustomerId = EnterpriseOrder.CustomerId
- LEFT OUTER JOIN [RxKey].[dbo].EnterpriseOrderEntry with (nolock) on EnterpriseOrderEntry.EnterpriseOrderId = EnterpriseOrder.EnterpriseOrderId and WorkflowState <> 4
- LEFT OUTER JOIN [RxKey].[dbo].WorkQueueItem with (nolock) on WorkQueueItem.WorkQueueItemId = EnterpriseOrderEntry.ItemFillId
- LEFT OUTER JOIN [RxKey].[dbo].Refill with (nolock) on Refill.RefillId = WorkQueueItem.RefillId
- LEFT OUTER JOIN [RxKey].[dbo].Rx with (nolock) on Rx.RxId = Refill.RxId
- LEFT OUTER JOIN [RxKey].[dbo].Customer pat on pat.customerid = rx.customerid
- LEFT OUTER JOIN [RxKey].[dbo].PrescriberAddress with (nolock) on PrescriberAddress.PrescriberAddressId = Refill.PrescriberAddressId
- LEFT OUTER JOIN[RxKey].[dbo]. Prescriber with (nolock) on Prescriber.PrescriberId = PrescriberAddress.PrescriberID
- LEFT OUTER JOIN [RxKey].[dbo].DrugIdentifier with (nolock) on DrugIdentifier.DrugId = refill.DrugId
- LEFT OUTER JOIN [RxKey].[dbo].CustomerDisease with (nolock) on CustomerDisease.CustomerId = Customer.CustomerId
- LEFT OUTER JOIN [RxKey].[dbo].[vw_Reporting_CustomerDiseaseDiagnosisAllergy] cdda on cdda.CustomerId = Rx.CustomerId
- LEFT OUTER JOIN [RxKey].[dbo].[vw_Reporting_DiseaseCodes] rdc on rdc.DiseaseCode = cdda.DiseaseType
- LEFT OUTER JOIN [RxKey].[dbo].[vw_Reporting_DrugNDC] Drug with (nolock) on Drug.DrugId = Refill.DrugId
- LEFT OUTER JOIN [RxKey].[dbo].[vw_RefillClaims] Claim with (nolock) on Claim.RefillId = Refill.RefillId
- LEFT OUTER JOIN [RxKey].[dbo].ThirdParty PrimaryPlan with (nolock) on PrimaryPlan.ThirdPartyId = Claim.FirstPlanThirdPartyId
- inner JOIN [RxKey].[dbo].StoreProfile with (nolock) ON StoreProfile.StoreNum = EnterpriseOrder.StoreNum
- inner join #TEMPTABLE temp on temp.entryid = EnterpriseOrderEntry.EnterpriseOrderEntryId
- Where (1=1
- and StoreProfile.IsPrimary = 1
- and Refill.Status = 7 --(7 = refill has been filled)
- and (EnterpriseOrderEntry.WorkflowStage > 120 or EnterpriseOrderEntry.WorkflowStage = 200 and EnterpriseOrderEntry.workflowstate = 5) --PACKING STAGE
- and EnterpriseOrderEntry.IsDeleted = 0
- and EnterpriseOrder.IsCancelled = 0
- and Drugidentifier.Identifier IN ('42747060290','42747060490')) -- 1st condition * ))
- or (EnterpriseOrder.IsCancelled = 1 AND EnterpriseOrderEntry.WorkflowStateDesc = 'Transferred') --second condition
- ) as info;
- ;With dataCTE
- AS
- (
- select *, '' as RowCnt
- from #STSHIPMENTTEMP
- ),
- footerCTE
- AS
- (
- select
- CAST('' as varchar(25)) as 'Patient ID',
- CAST('' AS varchar(25)) as 'HUB Patient #',
- CAST('' AS varchar(25)) as 'Patient First Name',
- CAST('' AS varchar(10)) as 'Patient Last Name',
- CAST('' AS varchar(50)) as 'Patient Address1',
- CAST('' AS varchar(50)) as 'Patient Address2',
- CAST('' AS varchar(10)) as 'Patient City',
- CAST('' AS varchar(10)) as 'Patient State',
- CAST('' AS varchar(9)) as 'Patient Zip',
- CAST('' AS varchar(50)) as 'SP Secondary Order Status',
- CAST('' AS varchar(50)) as 'Home Phone',
- CAST('' AS varchar(50)) as 'Mobile Phone',
- CAST('' AS varchar(50)) as 'Work Phone',
- CAST('' AS varchar(50)) as 'Patient Gender',
- CAST('' AS varchar(50)) as 'Patient DOB',
- CAST('' AS varchar(50)) as 'Patient Ship Address1',
- CAST('' AS varchar(50)) as 'Prescriber Address2',
- CAST('' AS varchar(50)) as 'Prescriber City',
- CAST('' AS varchar(50)) as 'Prescriber State',
- CAST('' AS varchar(50)) as 'Patient Ship Zip',
- CAST('' AS varchar(50)) as 'Prescriber Phone',
- CAST('' AS varchar(50)) as 'Prescriber Fax',
- CAST('' AS varchar(50)) as 'Prescriber NPI',
- CAST('' AS varchar(50)) as 'NDC#',
- CAST('' AS varchar(50)) as 'Product Description',
- CAST('' AS varchar(50)) as 'Quantity Dispensed',
- CAST('' AS varchar(50)) as 'Days Supply',
- CAST('' AS varchar(50)) as 'Program DispenseType',
- CAST('' AS varchar(50)) as 'Ship Date',
- ' '+convert(varchar(25),COUNT(*)) as RowCNT from #STSHIPMENTTEMP
- )
- select * from #STSHIPMENTTEMP
- union all
- select * from footerCTE
- drop table #STSHIPMENTTEMP
- drop table #TEMPTABLE
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement