Recreating SUMIFS in SQL with conditional logic - sql

I'm having a difficult time writing the SQL I'm able to accomplish with Excel SUMIFS. The ultimate goal is create share of requirement (loyalty) metric for each Customer. In the below, I'm trying to create the Category_Sales column. All other columns I already have in my SQL.
Here is what my Excel SUMIFS looks like.
=(Brand_Sales (range) , Cust_ID (range), Cust_ID (row), First_Buy (range), >=Brand_Str (row), Last Buy (range) <=, Brand_End (row))
The 268 value for Innocent is attained from the sum of Innocent, Cresco, Supply, and PTS since their First_Buy & Last_Buys are all inside the range of the Innocent Brand Start & End.
State
Brand
Cust_ID
First_Buy
Last_Buy
Brand_Str
Brand_End
Brand_Sales
Category_Sales
IL
Innocent
xyz
4/9/2022
4/9/2022
4/7/2022
5/29/2022
64
268
IL
Cresco
xyz
4/15/2022
4/15/2022
1/1/2022
5/30/2022
57
446
IL
Supply
xyz
4/15/2022
4/15/2022
1/1/2022
5/30/2022
45
446
IL
Rythm
xyz
1/3/2022
1/13/2022
1/1/2022
5/30/2022
121
446
IL
Natures
xyz
1/22/2022
1/22/2022
1/1/2022
5/30/2022
57
446
IL
PTS
xyz
4/26/2022
4/26/2022
1/1/2022
5/30/2022
102
446

Related

Dynamically Calculate difference columns based off slicer- POWERBI

I have a table with quarterly volume data, and a slicer that allows you to choose what quarter/year you want to see volume per code for. The slicer has 2019Q1 through 2021Q4 selections. I need to create dynamic difference column that will adjust depending on what quarter/year is selected in the slicer. I know I need to create a new measure using Calculate/filter but am a beginner in PowerBI and am unsure how to write that formula.
Example of raw table data:
Code
2019Q1
2019Q2
2019Q3
2019Q
2020Q1
2020Q2
2020Q3
2020Q4
11111
232
283
289
19
222
283
289
19
22222
117
481
231
31
232
286
2
19
11111
232
397
94
444
232
553
0
188
22222
117
411
15
14
232
283
25
189
Example if 2019Q1 and 2020Q1 are selected:
Code
2019Q1
2020Q1
Difference
11111
232
222
10
22222
117
481
-364
11111
232
397
-165
22222
117
411
-294
Power BI doesn't work that way. This is an Excel pivot table setup. You don't have any parameter to distinguish first and third or second and fourth row. They have the same code, so Power BI will aggregate their volumes. You could introduce a hidden index column but then why don't you simply stick to Excel? The Power BI approch to the problem would be to unpivot (stack) your table to a Code, Quarter and a Volume column, create 2 independent slicer tables for Minuend and Subtrahend and then CALCULATE your aggregated differences based on the SELECTEDVALUE of the 2 slicers.

JOIN Modification

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!

Applying Logic to Sets of Rows

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.

Pig script to tranpose on basis of certain criteria

I have a file containing data in following format:
abc 123 456
cde 45 32
efg 322 654
abc 445 856
cde 65 21
efg 147 384
abc 815 078
efg 843 286
and so on.
How can transpose it into following format using pig:
abc 123 456 cde 45 32 efg 322 654
abc 445 856 cde 65 21 efg 147 348
abc 815 078 efg 843 286
Also, in case cde is missing after abc, it should insert blank spaces instead, since it is a fixed width file.
I tried grouping but it ain't worked for me.
Well, you can do it by writing custom loader. The easiest attempt is to extend PigStorage and override getNext() method making it call record reader three times, instead of 1 and produce unioned Tuple.

slowly changing dimension in MDX/OLAP

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.