Im trying to update a single table with output of a select statement which returns two values. One is the accountid that I need to update in the table, and the other is the information I need to update.
Here is my select statement
select CTP.CARETEAMPATIENTID, O.ORGANIZATIONID
from acts.careteampatient ctp,
ods.member m,
ods.supplierorganization so,
ods.MASTERSUPPLIERSUPPLIERRELATION mssr,
ods.INSURANCEORGSUPPLIERRELATION IOSR,
ods.INSURANCEORGANIZATION IO,
acts.organization o
where ctp.accountorgid is null
and m.primarymemberplanid = ctp.primarymemberplanid
and SO.AHMSUPPLIERID = M.AHMSUPPLIERID
and mssr.SUPPLIERID = so.SUPPLIERORGID
AND iosr.SUPPLIERID = so.SUPPLIERORGID
AND io.INSURANCEORGID = iosr.INSURANCEORGID
and io.processingmodecd = 'P'
and so.usagemnemonic = 'P'
and O.ODSACCOUNTID = IO.INSURANCEORGID
and O.ACCOUNTFLG = 'Y'
I want to do something along the lines of
update careteampatient
from (select CTP.CARETEAMPATIENTID patientid, O.ORGANIZATIONID orgid
from acts.careteampatient ctp,
ods.member m,
ods.supplierorganization so,
ods.MASTERSUPPLIERSUPPLIERRELATION mssr,
ods.INSURANCEORGSUPPLIERRELATION IOSR,
ods.INSURANCEORGANIZATION IO,
acts.organization o
where ctp.accountorgid is null
and m.primarymemberplanid = ctp.primarymemberplanid
and SO.AHMSUPPLIERID = M.AHMSUPPLIERID
and mssr.SUPPLIERID = so.SUPPLIERORGID
AND iosr.SUPPLIERID = so.SUPPLIERORGID
AND io.INSURANCEORGID = iosr.INSURANCEORGID
and io.processingmodecd = 'P'
and so.usagemnemonic = 'P'
and o.odsaccountid = io.insuranceorgid
and o.accountflg = 'Y') b
set a.accountorgid = b.orgid
where a.careteampatientid = b.patientid
Here is the Merge I also tried
merge into careteampatient a
using (select CTP.CARETEAMPATIENTID patientid, O.ORGANIZATIONID orgid
from acts.careteampatient ctp,
ods.member m,
ods.supplierorganization so,
ods.MASTERSUPPLIERSUPPLIERRELATION mssr,
ods.INSURANCEORGSUPPLIERRELATION IOSR,
ods.INSURANCEORGANIZATION IO,
acts.organization o
where ctp.accountorgid is null
and m.primarymemberplanid = ctp.primarymemberplanid
and SO.AHMSUPPLIERID = M.AHMSUPPLIERID
and mssr.SUPPLIERID = so.SUPPLIERORGID
AND iosr.SUPPLIERID = so.SUPPLIERORGID
AND io.INSURANCEORGID = iosr.INSURANCEORGID
and io.processingmodecd = 'P'
and so.usagemnemonic = 'P'
and o.odsaccountid = io.insuranceorgid
and o.accountflg = 'Y') b
on a.careteampatientid = b.patientid
when matched then
update
set a.accountorgid = b.orgid
This isnt working and Im fresh out of ideas, any help would be awesome. Thanks
Figured it out, I was missing brackets on my ON. It should be on (a.careteampatientid = b.patientid) also my select should read select distinct
Related
In the below query when i comment one line AND EVT.EVT_EVNT_ID(+) = DPT.DPT_EVNT_ID or add outer join (+) in one line AND EVT.EVT_ENTITY_ID ='G' the query will fetch results, else it will not give any results.
`
select * from
(select B11.ext_db_id,B11.FRST_LINE_OF_PRFRD_NAME, E11.*, D1.D1_evnt_id,
(SELECT E1.E1_DT
FROM S1core.E1_EVNT_DT_DTLS E1
WHERE E1.E1_ENTITY_ID = D1.D1_ENTITY_ID
AND E1.E1_EVNT_ID = D1.D1_EVNT_ID
AND E1.E1_D1_ID = D1.D1_D1_ID
AND E1.E1_DT_TYP = 'MEET'
AND E1.E1_OPTN_SEQ_N = '999'
AND E1.STATUS = 'AUTHD' ) "MEET_DATE"
FROM S1core.E1_EVNT_DT_DTLS E11, S1core.SECURITY_ACCOUNT SFA,
S1core.EXTRNL_SYS_DETAILS EXT,
S1core.D1_DPOT_EVNT_DTLS D1, S1core.IP_ACNT_RELATION i1,
S1core.ELG_ELGBLTY E1,S1core.P1_D1_PRXY_DTLS P1, S1core.EVT_DTLS EVT,
S1core.BUSINESS_PARTNER B1, S1core.BUSINESS_PARTNER B11
WHERE
P1.P1_EVNT_ID(+) = D1.D1_EVNT_ID
AND B1.BP_ID = i1.IP_ID
AND SFA.BP_ID = B11.BP_ID
AND B11.OWNER_ENTITY = SFA.OWNER_ENTITY
AND P1.P1_D1_ID(+) = D1.D1_D1_ID
AND P1.P1_ENTITY_ID(+) = D1.D1_ENTITY_ID
AND P1.STATUS(+) = 'AUTHD'
AND EXT.LVL_REF = SFA.SCA_REF
AND EXT.OWNER_ENTITY = SFA.owner_entity
AND EXT.EXTRNL_SYS_ID = '39'
AND EXT.BP_ID = SFA.BP_ID
AND EXT.LVL = 5
AND E1.ELG_SEC_ID = D1.D1_SEC_ID
AND D1.D1_ENTITY_ID = E1.ELG_ENTITY_ID
AND D1.D1_EVNT_ID = E1.ELG_EVNT_ID
AND D1.D1_D1_ID = E1.ELG_D1_ID
AND E1.ELG_ENTITY_ID = SFA.OWNER_ENTITY
AND E1.ELG_ACNT_ID = SFA.SCA_REF
AND i1.owner_entity = SFA.OWNER_ENTITY
AND i1.owner_entity = B1.OWNER_ENTITY
AND i1.SCA_REF = SFA.SCA_REF
AND i1.stat <> 2
AND EVT.EVT_ENTITY_ID ='G'
AND EVT.EVT_EVNT_ID(+) = D1.D1_EVNT_ID
AND EVT.STATUS(+) = 'AUTHD'
AND E11.E1_ENTITY_ID = D1.D1_ENTITY_ID
AND E11.E1_EVNT_ID = D1.D1_EVNT_ID
AND E11.E1_D1_ID = D1.D1_D1_ID
AND D1.D1_EVNT_GRP = 'MEETING'
AND E1.ELG_FNL_ELGBL_QNTTY > 0
AND D1.D1_ENTITY_ID = 'GSSIN' -- P_D1_ENTITY_ID
AND B11.ext_db_id LIKE 'LICMF' -- P_GLOBAL_CUSTODIAN
AND B1.ext_db_id LIKE '%' -- P_FUND_MANAGER
AND SUBSTR(EXT.LEVEL_EXTNL_SYS,1,5) LIKE '%' -- P_SUB_ACCOUNT_ID
AND SUBSTR(EXT.LEVEL_EXTNL_SYS,6,9) LIKE '%' -- P_SCHEMA_ID
AND SUBSTR( D1.D1_EVNT_TYP, 1, 4 ) LIKE '%' -- P_EVENT_TYPE
AND E11.E1_DT_TYP = 'MEET'
and D1.D1_evnt_id='12457886544'
AND D1.OU_ID IN('17','80') -- ('S1 INDIA PC')
) ;
Am I missing any outer join or the query is having any other issue as i can see that dpt_evnt_id='12457886544'is available in all tables.
`
I tried to run this query by commenting AND EVT.EVT_EVNT_ID(+) = DPT.DPT_EVNT_ID
and by adding outer join in EVT.EVT_ENTITY_ID (+) ='G'
You have:
AND EVT.EVT_ENTITY_ID ='G'
AND EVT.EVT_EVNT_ID(+) = D1.D1_EVNT_ID
AND EVT.STATUS(+) = 'AUTHD'
Even though the second two have (+) the first one does not so the join to EVT will be treated as an INNER JOIN and not an OUTER JOIN.
You should convert your code to use ANSI joins as it will be much easier to spot issues like this.
Note: There may be more issues like this. However, your code has so many joins, the syntax is archaic, and we have no way of validating the correctness that I stopped looking after finding the first issue. You are going to need to debug it all to check for any other issues.
I have a select statement like below :
SELECT * FROM
(SELECT d.value, d.line, d.language, l.pos_id, l.vehicle_id,l.vehicle_given
FROM Vehicle_value d,
vehicle_document l
WHERE d.gen_id = l.gen_id
AND d.pos_id = l.pos_id
AND d.gen_id = 1255) d1
LEFT OUTER JOIN
(SELECT d.value, d.line, d.language, l.pos_id, l.vehicle_id,l.vehicle_given
FROM TBB_GEN_Daten d,
TBB_GEN_Lesehilfe l
FROM Vehicle_value d,
vehicle_document l,
WHERE d.gen_id = l.gen_id
AND d.pos_id = l.pos_id
AND d.gen_id = 1254) d2
ON d1.value = d2.value
AND d1.pos_id = d2.pos_id
AND case when d1.vehicle_given = 'YES' then (d1.vehicle_id = d2.vehicle_id)
else end
AND d1.value is not null;
So I wanted to compare the d1.vehicle_id to d2.vehicle_id only when the d1.vehicle_given value is 'YES'. But the case statement i have used here is wrong and hence it doesnt work. is there any other to do this comparision ?
Bunch of typos in your code, but - when fixed, and with CASE being rewritten - query looks like this:
SELECT *
FROM (SELECT d.VALUE,
d.line,
d.language,
l.pos_id,
l.vehicle_id,
l.vehicle_given
FROM Vehicle_value d, vehicle_document l
WHERE d.gen_id = l.gen_id
AND d.pos_id = l.pos_id
AND d.gen_id = 1255) d1
LEFT OUTER JOIN (SELECT d.VALUE,
d.line,
d.language,
l.pos_id,
l.vehicle_id,
l.vehicle_given
FROM TBB_GEN_Daten d,
TBB_GEN_Lesehilfe l,
Vehicle_value d,
vehicle_document l
WHERE d.gen_id = l.gen_id
AND d.pos_id = l.pos_id
AND d.gen_id = 1254) d2
ON d1.VALUE = d2.VALUE
AND d1.pos_id = d2.pos_id
AND d1.vehicle_id =
CASE
WHEN d1.vehicle_given = 'YES' THEN d2.vehicle_id
ELSE d1.vehicle_id
END
AND d1.VALUE IS NOT NULL;
Use brackets and an OR condition for either the matching condition or when it is not YES:
AND ( d1.vehicle_given <> 'YES'
OR d1.vehicle_given IS NULL
OR d1.vehicle_id = d2.vehicle_id )
If you use a CASE expression such as:
AND d1.vehicle_id = CASE
WHEN d1.vehicle_given = 'YES'
THEN d2.vehicle_id
ELSE d1.vehicle_id
END
Then when the ELSE condition is matched you will effectively end up with AND d1.vehicle_id = d1.vehicle_id. This, naively, looks like it should be fine but if d1.vehicle_id is NULL then you will end up comparing NULL = NULL and the result is not truthy and the row will be excluded and that is not what you want.
Why does this code show invalid identifier when sum is used in distance and air_time column?
When sum is not used this statement process successfully but using sum I get error? I need to use sum for this statement.
MERGE INTO FACT_COMPANY_GROWTH F
USING (SELECT DISTINCT TIME_ID, FLIGHT_KEY, AEROPLANE_KEY, SUM(DISTANCE) AS TOTAL_DISTANCE, SUM(AIR_TIME) AS TOTAL_AIRTIME
FROM TRANSFORM_FLIGHT T
INNER JOIN TRANSFORM_AEROPLANE A
ON T.FK_AEROPLANE_KEY = A.AEROPLANE_KEY
INNER JOIN DIM_TIME D
ON D.YEAR = T.YEAR
AND D.MONTH = T.MONTH
GROUP BY TIME_ID, FLIGHT_KEY, AEROPLANE_KEY) S
ON (F.FK1_TIME_ID = S.TIME_ID
AND F.FK2_FLIGHT_KEY = S.FLIGHT_KEY
AND F.FK3_AEROPLANE_KEY = S.AEROPLANE_KEY
)
WHEN MATCHED THEN
UPDATE SET
F.TOTAL_AIRTIME = S.AIR_TIME,
F.TOTAL_DISTANCE = S.DISTANCE,
F.TOTAL_NO_OF_FLIGHTS = S.FLIGHT_KEY,
F.TOTAL_NO_OF_AEROPLANE = S.AEROPLANE_KEY
WHEN NOT MATCHED THEN
INSERT(FACT_ID, FK1_TIME_ID, FK2_FLIGHT_KEY, FK3_AEROPLANE_KEY, TOTAL_DISTANCE, TOTAL_AIRTIME, TOTAL_NO_OF_FLIGHTS, TOTAL_NO_OF_AEROPLANE)
VALUES
(NULL, S.TIME_ID, S.FLIGHT_KEY, S.AEROPLANE_KEY, S.DISTANCE, S.AIR_TIME, S.FLIGHT_KEY, S.AEROPLANE_KEY);
USING(
SELECT DISTINCT
TIME_ID,
FLIGHT_KEY,
AEROPLANE_KEY,
SUM(DISTANCE) AS TOTAL_DISTANCE,
SUM(AIR_TIME) AS TOTAL_AIRTIME
...) S
The problem is at UPDATE SET F.TOTAL_AIRTIME = S.AIR_TIME. There are 5 fields defined in S and none is named AIR_TIME.
UPDATE SET
F.TOTAL_AIRTIME = S.TOTAL_AIRTIME,
F.TOTAL_DISTANCE = S.TOTAL_DISTANCE,
I'm trying to convert the following SQL query to LINQ, but getting different result count with both,
SQL Query:
SELECT T5.CNTR, T5.BenefitCode,T5.ApprovedFlag,
T5.PaymentFrequencyCode, T5.InstalmentAmt, T5.TotalAmt,
T5.CarRego
FROM
dbo.EmployeeBenefit As T5
LEFT JOIN dbo.Payee ON T5.PayeeCntr = dbo.Payee.CNTR
LEFT JOIN dbo.BankDetails ON dbo.Payee.BankCntr = dbo.BankDetails.BankCntr
Left Join dbo.EmployeeCar As T4 on T5.EmployeeCarCntr=T4.Cntr
Inner Join dbo.EmployeeEntity As T1 On T5.EmployeeEntityCntr=T1.EmployeeEntityCntr
Inner Join dbo.EmployerEntity As T2 On T1.EmployerEntityCntr=T2.EmployerEntityCntr
where T5.EmployeeCntr = 117165
AND ((T5.EndDate is Null) OR (T5.EndDate >= GETDATE()))
LINQ:
var result = (from employeeBenefit in context.EmployeeBenefit
from payee in context.Payee.Where(x => x.Cntr == employeeBenefit.PayeeCntr).DefaultIfEmpty()
from bankDetails in context.BankDetails.Where(x => x.BankCntr == employeeBenefit.PayeeCntr).DefaultIfEmpty()
from employeeCar in context.EmployeeCar.Where(x => x.Cntr == payee.BankCntr).DefaultIfEmpty()
from employeeEntity in context.EmployeeEntity
where employeeEntity.EmployeeEntityCntr == employeeBenefit.EmployeeEntityCntr
from employeeEntity1 in context.EmployeeEntity
where employeeEntity.EmployerEntityCntr == employeeEntity1.EmployerEntityCntr
&& employeeBenefit.EmployeeCntr == iEmployeeID
&& (!employeeBenefit.EndDate.HasValue || employeeBenefit.EndDate >= DateTime.Now)
&& employeeBenefit.EmployeeCntr == 117165
&& employeeBenefit.CarRego == registration
select new
{
CNTR = employeeBenefit.Cntr,
BenefitCode = employeeBenefit.BenefitCode,
PaymentFrequencyCode = employeeBenefit.PaymentFrequencyCode,
InstalmentAmount = employeeBenefit.InstalmentAmt,
TotalAmount = employeeBenefit.TotalAmt,
CarRego = employeeBenefit.CarRego,
ApprovedFlag = employeeBenefit.ApprovedFlag
}).ToList();
Please let me know what i'm missing.
For the data in my database the SQL query is returning 10 records. But, the LINQ is returning 2700 records.
Not a full answer (I'm late for work) but:
var result = (from T5 in context.EmployeeBenefit
join PY in dbo.Payee on T5.PayeeCntr equals PY.CNTR into PY1
where T5.EmployeeCntr = 117165
select new {
CNTR = T5.Cntr,
...
}
).ToList();
It was the issue with the condition mismatch that i had done in the LINQ. The below query just worked fine. Thank you for helping me with the issue.
var result = (from employeeBenefit in context.EmployeeBenefit
from payee in context.Payee.Where(x => x.Cntr == employeeBenefit.PayeeCntr).DefaultIfEmpty()
from bankDetails in context.BankDetails.Where(x => x.BankCntr == payee.BankCntr).DefaultIfEmpty()
from employeeCar in context.EmployeeCar.Where(x => x.Cntr == employeeBenefit.EmployeeCarCntr).DefaultIfEmpty()
join employeeEntity in context.EmployeeEntity
on employeeBenefit.EmployeeEntityCntr equals employeeEntity.EmployeeEntityCntr
join employerEntity in context.EmployerEntity
on employeeEntity.EmployerEntityCntr equals employerEntity.EmployerEntityCntr
where employeeBenefit.EmployeeCntr == 117165 && (!employeeBenefit.EndDate.HasValue || employeeBenefit.EndDate >= DateTime.Now)
select new
{
CNTR = employeeBenefit.Cntr,
BenefitCode = employeeBenefit.BenefitCode,
PaymentFrequencyCode = employeeBenefit.PaymentFrequencyCode,
InstalmentAmount = employeeBenefit.InstalmentAmt,
TotalAmount = employeeBenefit.TotalAmt,
CarRego = employeeBenefit.CarRego,
ApprovedFlag = employeeBenefit.ApprovedFlag
}).ToList();
I have below linq select coding. The result become empty if db.actions.ACTC_DBID values are null. How to avoid that. I need to get the query result with null values.
var apList = (from r in db.components
join o in db.cmp_actionpanel on r.CMP_DBID equals o.CMP_DBID
join u in db.cmp_button on o.BTN_DBID equals u.BTN_DBID
join a in db.actions on o.ACTC_DBID equals a.ACTC_DBID
where (r.ORG_DBID == org && r.CMP_DBID == id)
orderby o.ORDER_IDX
select new
{
AP_DBID = o.AP_DBID,
CMP_NAME = r.CMP_NAME,
CMP_DBID = r.CMP_DBID,
BTN_NAME = u.BTN_NAME,
BTN_DBID = u.BTN_DBID,
ORDER_IDX = o.ORDER_IDX,
ALWAYS_VISIBLE = o.ALWAYS_VISIBLE,
ACTC_DBID = a.ACTC_DBID,
ACTION_CODE = a.ACTION_CODE,
ACTION_CMP_DBID = o.ACTION_CMP_DBID,
STARTS_JOB_PROGRESSION = o.STARTS_JOB_PROGRESSION,
VISIBLE_ON = o.VISIBLE_ON,
CAPTURE_INFO = o.CAPTURE_INFO,
COMMAND = o.COMMAND
}).ToList();
var apList = (from r in db.components
join o in db.cmp_actionpanel on r.CMP_DBID equals o.CMP_DBID
join u in db.cmp_button on o.BTN_DBID equals u.BTN_DBID
join a in db.actions on o.ACTC_DBID equals a.ACTC_DBID into aGroup
where (r.ORG_DBID == org && r.CMP_DBID == id)
orderby o.ORDER_IDX
from ag in aGroup.DefaultIfEmpty()
select new
{
AP_DBID = o.AP_DBID,
CMP_NAME = r.CMP_NAME,
CMP_DBID = r.CMP_DBID,
BTN_NAME = u.BTN_NAME,
BTN_DBID = u.BTN_DBID,
ORDER_IDX = o.ORDER_IDX,
ALWAYS_VISIBLE = o.ALWAYS_VISIBLE,
ACTC_DBID = ag.ACTC_DBID == null ? (int?)null : ag.ACTC_DBID,
ACTION_CODE = ag.ACTION_CODE,
ACTION_CMP_DBID = o.ACTION_CMP_DBID,
STARTS_JOB_PROGRESSION = o.STARTS_JOB_PROGRESSION,
VISIBLE_ON = o.VISIBLE_ON,
CAPTURE_INFO = o.CAPTURE_INFO,
COMMAND = o.COMMAND
}).ToList();