-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDonor.sql
34 lines (22 loc) · 1.17 KB
/
Donor.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
--Points at correct database with write permissions
USE SomeResearchDBName
--Drops view if it exists
if object_id('CTR_DON','v') is not null
drop view CTR_DON;
go
CREATE VIEW [CTR_DON] AS
select d.donor_id,d.OPOcenter as center,d.donation_dt,D.orgs_recov,d.orgs_txed,d.orgs_disc,d.NUM_HR_RECOV,d.NUM_HR_TX,d.NUM_HR_DISC,
d.ki_recov,d.ki_txed,d.ki_disc,
d.li_recov,d.LI_TXED,d.LI_DISC,
d.LU_RECOV,d.LU_TXED,d.LU_DISC,
d.PA_RECOV,d.PA_TXED,d.PA_DISC,
d.IN_RECOV,d.IN_TXED,d.IN_DISC,
d.nondcd,d.tot_disposition,
case when datepart(month,d.donation_dt)=1 then concat('March 31,',DATEPART(year,d.donation_dt))
when datepart(month,d.donation_dt)=2 then concat('June 30,',DATEPART(year,d.donation_dt))
when datepart(month,d.donation_dt)=3 then concat('September 30,',DATEPART(year,d.donation_dt))
when datepart(month,d.donation_dt)=4 then concat('December 31,',DATEPART(year,d.donation_dt))
end as caldate
from SomeResearchDB.DSV.Donors d
WHERE d.donation_dt between DATEADD(yy, DATEDIFF(yy, 0, GETDATE()) - 3, 0) and iif(datepart(dd,getdate())<15,DATEADD(MONTH, DATEDIFF(MONTH, -1, GETDATE())-2, -1),DATEADD(MONTH, DATEDIFF(MONTH, -1, GETDATE())-1, -1));
go