healthcare enrollment verification of claims - sql

All:
I'm trying to verify if a hospital stay should be covered based on enrollment data using SAS.
There are two tables:
"enr" : 2 years of a binary (0/1) if the patient was enrolled on that month.
data enr;
input id year jan feb mar apr may jun jul aug sep oct nov dec;
cards;
1 2018 0 0 0 1 1 0 0 0 0 0 0 1
1 2019 1 1 1 1 1 1 1 1 1 1 1 1
2 2018 1 1 0 0 1 1 1 1 1 1 1 1
2 2019 1 1 1 1 1 1 0 0 0 0 0 0
;
"clms" : id, admit_dt, discharge_dt, dr_id, cost
data clms;
input id admit_dt mmddyy10. #14dischrg_dt mmddyy10. code dr_id cost;
format admit_dt dischrg_dt mmddyy10.;
cards;
1 02/01/2019 02/06/2019 470 2 800
1 06/01/2019 06/15/2019 251 30 400
2 10/18/2018 10/22/2018 871 5 250
2 05/18/2019 05/22/2019 999 3 1000
;
For each "id" on the claims table, I wish to identify claims to be paid under the rule: for a paid claim, the patient must be enrolled in the 4 months prior and 4 months after the date of admission(admit_dt).
One of the tricky issues is the format of the months on the enrollment table(character, e.g."feb") differs from that of the admission date mmddyyy10 on the claims table.
Should I use an sql solution?

Transpose the enr data, compute which months are banded together, then perform a SQL join.
Example:
data enr_tall;
set enr;
array months jan feb mar apr may jun jul aug sep oct nov dec;
do i = 1 to 12;
month = mdy (i,1,year);
enrolled = months[i];
output;
end;
keep id month enrolled;
run;
data enr_tall_bands;
set enr_tall;
by id month;
retain count4;
if first.id then do;
count4 = 0;
band + 1;
end;
if ^first.id and not enrolled and count4 > 0 then do;
count4 = 0;
band + 1;
end;
if enrolled then do;
if count4 = 0 then band + 1;
count4 + 1;
end;
run;
proc sql;
create table news as
select
clms.*
, ( select count(distinct band) = 1 and min(enrolled) = 1
from enr_tall_bands as bands
where
bands.id = clms.id
& admit_dt between
intnx('month', month, -4)
and
intnx('month', month, 4)
) as
payable
from clms;

Related

SQL show UserCode from the highest value in column

I am making a select from a table that has alums, but those alums can have a "level" being 01, 02, 03, 04, 05 or 99 and if I select a distinct(code) there will be 2 or 3 files of the same "code" with different "level". I want to be able to ONLY get the "code" with the higher "level" anytime.
Example of my code:
select
Level,
count(Code) as Atendee,
count(CASE WHEN CodPlace = 01 THEN (Atendee) ELSE null END ) as AtendeeCho,
count(CASE WHEN CodPlace = 02 THEN (Atendee) ELSE null END ) as AtendeeSB,
count(CASE WHEN CodPlace = 14 THEN (Atendee) ELSE null END ) as AtendeeIca
from #TempoPar
group by Level
order by Level
And here is the result:
Level Atendee AtendeeCho AtendeeSB AtendeeIca
1 3 2 0 1
2 0 0 0 0
3 2 2 0 0
99 1 1 0 0
Problem is that the one who is in AtendeeCho from level 1, 2 and 99 is the same person and he should only be on 99 or 3 if he wasn't on 99.
Thanks in advance!
Data example:
Code ( user) Level Career CodPlace
12345 1 9 01
12345 3 15 01
12346 1 10 14
12347 1 10 01
12345 3 15 01
12347 99 15 01
Now, this is what I require:
From the data sample:
Code ( user) Level Career CodPlace
12345 3 15 01
12346 1 10 14
12347 99 15 01
In the final output with the corrected data it should be:
Level Atendee AtendeeCho AtendeeSB AtendeeIca
1 1 0 0 1
2 0 0 0 0
3 1 1 0 0
99 1 1 0 0

sql split yearly record into 12 monthly records

I am trying to use common table expression to split an yearly record into 12 monthly records. I have to do it for next 20 years records . That means 20 rows into 600 rows (20*12=600 records).
What is the best way to do it. Can anyone help with an efficient way to do it.
Using a single table as shown below. Year 0 means current year so it should split into remaining months and year=1 means next year onward it should split into 12 (months) records
id year value
1 0 3155174.87
1 1 30423037.3
1 2 35339631.25
expected result should look like this:
Id Year Month Value Calender year
1 0 5 150 2022
1 0 6 150 2022
1 0 7 150 2022
1 0 8 150 2022
1 0 9 150 2022
1 0 10 150 2022
1 0 11 150 2022
1 0 12 150 2022
1 0 1 150 2023
1 0 2 150 2023
1 0 3 150 2023
1 0 4 150 2023
1 1 5 100 2023
1 1 6 100 2023
1 1 7 100 2023
1 1 8 100 2023
1 1 9 100 2023
1 1 10 100 2023
1 1 11 100 2023
1 1 12 100 2023
1 1 1 100 2024
1 1 2 100 2024
1 1 3 100 2024
1 1 4 100 2024
You can simply join onto a list of months, and then use a bit of arithmetic to split the Value
SELECT
t.Id,
t.Year,
v.Month,
Value = t.Value / CASE WHEN t.Year = 0 THEN 13 - MONTH(GETDATE()) ELSE 12 END
FROM YourTable t
JOIN (VALUES
(1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12)
) v(Month) ON t.year > 0 OR v.Month >= MONTH(GETDATE());
db<>fiddle

Postgres query for annual sales report by rep. grouped by month

I would like to create an annual sales report table by sales rep, showing all the twelve month. The data I have is more or less like in this example:
id | rep | date | price
----------------------------
1 1 2017-01-01 20
2 1 2017-01-20 44
3 2 2017-02-18 13
4 2 2017-03-08 12
5 2 2017-04-01 88
6 2 2017-09-05 67
7 3 2017-01-31 10
8 3 2017-06-01 74
The result I need would be like this:
Rep Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
----------------------------------------------------
1 64 0 0 0 0 0 0 0 0 0 0 0
2 0 13 12 88 0 0 0 0 67 0 0 0
3 10 0 0 0 0 74 0 0 0 0 0 0
What would be the most efficient way to write this query?
One way:
select rep,
sum(case when extract('month' from date) = 1 then price else 0 end ) as Jan,
sum(case when extract('month' from date) = 2 then price else 0 end ) as Feb
-- another months here
from t
group by rep
One way is to use windowed functions with FILTER:
SELECT DISTINCT
"rep",
COALESCE(SUM(price) FILTER (WHERE extract('month' from "date") = 1) OVER(PARTITION BY "rep"),0) AS Jan,
COALESCE(SUM(price) FILTER (WHERE extract('month' from "date") = 2) OVER(PARTITION BY "rep"),0) AS Feb
--....
FROM ta;
Rextester Demo
Warning!
You probably want to partition by YEAR too to avoid summing JAN 2017 and JAN 2018.

I have been trying to write a below SQL query ande getting result as given below the query

select
TRN_ADP_Items_Tracker.request_id,
TRN_ADP_Stationary_Request.requester_name,
item_id,
case when MONTH(TRN_ADP_Items_Tracker.ITEM_QTY_TRADE_DATE)=6 then sum(item_qty_traded) else 0 end as JUNE,
case when MONTH(TRN_ADP_Items_Tracker.ITEM_QTY_TRADE_DATE)=7 then sum(item_qty_traded) else 0 end as JULY,
case when MONTH(TRN_ADP_Items_Tracker.ITEM_QTY_TRADE_DATE)=8 then sum(item_qty_traded) else 0 end as AUG
from TRN_ADP_Items_Tracker
join TRN_ADP_Stationary_Request
on TRN_ADP_Items_Tracker.request_id = TRN_ADP_Stationary_Request.stationary_request_id
group by TRN_ADP_Stationary_Request.requester_name,item_id,
MONTH(TRN_ADP_Items_Tracker.ITEM_QTY_TRADE_DATE);
request requester item
_id _name _id JUNE JULY AUG
3 Prasad 150 2 0 0
3 Prasad 154 2 0 0
1 Sneha 150 15 0 0
1 Sneha 150 0 15 0
1 Sneha 150 0 0 6
1 Sneha 154 4 0 0
But here I want Result like:-
request_id requester_name item_id JUNE JULY AUG
1 Sneha 150 15 15 6
if 1st 3 columns data is same then then months data should be come in 1 row, as given above.
you need to correct your GROUP_ID to have only three columns from your select
select
TRN_ADP_Items_Tracker.request_id,
TRN_ADP_Stationary_Request.requester_name,
item_id
from
group by TRN_ADP_Stationary_Request.requester_name,item_id,
MONTH(TRN_ADP_Items_Tracker.ITEM_QTY_TRADE_DATE)
Need to take out request_id column from select list.
select
--TRN_ADP_Items_Tracker.request_id,
TRN_ADP_Stationary_Request.requester_name,
item_id,
case when MONTH(TRN_ADP_Items_Tracker.ITEM_QTY_TRADE_DATE)=6 then sum(item_qty_traded) else 0 end as JUNE,
case when MONTH(TRN_ADP_Items_Tracker.ITEM_QTY_TRADE_DATE)=7 then sum(item_qty_traded) else 0 end as JULY,
case when MONTH(TRN_ADP_Items_Tracker.ITEM_QTY_TRADE_DATE)=8 then sum(item_qty_traded) else 0 end as AUG
from TRN_ADP_Items_Tracker
join TRN_ADP_Stationary_Request
on TRN_ADP_Items_Tracker.request_id = TRN_ADP_Stationary_Request.stationary_request_id
group by TRN_ADP_Stationary_Request.requester_name,item_id,
MONTH(TRN_ADP_Items_Tracker.ITEM_QTY_TRADE_DATE);

SQL Server to show data for each month from table

How to populate or display the result for each month?
I have a table with four columns
Plan Status Creation date Triggers
I need to get the results for each plan each month how many triggers count are there. my report should look like this.
Plan Jan feb march Apr may jun jul aug sep oct nov dec Totaol
001 2 0 1 1 0 1 1 1 2 3 1 7 21
002 2 0 1 1 0 1 1 1 2 3 1 7 21
003 2 0 1 1 0 1 1 1 2 3 1 7 21
Could you please help me out how to achieve this results?
Thanks in advance.
select
plan,
sum(case when datepart(mm,[Creation date]) = 1 then 1 else 0 end) as Jan,
sum(case when datepart(mm,[Creation date]) = 2 then 1 else 0 end) as Feb,
...
sum(triggers) as Total
from table
where Status = 'SomeStatus'
group by Plan