I have a table that has over 30 columns, it's patient medical record data so it has many fields. I have another table that Verified doctor information.
Say table 1 has columns patientID, patientName, patientAddress, ...., PrescriberID, PrescriberName, PrescriberAddress, PrescriberCity, PrescriberState, PrescriberZip, PrescriberPhone, ... plus many others
Table 2 has PrescriberID, PrescriberName, PrescriberAddress, PrescriberCity, PrescriberState, PrescriberZip, PrescriberPhone
How do I query so that if PrescriberID from table 1 is in table 2, name/address/city/state/zip/phone come from table 2, but if not, leave the information that was there?
EDIT: I Tried a suggestion and it worked. Thank you.
SELECT t1.Id, t1.PBM_ID, t1.CLIENT_ID, t1.CLAIMNUMBER, t1.PATIENTLNAME, t1.PATIENTFNAME, t1.PATIENTGENDER, t1.PATIENTADDRESS1, t1.PATIENTADDRESS2, t1.PATIENTCITY, t1.PATIENTSTATE, t1.PATIENTZIP, t1.PATIENTDATEOFBIRTH, t1.PATIENTCELLPHONE, t1.DATEOFINJURY, t1.CLAIMORGIN, t1.DOS, t1.SUBCARRIER, t1.GROUPDESCRIPTION, t1.SUBGROUP, t1.POLICYNUMBER, t1.JURISDICTIONSTATE, t1.ADJUDICATIONDATE, t1.CLIENTBILLRECEIVEDDATE, t1.PRESCRIBERDEA, t1.PRESCRIBERNPI, COALESCE(t2.Name, t1.PRESCRIBERNAME) as PRESCRIBERNAME, COALESCE(t2.Address, t1.PRESCRIBERADDRESS) as PRESCRIBERADDRESS, COALESCE(t2.City, t1.PRESCRIBERCITY) as PRESCRIBERCITY, COALESCE(t2.State, t1.PRESCRIBERSTATE) as PRESCRIBERSTATE, COALESCE(t2.Zip, t1.PRESCRIBERZIP) as PRESCRIBERZIP, COALESCE(t2.Phone, t1.PRESCRIBERPHONE) as PRESCRIBERPHONE, t1.PHARMACYPAIDDATE, t1.PHARMACYNABP, t1.PHARMACYNPI, t1.PHARMACYTAXID, t1.PHARMACYNAME, t1.PHARMACYPHONE, t1.PHARMACYADDRESS, t1.PHARMACYCITY, t1.PHARMACYSTATE, t1.PHARMACYZIP, t1.THIRDPARTYREPRICERNABP, t1.THIRDPARTYNAME, t1.THIRDPARTYNETWORK, t1.THIRDPARTYCLAIMSOURCE, t1.RXNUMBER, t1.DRUGNDC, t1.DRUGGPI, t1.DRUGNAME, t1.DRUGSTRENGTH, t1.DRUGTYPE, t1.ISCOMPOUND, t1.INDIVIDUALMED, t1.CUMULATIVEMED, t1.LEGALCLASS, t1.DAW, t1.QUANTITY, t1.DAYSSUPPLY, t1.REFILLNUMBER, t1.INVOICENUMBER, t1.BILLEDDATE, t1.BILLEDAMOUNT, t1.AWP, t1.STATEFEE_UCPRICE, t1.SAVINGS, t1.EMPLOYERNAME, t1.PANUMBER, t1.PAAPPROVINGADJUSTER, t1.ADJUSTERNAME, t1.ADJUSTEREMAIL, t1.ADJUSTERPHONE, t1.BRANCHCODE, t1.BRANCHNAME, t1.LoadDate, t1.FileType, t1.AdjusterFname, t1.AdjusterLname, t1.Clean_NDC, t1.CASEMANAGERNAME, t1.CASEMANAGEREMAIL
FROM PBM_Common as t1
LEFT OUTER JOIN NPI_Records as t2
ON t1.PRESCRIBERNPI = t2.NPI_Number;
First start by writing a SQL statement that will give you the result set that contains your logic. Use a LEFT OUTER JOIN to get all records from table1 and only those records from table2 that match for each table1.prescriberid. Use COALESCE() to pick the table2 values, unless they are null, then use table1 values:
SELECT t1.patientID, t1.patientName, t1.patientAddress, ...., t1.PrescriberID, COALESCE(t2.PrescriberName, t1.PrescriberName) as PrescriberName, COALESCE(t2.PrescriberAddress, t1.PrecsriberAddress) as PrescriberAddress, ... plus many others
FROM table1 as t1
LEFT OUTER JOIN table2 as t2
ON t1.PrescriberID = t2.PrescriberID;
You could just stop there as you have the data you want. If you must override the values in your table1 table, then you'll need to develop this SELECT statement into an UPDATE statement. You can see some common ways to do this here. For this example I will use the CTE approach:
With correct_date AS
(
SELECT t1.patientID, t1.patientName, t1.patientAddress, ...., t1.PrescriberID, COALESCE(t2.PrescriberName, t1.PrescriberName) as PrescriberName, COALESCE(t2.PrescriberAddress, t1.PrecsriberAddress) as PrescriberAddress, ... plus many others
FROM table1 as t1
LEFT OUTER JOIN table2 as t2
ON t1.PrescriberID = t2.PrescriberID
)
UPDATE T2
SET PrescriberName = cd.PrescriberName,
PrescriberAddress = cd.PrescriberAddress
... plus many others
FROM table2 as T2
INNER JOIN correct_data cd
ON T2.PatientID = cd.PatiendID
Probably best to make a backup of table1 before running anything undo-able like an UPDATE statement.
Lastly, I think it's worth pointing out that this schema is terrible. Why store attributes of the prescriber in both tables where situations like the one you are trying to solve for can even exist. Just remove these columns from Table1 and leave only PrescriberID and then join in Table2 when you need those values.
Related
This clause keeps showing "ERROR: missing FROM-clause entry for table "subid" in postgresql 10.
How so?
UPDATE "io_S1"."tc_history"
SET "c_TIME" = TT."c_TIME",
"b_TIME_c"=TT."b_TIME_c",
"b_TIME_COLLECTION"=TT."b_TIME_COLLECTION",
"c_NOTE"=TT."c_NOTE",
"b_TIME_TAXI"=TT."b_TIME_TAXI",
"b_LOCATION_TAXI"=TT."b_LOCATION_TAXI",
"ESTABLISHED_TIME"=TT."c_TIME"
FROM (
SELECT "c_TIME","b_TIME_c","b_TIME_COLLECTION","c_NOTE","b_TIME_TAXI","b_LOCATION_TAXI","c_TIME"
FROM "io_TRACE"."PERSONAL_Tc_RECORD" TR
WHERE TR."PERSONAL_SERIAL_ID" IN (SELECT "SUBJECT_ID"
FROM "io_COLLECTION"."COLLECTION_CONSENT_RECORD" AS SUBID
WHERE SUBID."SUBJECT_CITIZEN_ID" IN (SELECT "SUBJECT_CITIZEN_ID"
FROM "io_S1"."tc_history" AS TC))
) AS TT
WHERE "SUBJECT_CITIZEN_ID"=SUBID."SUBJECT_CITIZEN_ID";
You can try below
UPDATE "io_S1"."tc_history"
SET "c_TIME" = TT."c_TIME",
"b_TIME_c"=TT."b_TIME_c",
"b_TIME_COLLECTION"=TT."b_TIME_COLLECTION",
"c_NOTE"=TT."c_NOTE",
"b_TIME_TAXI"=TT."b_TIME_TAXI",
"b_LOCATION_TAXI"=TT."b_LOCATION_TAXI",
"ESTABLISHED_TIME"=TT."c_TIME"
FROM (
SELECT "c_TIME","b_TIME_c","b_TIME_COLLECTION","c_NOTE","b_TIME_TAXI","b_LOCATION_TAXI","c_TIME",
SUBID."SUBJECT_CITIZEN_ID"
FROM "io_TRACE"."PERSONAL_Tc_RECORD" TR inner join
"io_COLLECTION"."COLLECTION_CONSENT_RECORD" AS SUBID
on TR."PERSONAL_SERIAL_ID"=SUBID."SUBJECT_ID"
inner join "io_S1"."tc_history" AS TC
on TC."SUBJECT_CITIZEN_ID"=SUBID."SUBJECT_CITIZEN_ID"
) AS TT
WHERE "SUBJECT_CITIZEN_ID"=TT."SUBJECT_CITIZEN_ID";
Alias used 'SUBID' is out of scope, therefore, the from clause doesn't recognize it. Best to use the normal convention as 'TableName.ColumnName', since two tables have the same column name.
I am getting an error saying that I cannot use Top 10 with distinct I am wondering if there is any way I can get my query to work on that fashion. this is what the error is saying .
[Teradata Database] [6916] TOP N Syntax error: Top N option is not supported with DISTINCT option.
Query Below:
Thank you.
Select Distinct TOP 10 t1.Adjustment_ID, t1.OfficeNum, t1.InvoiceNum, t1.PatientNum,
t1.CurrentStatus, t1.AdjustmentTotal, t1.SubmittedOn, t1.UserSubmitted,
t1.Invoice_Type, t1.Pat_First_Name, t1.Pat_Last_Name,t2.Reason_Code FROM App_UnityAdj_AdjInfo_Tbl t1
Left Join RCM_WORK_PRD.App_UnityAdj_AdjRecord_Tbl t2
On t1.Adjustment_ID = t2.Adjustment_ID
Where t1.UserSubmitted = 'Name' AND (t1.CurrentStatus = 'Pending' OR t1.CurrentStatus = 'Deny')
What are you trying to do? You have columns in the SELECT that are not in the GROUP BY. You also have TOP without ORDER BY, which is suspicious.
One simple method is to move all the SELECT columns to the GROUP BY:
select TOP 10 t1.Adjustment_ID, t1.OfficeNum, t1.InvoiceNum, t1.PatientNum,
t1.CurrentStatus, t1.AdjustmentTotal, t1.SubmittedOn, t1.UserSubmitted,
t1.Invoice_Type, t1.Pat_First_Name, t1.Pat_Last_Name, t2.Reason_Code
from App_UnityAdj_AdjInfo_Tbl t1 Left Join
RCM_WORK_PRD.App_UnityAdj_AdjRecord_Tbl t2
On t1.Adjustment_ID = t2.Adjustment_ID
where t1.UserSubmitted = 'Name' AND
t1.CurrentStatus in ('Pending', 'Deny')
group by t1.Adjustment_ID, t1.OfficeNum, t1.InvoiceNum, t1.PatientNum,
t1.CurrentStatus, t1.AdjustmentTotal, t1.SubmittedOn, t1.UserSubmitted,
t1.Invoice_Type, t1.Pat_First_Name, t1.Pat_Last_Name,t2.Reason_Code;
I think I figured it out, in case anyone wants to know it is sample 10
Select Distinct t1.Adjustment_ID, t1.OfficeNum, t1.InvoiceNum, t1.PatientNum,
t1.CurrentStatus, t1.AdjustmentTotal, t1.SubmittedOn, t1.UserSubmitted,
t1.Invoice_Type, t1.Pat_First_Name, t1.Pat_Last_Name,t2.Reason_Code FROM App_UnityAdj_AdjInfo_Tbl t1
Left Join RCM_WORK_PRD.App_UnityAdj_AdjRecord_Tbl t2
On t1.Adjustment_ID = t2.Adjustment_ID
Where t1.AssignedTo IS null AND (t1.CurrentStatus = 'Pending')
sample 10
I have this sql:
Select
t1.field01,
t1.field02,
t2.name02,
t2.surname02,
from
lib01/FirstFile as t1,
lib02/SecondFile (lib02/ThirdFile) as t2
where
t1.field01 = t2.field02 (or t3.filed02)
I need to have the condition that if t1.field02 is equal to "AX" I have to read the second file (lib02/SecondFile), if t1.field02 is equal to "BX" I have to read by the third file (lib02/ThirdFile).
Select
CASE
WHEN ((Select t1.field02 from lib01/FirstFile as t1 ) = AX)
THEN Select t2.name02, t2.surname02
from lib02/SecondFile (lib02/ThirdFile) as t2
CASE
WHEN ((Select t1.field02 from lib01/FirstFile as t1 ) = BX)
THEN Select t3.name02, t3.surname02
from (lib02/ThirdFile) as t3
END
Change the code after each THEN clause if that retrieve is not what you want, but that is the logic.
And try to avoid use old syntax for join
I have the following SQL query:
SELECT VehicleRegistrations.ID, VehicleRegistrations.VehicleReg,
VehicleRegistrations.Phone, VehicleType.VehicleTypeDescription,
dt.ID AS 'CostID', dt.IVehHire, dt.FixedCostPerYear, dt.VehicleParts,
dt.MaintenancePerMile, dt.DateEffective
FROM VehicleRegistrations
INNER JOIN VehicleType ON VehicleRegistrations.VehicleType = VehicleType.ID
LEFT OUTER JOIN (SELECT TOP (1) ID, VehicleRegID, DateEffective, IVehHire,
FixedCostPerYear, VehicleParts, MaintenancePerMile
FROM VehicleFixedCosts
WHERE (DateEffective <= GETDATE())
ORDER BY DateEffective DESC) AS dt
ON dt.VehicleRegID = VehicleRegistrations.ID
What I basically want to do is always select the top 1 record from the 'VehicleFixedCosts' table, where the VehicleRegID matches the one in the main query. What is happening here is that it's selecting the top row before the join, so if the vehicle registration of the top row doesn't match the one we're joining to it returns nothing.
Any ideas? I really don't want to have use subselects for each of the columns I need to return
Try this:
SELECT vr.ID, vr.VehicleReg,
vr.Phone, VehicleType.VehicleTypeDescription,
dt.ID AS 'CostID', dt.IVehHire, dt.FixedCostPerYear, dt.VehicleParts,
dt.MaintenancePerMile, dt.DateEffective
FROM VehicleRegistrations vr
INNER JOIN VehicleType ON vr.VehicleType = VehicleType.ID
LEFT OUTER JOIN (
SELECT ID, VehicleRegID, DateEffective, IVehHire, FixedCostPerYear, VehicleParts, MaintenancePerMile
FROM VehicleFixedCosts vfc
JOIN (
select VehicleRegID, max(DateEffective) as DateEffective
from VehicleFixedCosts
where DateEffective <= getdate()
group by VehicleRegID
) t ON vfc.VehicleRegID = t.VehicleRegID and vfc.DateEffective = t.DateEffective
) AS dt
ON dt.VehicleRegID = vr.ID
Subquery underneath dt might need some grouping but without schema (and maybe sample data) it's hard to say which column should be involved in that.
Query:
SELECT t1.id,
t1.ads_city,
t1.ads_title,
t1.ads_description,
t1.ads_type,
t2.ads_activate,
t2.postads_id,
t2.ads_id
FROM table_1 t1
JOIN nextpostads t2 ON t1.id = t2.postads_id
WHERE MATCH(t1.ads_title,t1.ads_description) AGAINST ('LCD projector ,' IN BOOLEAN MODE)
AND t2.ads_activate='Yes'
AND t1.ads_type='offering'
I have 2 record first record title is
" LCD projector,plasma display,recording speaker products"
and second record title is
" Interactive products(projection screen,projectors,touch panel,network camera)"
But from the above query I am not related result what is problem with it ?
My guess is the t2.ads_activate value isn't "Yes" and/or the t1.ads_type value isn't "offering". Try:
SELECT t1.id,
t1.ads_city,
t1.ads_title,
t1.ads_description,
t1.ads_type,
t2.ads_activate,
t2.postads_id,
t2.ads_id
FROM table_1 t1
JOIN nextpostads t2 ON t1.id = t2.postads_id
WHERE MATCH(t1.ads_title,t1.ads_description) AGAINST ('LCD projector ,' IN BOOLEAN MODE)
If the records you expect are returned, add the missing WHERE clauses , testing them one at a time. Otherwise, there's something with your JOIN...