I want to add logic that calculates price per claim. Below, there are two claims, one for patient 5, and another for patient 6. Original idea is to create a unique list of patient numbers in a separate table, then sort the original table by these unique patient numbers and run conditional statements to output a single value (reimbursement value).Then iterate through the unique table until completed. Does this sound like a feasible workflow? Not necessarily looking for specific code but more of a workflow/process
For example/context:
PatNo
RevCode
CPT
BilledCharges
DRG
5
141
null
100
880
5
636
J1234
50
null
6
111
null
8000
783
6
636
J1234
300
null
PSYCH look up table: if claim has DRG on table then calculate 75% of BilledCharges for claim.
DRG
Service Category
876
PSYCH
880
PSYCH
881
PSYCH
882
PSYCH
883
PSYCH
884
PSYCH
885
PSYCH
886
PSYCH
887
PSYCH
C- Section look up table: if claim has DRG on table pay $5000 for claim.
DRG
Service
765
C-SECTION
766
C-SECTION
783
C-SECTION
784
C-SECTION
786
C-SECTION
787
C-SECTION
785
C-SECTION
788
C-SECTION
If claim has RevCode 636, then add 50% of charges to claim reimbusment.
OUTPUT:
PatNo
Reimburs.
5
100
6
5150
So...
Patient 5's reimbursement is...(75% of 100) + (50% of 50) = 100
Patient 6's reimbursement is...(5000) + (50% of 300)
Assuming you've told us all the rules...
You can left join the tables, to check if values are present there or not, then use case expressions to apply the logic, and finally aggregate it to sum it all up...
SELECT
YourTable.patno,
SUM(
CASE WHEN section.drg IS NOT NULL THEN 5000
WHEN psych.drg IS NOT NULL THEN 0.75 * yourTable.billedcharges
WHEN yourTable.revcode = 636 THEN 0.5 * yourTable.billedcharges
ELSE 0
END
)
FROM
yourTable
LEFT JOIN
section
ON section.drg = yourTable.drg
LEFT JOIN
psych
ON psych.drg = yourTable.drg
GROUP BY
yourTable.patno
Please forgive typos, I'm on my phone.
Related
I'm trying to select some values based on some proprietary data, and I just changed the variables to reference house prices.
I am trying to get the total offers for houses where they were sold at the bid or at the ask price, with offers under 15 and offers * sale price less than 5,000,000.
I then want to get the total number of offers for each neighborhood on each day, but instead I'm getting the total offers across each neighborhood (n1 + n2 + n3 + n4 + n5) across all dates and the total offers in the dataset across all dates.
My current query is this:
SELECT DISTINCT(neighborhood),
DATE(date_of_sale),
(SELECT SUM(offers)
FROM `big_query.a_table_name.houseprices`
WHERE ((offers * accepted_sale_price < 5000000)
AND (offers < 15)
AND (house_bid = sale_price OR
house_ask = sale_price))) as bid_ask_off,
(SELECT SUM(offers)
FROM `big_query.a_table_name.houseprices`) as
total_offers,
FROM `big_query.a_table_name.houseprices`
GROUP BY neighborhood, DATE(date_of_sale) LIMIT 100
Which I am expecting a result like, with date being repeated throughout as d1, d2, d3, etc.:
but am instead receiving
I'm aware that there are some inherent problems with what I'm trying to select / group, but I'm not sure what to google or what tutorials to look at in order to perform this operation.
It's querying quite a bit of data, and I want to keep costs down, as I've already racked up a smallish bill on queries.
Any help or advice would be greatly appreciated, and I hope I've provided enough information.
Here is a sample dataframe.
neighborhood date_of_sale offers accepted_sale_price house_bid house_ask
bronx 4/1/2022 3 323 320 323
manhattan 4/1/2022 4 244 230 244
manhattan 4/1/2022 8 856 856 900
queens 4/1/2022 15 110 110 135
brooklyn 4/2/2022 12 115 100 115
manhattan 4/2/2022 9 255 255 275
bronx 4/2/2022 6 330 300 330
queens 4/2/2022 10 405 395 405
brooklyn 4/2/2022 4 254 254 265
staten_island 4/3/2022 2 442 430 442
staten_island 4/3/2022 13 195 195 225
bronx 4/3/2022 4 650 650 690
manhattan 4/3/2022 2 286 266 286
manhattan 4/3/2022 6 356 356 400
staten_island 4/4/2022 4 361 361 401
staten_island 4/4/2022 5 348 348 399
bronx 4/4/2022 8 397 340 397
manhattan 4/4/2022 9 333 333 394
manhattan 4/4/2022 11 392 325 392
I think that this is what you need.
As we group by neighbourhood we do not need DISTINCT.
We take sum(offers) for total_offers directly from the table and bids from a sub-query which we join to so that it is grouped by neighbourhood.
SELECT
h.neighborhood,
DATE(h.date_of_sale) AS date_,
s.bids AS bid_ask_off,
SUM(h.offers) AS total_offers,
FROM
`big_query.a_table_name.houseprices` h
LEFT JOIN
(SELECT
neighborhood,
SUM(offers) AS bids
FROM
`big_query.a_table_name.houseprices`
WHERE offers * accepted_sale_price < 5000000
AND offers < 15
AND (house_bid = sale_price OR
house_ask = sale_price)
GROUP BY neighborhood) s
ON h.neighborhood = s.neighborhood
GROUP BY
h.neighborhood,
DATE(date_of_sale),
s.bids
LIMIT 100;
Or the following which modifies more the initial query but may be more like what you need.
SELECT
h.neighborhood,
DATE(h.date_of_sale) AS date_,
s.bids AS bid_ask_off,
SUM(h.offers) AS total_offers,
FROM
`big_query.a_table_name.houseprices` h
LEFT JOIN
(SELECT
date_of_sale dos,
neighborhood,
SUM(offers) AS bids
FROM
`big_query.a_table_name.houseprices`
WHERE offers * accepted_sale_price < 5000000
AND offers < 15
AND (house_bid = sale_price OR
house_ask = sale_price)
GROUP BY
neighborhood,
date_of_sale) s
ON h.neighborhood = s.neighborhood
AND h.date_of_sale = s.dos
GROUP BY
h.neighborhood,
DATE(date_of_sale),
s.bids
LIMIT 100;
Preview of tables I am working with:
For example/context:
Table Name: [GMC Claims 2019]
PName
HSSV
DateOfService
InsName
DRG
RevCode
CPT
Qty
BilledCharges
5
Hisham
SIP
5
BCBS
870
344
44
15
5
Hisham
SIP
5
BCBS
0
440
70
69
5
Hisham
SIP
5
BCBS
0
440
70
69
5
Hisham
SIP
5
BCBS
0
419
70
69
Table Name: BCBS_DRG_REIMBURS
DRG Code
Description
DRG Weight
ALOS
High Trim
Effective Date
DRG Rate
Per Diem High Trim Outlier
42
XXXXX
YYYYY
30
54
10/1/2018
$235,121.59
$5,486.17
101
XXXXX
YYYYY
24
40
10/1/2018
$146,736.72
$4,279.82
870
XXXX
YYYYY
13
23
10/1/2018
$80894.61
$3934.56
Table Name: [BCBS DRG CARVE OUT 07012016]
DRG
SERVICE
PMT (SDA)
101
DRG CARVE OUT
13537
439
DRG CARVE OUT
13537
My Current Code:
SELECT
DRG, DRG_Rate,BilledCharges,'TotalPmt'=
CASE
WHEN BilledCharges>275000
THEN BilledCharges-275000*0.195+0
ELSE NULL
END
FROM
[GMC Claims 2019]
JOIN BCBS_DRG_REIMBURS
ON [GMC Claims 2019].DRG = BCBS_DRG_REIMBURS.DRG_Code
LEFT JOIN [BCBS DRG CARVE OUT 07012016]
ON [GMC Claims 2019].DRG = [BCBS DRG CARVE OUT 07012016]. DRG_c
Output of my current code:
DRG_Rate
BilledCharges
TotalPmt
870
80894.61
485000
My goal:
I need the sum of Billed Charges in the [GMC Claims 2019] table to be used in my calculation in my above query, not just the individual line item Billed Charges. Is there an easy way to do this or do I have to modify my join statement?
In plain English, this is what I want my case statement to read: "If the sum of billed charges for patient 5 from the GMC table > 275,000, THEN TotalPmt is = sum of billed charges for patient 5 (which is $1,209,350.52) - 275000 * 0.195 + DRG_Rate in my current output (80894.61) to EQUAL $263,092.96
That's the number that should be in the TotalPmt column in my final output. The Sum of billed charges in the first table is the only piece I am missing to make this work.
Any tips/advice would be appreciated!
I have a table as below. I want to do a group by in such a way that 1-4 weeknums are joined together and 5-8 weeknums are joined together. Or in other words i want to get the monthly total from below fields
table1
weeknum amount
1 1000
2 1100
3 1200
4 1300
5 1400
6 1500
7 1600
8 1700
The output i need is as below
output
max(weeknum) sum(amount)
4 4600
8 6200
The below answer did not work exactly for my actual values as below. I want to start with 4 weeks grouping. The formula (weeknum-1)/4 returns 3 groups as in the expected is only 2
weeknum Group Expr Expected Group Expr
1855 463 463
1856 463 463
1857 464 463
1858 464 463
1859 464 464
1860 464 464
1861 465 464
1862 465 464
Need to execute the query in oracle
Try using FLOOR that rounds the number down in the group by clause:
SELECT MAX(t.weeknum),sum(amount)
FROM table1 t
GROUP BY FLOOR((t.weeknum-1)/4)
This will make sure every 4 weeks are treated as a group :
(1-1)/4 -> 0
(2-1)/4 -> 0
...
(5-1)/4 -> 1
I have got an OLAP cube that has purchased per date, per vendor and some other dimensions.
Below a sample of the data. The vendor is identified by the unique id VendorID:
Date CCID GLID CatID VendorID Amount
31-3-2012 659 55 25 807 124.5
14-5-2012 425 74 1 1452 371.53
1-4-2012 353 55 106 1648 26.79
2-7-2012 339 78 25 1275 1208
8-7-2012 460 66 41 4311 763.25
The vendor itself has a score with values 1-good, 2-average, 3-poor, 4-unattended. These scores vary over time.
Example for vendor 807:
VendorID VendorIDDate Score
807 1-1-2012 4-unattended
807 27-2-2013 2-average
807 1-4-2014 3-poor
807 31-12-2014 1-good
Now when I start a query I would like to count the number of vendors per Score for a specific slicer on GLID, CCID and CatID on a certain date.
What is the best way to model this?
I know I can add the score to the basic fact table using a look-up for each date, but I assume there is a much better way.
I have two tables:
person table accident table
pid name phone acc_id pid type
1 Mike 3123223232 132 1 car
2 Kyle 133 3 snow
3 Nick 3124567654 134 4 cold
4 John 3124566663 135 2 sun
5 Pety 4234345453 136 3 hot
137 2 sun
138 3 snow
139 2 cold
140 1 hot
I need to find all accidents acc_id with a reference to each other that incurred to the same person given that she has a valid telephone number
So the result would be the following:
acc_id reference
133 136
133 138
136 133
136 138
138 133
138 136
132 140
140 132
So, person with pid = 3 had accidents 133, 136, 138 and this person has a phone, thus these three acc_id refer to each other. Next, pid = 2 also had three accidents, however since her phone number is unknown, we do not include her. Next, pid = 1 had two accidents 132, 140 and she has a phone number , so we include her accident numbers.
I know a method how to write a query to do this (for the sake of space I did not include), but it includes joining these tables two times and I think that there must be a more efficient way. Can anybody help me?
How about something like this? (not sure if this is what you already had)
select acc1.acc_id, acc2.acc_id as reference
from accidents acc1
inner join accidents acct2 on acc1.pid = acc2.pid and acc1.acc_id <> acc2.acc_id
inner join people on people.pid = acc1.pid
where people.phone <> ""