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 <> ""
Related
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.
new to learning sql/postgresql and have been hunting all over looking for help with a query to find only the matching id values in a table so i can pull data from another table for a learning project. I have been trying to use the count command, which doesn't seem right, and struggling with group by.
here is my table
id acct_num sales_tot cust_id date_of_purchase
1 9001 1106.10 116 12-Jan-00
2 9002 645.22 125 13-Jan-00
3 9003 1096.18 137 14-Jan-00
4 9004 1482.16 118 15-Jan-00
5 9005 1132.88 141 16-Jan-00
6 9006 482.16 137 17-Jan-00
7 9007 1748.65 147 18-Jan-00
8 9008 3206.29 122 19-Jan-00
9 9009 1184.16 115 20-Jan-00
10 9010 2198.25 133 21-Jan-00
11 9011 769.22 141 22-Jan-00
12 9012 2639.17 117 23-Jan-00
13 9013 546.12 122 24-Jan-00
14 9014 3149.18 116 25-Jan-00
trying to write a simple query to only find matching customer id's, and export them to the query window.
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 am trying to obtain a list of Case_Id's where the case does not contain a specific RoleId using Microsoft Sql Server 2012.
For example, I would like to obtain a collection of Case_Id's that do not contain a RoleId of 4.
So from the data set below the query would exclude Case_Id's 49, 50, and 53.
Id RoleId Person_Id Case_Id
--------------------------------------
108 4 108 49
109 1 109 49
110 4 110 50
111 1 111 50
112 1 112 51
113 2 113 52
114 1 114 52
115 7 115 53
116 4 116 53
117 3 117 53
So far I have tried the following
SELECT Case_Id
FROM [dbo].[caseRole] cr
WHERE cr.RoleId!=4
GROUP BY Case_Id ORDER BY Case_Id
The not exists operator seems to fit your need exactly:
SELECT DISTINCT Case_Id
FROM [dbo].[caseRole] cr
WHERE NOT EXISTS (SELECT *
FROM [dbo].[caseRole] cr_inner
WHERE cr_inner.Case_Id = cr.case_id
AND cr_inner.RoleId = 4);
Just add a having clause instead of where:
SELECT Case_Id
FROM [dbo].[caseRole] cr
GROUP BY Case_Id
HAVING SUM(case when cr.RoleId = 4 then 1 else 0 end) = 0
ORDER BY Case_Id;
Given two tables:
1st Table Name: FACETS_Business_NPI_Provider
Buss_ID NPI Bussiness_Desc
11 222 Eleven 222
12 223 Twelve 223
13 224 Thirteen 224
14 225 Fourteen 225
11 226 Eleven 226
12 227 Tweleve 227
12 228 Tweleve 228
2nd Table : FACETS_PROVIDERs_Practitioners
NPI PRAC_NO PROV_NAME PRAC_NAME
222 943 P222 PR943
222 942 P222 PR942
223 931 P223 PR931
224 932 P224 PR932
224 933 P224 PR933
226 950 P226 PR950
227 951 P227 PR951
228 952 P228 PR952
228 953 P228 PR953
With below query I'm getting following results whereas it is expected to have the provider counts from table FACETS_Business_NPI_Provider (i.e. 3 instead of 4 for Buss_Id 12 and 2 instead of 3 for Buss_Id 11, etc).
SELECT BP.Buss_ID,
COUNT(BP.NPI) PROVIDER_COUNT,
COUNT(PP.PRAC_NO)PRACTITIONER_COUNT
FROM FACETS_Business_NPI_Provider BP
LEFT JOIN FACETS_PROVIDERs_Practitioners PP
ON PP.NOI=BP.NPI
group by BP.Buss_ID
Buss_ID PROVIDER_COUNT PRACTITIONER_COUNT
11 3 3
12 4 4
13 2 2
14 1 0
If I understood it correctly, you might want to add a DISTINCT clause to the columns.
Here is an SQL Fiddle, which we can probably use to discuss further.
http://sqlfiddle.com/#!2/d9a0e6/3