generated from nhsengland/analyticsunit-template
-
Notifications
You must be signed in to change notification settings - Fork 4
/
gp_data_input_sql.R
38 lines (29 loc) · 1.05 KB
/
gp_data_input_sql.R
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
35
36
37
38
library(odbc)
library(DBI)
library(tidyverse)
source ('personal_credentials.R')
con_udal <- DBI::dbConnect(drv = odbc::odbc(),
driver = "ODBC Driver 17 for SQL Server",
server = serv,
database = db,
UID = user,
authentication = "ActiveDirectoryInteractive")
df2 <- DBI::dbGetQuery(conn = con_udal, statement = "
SELECT [Appointment_Date]
,[Appointment_Status]
,[HCP_Type]
,[Appointment_Mode]
,[Time_Between_Book_And_Appt]
,[Count_Of_Appointments]
,heir.stp_name
,[Region_Name]
,DENSE_RANK () OVER (PARTITION BY ([Appointment_Date]) ORDER BY AuditKey DESC) AS RN
,AuditKey
FROM UKHF_Appts_In_General_Practice.Appts_GP_Daily1_1 as gp
left join [Reporting_UKHD_ODS].[Commissioner_Hierarchies_ICB] as heir
on gp.commissioner_code = heir.[Organisation_Code]
where Appointment_Date >= '2018-01-01'
")
df2 <- df2 |>
filter (RN == '1')
write.csv(df2,"gp_dat2.csv")