I wrote a query that does an inner join of two subqueries. The first subquery has an alias of "SRC" and the other has an alias of "DEST". What I want to do is update some fields in the table NomineeActionLegislatorVoteDetail (part of DEST subquery) with values from the table Nominee_Committee_Vote (part of SRC subquery). It souds easy but I just cannot figure out how to do it. Does anyone have any suggestions? Any help would be appreciated.
Here is the query I wrote:
select *
from (
select ncv.*,
na.NomineeActionId,
l.LegislatorId
from ongoing..Nominee_Committee_Vote ncv
inner join azleg..NomineeAction na on
ncv.session_id = na.x_session_id and
ncv.committee_id = na.x_committee_id and
ncv.agency_id = na.x_agency_id and
ncv.position_id = na.x_position_id and
ncv.nominee_id = na.x_nominee_id and
ncv.received_date = na.x_received_date
inner join status..session s on
ncv.session_id = s.session_id
inner join azleg..Legislator l on
ncv.member_id = l.x_member_id and
s.legislature = l.LegislatureId
) SRC
inner join (
select votedetail.*
from azleg..NomineeActionLegislatorVoteDetail votedetail
inner join azleg..NomineeAction nom_action on
votedetail.NomineeActionId = nom_action.NomineeActionId
) DEST on
SRC.agency_id = DEST.x_agency_id and
SRC.position_id = DEST.x_position_id and
SRC.nominee_id = DEST.x_nominee_id and
SRC.received_date = DEST.x_received_date and
SRC.session_id = DEST.x_session_id and
SRC.committee_id = DEST.x_committee_id and
SRC.member_id = DEST.x_member_id
where SRC.NomineeActionId <> DEST.NomineeActionId
OR SRC.LegislatorId <> DEST.LegislatorId
OR SRC.Vote <> DEST.Vote
Can you insert the update in front of the sub queries
UPDATE NomineeActionLegislatorVoteDetail
SET NomineeActionLegislatorVoteDetail.COLUMNNAME = src.VALUE
--SubQueriesBelow
from
(select ncv.*, na.NomineeActionId, l.LegislatorId from ongoing..Nominee_Committee_Vote ncv
inner join azleg..NomineeAction na on
ncv.session_id = na.x_session_id and
ncv.committee_id = na.x_committee_id and
ncv.agency_id = na.x_agency_id and
ncv.position_id = na.x_position_id and
ncv.nominee_id = na.x_nominee_id and
ncv.received_date = na.x_received_date
inner join status..session s on
ncv.session_id = s.session_id
inner join azleg..Legislator l on
ncv.member_id = l.x_member_id and
s.legislature = l.LegislatureId) SRC
inner join
(select votedetail.* from azleg..NomineeActionLegislatorVoteDetail votedetail
inner join azleg..NomineeAction nom_action on
votedetail.NomineeActionId = nom_action.NomineeActionId) DEST on
SRC.agency_id = DEST.x_agency_id and
SRC.position_id = DEST.x_position_id and
SRC.nominee_id = DEST.x_nominee_id and
SRC.received_date = DEST.x_received_date and
SRC.session_id = DEST.x_session_id and
SRC.committee_id = DEST.x_committee_id and
SRC.member_id = DEST.x_member_id
where SRC.NomineeActionId <> DEST.NomineeActionId
OR SRC.LegislatorId <> DEST.LegislatorId
OR SRC.Vote <> DEST.Vote
It looks like the update is on a table in the sub query so it could be re factored to have one subquery
I've adjusted the lay-out of your query a bit to disclose better where subqueries start and end. In your DEST subquery, you join NomineeAction but you don't select any columns from it, so it only filters rows from NomineeActionLegislatorVoteDetail for which no NomineeAction exists. So really you wanna be updating and joining on columns from NomineeActionLegislatorVoteDetail exclusively. Based on that, I would write the update query like so:
update votedetail set
votedetail.SomeColumn = SomeValue,
votedetail.SomeOtherColumn = SomeOtherValue
from azleg..NomineeActionLegislatorVoteDetail votedetail
join azleg..NomineeAction nom_action on
votedetail.NomineeActionId = nom_action.NomineeActionId
join (
select ncv.*,
na.NomineeActionId,
l.LegislatorId
from ongoing..Nominee_Committee_Vote ncv
inner join azleg..NomineeAction na on
ncv.session_id = na.x_session_id and
ncv.committee_id = na.x_committee_id and
ncv.agency_id = na.x_agency_id and
ncv.position_id = na.x_position_id and
ncv.nominee_id = na.x_nominee_id and
ncv.received_date = na.x_received_date
inner join status..session s on
ncv.session_id = s.session_id
inner join azleg..Legislator l on
ncv.member_id = l.x_member_id and
s.legislature = l.LegislatureId
) SRC on
SRC.agency_id = votedetail.x_agency_id and
SRC.position_id = votedetail.x_position_id and
SRC.nominee_id = votedetail.x_nominee_id and
SRC.received_date = votedetail.x_received_date and
SRC.session_id = votedetail.x_session_id and
SRC.committee_id = votedetail.x_committee_id and
SRC.member_id = votedetail.x_member_id
where SRC.NomineeActionId <> votedetail.NomineeActionId
OR SRC.LegislatorId <> votedetail.LegislatorId
OR SRC.Vote <> votedetail.Vote
Related
Any possibility methods to avoiding "Union all" when different joining > conditions varies on each section
SELECT RS1.*, EXL.*
FROM "EXL" "EXL"
INNER JOIN "RS1" "RS1"
ON "RS1"."HEADER_KEY" = "EXL"."HEADER_KEY"
WHERE "RS1"."PIPE_KEY" = '1109' AND
"RS1"."COLK" IS NULL AND
"RS1".CT1 = 0 AND "RS1".CT2 > 0
UNION ALL
SELECT RS1.*, EXL.*
FROM "EXL" "EXL"
INNER JOIN "RS1" "RS1"
ON "RS1"."HEADER_KEY" = "EXL"."HEADER_KEY"
INNER JOIN "YFS"."STATUS_MAP" "SOS"
ON "SOS"."STATUS" = "RS1"."STATUS"
INNER JOIN "RS1" "RS2"
ON "RS2"."LINE_KEY" = "RS1"."CHAINLINE_KEY" AND
"RS2"."PIPEKEY" = "SOS"."TYPE_KEY" AND
"RS2"."STATUS" = "SOS"."EXTN_STATUS" AND
"RS2"."PIPE_KEY" = '4093'
WHERE "RS1"."PIPE_KEY" = '1109' AND
"RS1"."COLK" IS NULL AND
"RS1".CT1 = 0 AND "RS1".CT2 > 0
Since the two SELECTs have exactly the same WHERE-condition, I assume that you want to return rows from EXL, even when rows from YFS.STATUS_MAP are missing (otherwise please explain what your intention is). You can do this with LEFT JOIN
SELECT RS1.*, EXL.*
FROM
"EXL"
INNER JOIN "RS1"
ON "RS1"."HEADER_KEY" = "EXL"."HEADER_KEY"
LEFT JOIN "YFS"."STATUS_MAP" "SOS"
ON "SOS"."STATUS" = "RS1"."STATUS"
LEFT JOIN "RS1" "RS2"
ON "RS2"."LINE_KEY" = "RS1"."CHAIN_LINE_KEY" AND
"RS2"."PIPEKEY" = "SOS"."TYPE_KEY" AND
"RS2"."STATUS" = "SOS"."EXTN_STATUS" AND
"RS2"."PIPELINE_KEY" = '4093'
WHERE "RS1"."PIPELINE_KEY" = '1109' AND
"RS1"."COLK" IS NULL AND
"RS1".CT1 = 0 AND "RS1".CT2 > 0
I have this query in sql
SELECT
SI_Articulo = COALESCE(t.SI_Articulo, c.SI_Num_Articulo),
SI_Ubicacion = COALESCE(t.SI_Ubicacion, c.SI_Ubicacion),
SI_OV = COALESCE(c.SI_OV,''),
SI_Ubicacion_Fisica = COALESCE(c.SI_Ubicacion_Fisica,''),
SI_Existencia = COALESCE(t.SI_Existencia, 0),
SI_Cantidad = COALESCE(c.SI_Cantidad, 0),
SI_Cantidad2 = (SELECT COALESCE(c2.SI_Cantidad,0)
FROM SI_Conteo c2
WHERE c2.SI_Num_Conteo = 2 /*AND c2.SI_Num_Articulo = 200002*/
AND t.SI_Ubicacion = c2.SI_Ubicacion),
SI_Dif = COALESCE(c.SI_Cantidad, 0) - COALESCE(t.SI_Existencia, 0),
SI_Dif_Dinero = (COALESCE(c.SI_Cantidad,0) - COALESCE(t.SI_Existencia,0)) * COALESCE(m.SI_Costo_Promedio,0)
FROM SI_Inventario_Teorico_QAD t
LEFT JOIN SI_Conteo c
ON (t.SI_Articulo = c.SI_Num_Articulo
AND c.SI_Num_Conteo = 1
AND t.SI_Ubicacion = c.SI_Ubicacion)
INNER JOIN SI_Maestro_Ref_QAD m
ON (t.SI_Articulo = m.SI_Num_Articulo
OR c.SI_Num_Articulo = m.SI_Num_Articulo)
WHERE c.SI_Num_Articulo = 200002
OR t.SI_Articulo = 200002
Which brings me the next result
img1
My problem is that in the SI_Conteo table I have the same reference added, but it does not exist in the SI_SI_Inventario_Teorico_QAD table so it does not show it to me.
This is the one that does not bring me
img2
According to your problem that the reference in the table "SI_Conteo" and not in the table "SI_Inventario_Teorico_QAD",
you may need to use right join like:
SI_Conteo right join SI_Inventario_Teorico_QAD
or use left like:
SI_Inventario_Teorico_QAD left join SI_Conteo
I have a query according to the :
SELECT
tblDefinition.TopsisType, tblTemplate.tType, tblEvaluationFormCategory.Title AS EvaluationCategory,
CASE tType WHEN 1 THEN N'2'
ELSE N'1' END ProcessType,
tblDefinition.Description, tblDefinition.TemplateID, 1 Destinition
FROM tblTemplate INNER JOIN
tblEvaluationFormCategory ON tblTemplate.CategoryID = tblEvaluationFormCategory.ID INNER JOIN
tblEFQMAlternative INNER JOIN
tblDefinition ON ([tblEFQMAlternative].[TopsisID] = [tblDefinition].[TopsisID] AND [tblEFQMAlternative].[CustomerID] = [tblDefinition].[CustomerID]) INNER JOIN
tblGroupMembers ON ([tblDefinition].[TopsisID] = [tblGroupMembers].[TopsisID]
AND tblDefinition.CustomerID =
tblGroupMembers.CustomerID) ON tblTemplate.TemplateID = tblDefinition.TemplateID
INNER JOIN
tblTerm ON tblDefinition.TermGrant = tblTerm.Title
WHERE (tblTerm.ID = 20) AND (tblGroupMembers.UserID = 558)
GROUP BY tblDefinition.TopsisType, tblTemplate.tType, tblEvaluationFormCategory.Title,
tblDefinition.Description, tblDefinition.TemplateID
when i convert to linq, then error to :
Field [([tblDefinition].[TopsisID] = [tblGroupMembers]].[[TopsisID] AND tblDefinition.CustomerID = tblGroupMembers.CustomerID) ON tblTemplate.TemplateID] not found in the current Data Context.
What should I do?
I have to change a query, and want to add some pre-defined results (outputs) to the query when two values are found on two different columns.
Basically I want to add values to a new column on a query. I tried to use CASE but no luck until now.
SELECT DISTINCT
Q1.SCHD_ID,
Q1.SCHD_DESC,
Q1.TIMEZONE_ID,
Q1.DISPLAY_IN_SCHD_TZ,
Q1.LOGO_ID,
Q1.CODICE_SKOFF,
Q1.CODICE_MODULO,
case
when (Q1.PIANO_CODICE = 'FND001' AND Q1.CODICE_SKOFF = '2346') THEN 'testA'
WHEN (Q1.PIANO_CODICE = 'FND001' AND Q1.CODICE_SKOFF = '2347') THEN 'testB'
WHEN (Q1.PIANO_CODICE = 'FND001' AND Q1.CODICE_SKOFF = '3287') THEN 'testC'
WHEN (Q1.PIANO_CODICE = 'FND001' AND Q1.CODICE_SKOFF = '0000') THEN 'testD'
WHEN (Q1.PIANO_CODICE = 'TEST' AND Q1.CODICE_SKOFF = '4435') THEN 'testE'
ELSE 'NULL'
END as TITOLO_MODULO_SO
FROM
(SELECT
sc.SCHD_ID,
sc.SCHD_DESC,
sc.TIMEZONE_ID,
sc.DISPLAY_IN_SCHD_TZ,
SUBSTR(su1.USER_VALUE, 1 ,3) as LOGO_ID,
su1r.USER_DESC as PIANO_FINANZIAMENTO,
su1r.USER_ID as PIANO_CODICE,
cu1.USER_VALUE as TITOLO_MODULO,
cu2.USER_VALUE as CODICE_MODULO,
c.CPNT_TITLE as ARGOMENTO,
sr.START_DTE,
sr.END_DTE,
cu3.USER_VALUE as DURATA_CORSO,
su4.USER_VALUE as CODICE_SKOFF
FROM PA_SCHED sc
-- Campo "Piano principale di finanziamento"
left outer join PA_SCHED_USER su1 on sc.SCHD_ID = su1.SCHD_ID and su1.COL_NUM = 50
left outer join PA_USRRF_SCHED su1r on su1r.COL_NUM = su1.COL_NUM and su1.USER_VALUE = su1r.USER_ID
-- Informazioni ITEM
left outer join PA_CPNT c on c.CPNT_TYP_ID = sc.CPNT_TYP_ID and c.CPNT_ID = sc.ACT_CPNT_ID and c.REV_DTE = sc.REV_DTE
-- ITEM -> Titolo per finanziamenti
left outer join PA_CPNT_USER cu1 on cu1.CPNT_TYP_ID = c.CPNT_TYP_ID and cu1.CPNT_ID = c.CPNT_ID and cu1.REV_DTE = c.REV_DTE and cu1.COL_NUM = 10
-- ITEM -> Codice Finanziamento
left outer join PA_CPNT_USER cu2 on cu2.CPNT_TYP_ID = c.CPNT_TYP_ID and cu2.CPNT_ID = c.CPNT_ID and cu2.REV_DTE = c.REV_DTE and cu2.COL_NUM = 100
-- ITEM -> Ore finanziamento
left outer join PA_CPNT_USER cu3 on cu3.CPNT_TYP_ID = c.CPNT_TYP_ID and cu3.CPNT_ID = c.CPNT_ID and cu3.REV_DTE = c.REV_DTE and cu3.COL_NUM = 90
-- Dati della Scheduled Offering
left outer join PS_SCHD_RESOURCES sr on sr.SCHD_ID = sc.SCHD_ID
-- Facility
left outer join PA_FACILITY f on f.FACILITY_ID = sc.FACILITY_ID
-- Location
left outer join PA_LOCN l on l.LOCN_ID = sr.LOCN_ID
-- Campo "Banca capofila"
left outer join PA_SCHED_USER su3 on sc.SCHD_ID = su3.SCHD_ID and su3.COL_NUM = 10
left outer join PA_USRRF_SCHED su3ref on su3ref.COL_NUM = su3.COL_NUM and su3ref.USER_ID = su3.USER_VALUE
-- Campo "Multisocietario"
left outer join PA_SCHED_USER su2 on sc.SCHD_ID = su2.SCHD_ID and su2.COL_NUM = 30
-- Campo "Codice Finanz. SKOFF"
left outer join PA_SCHED_USER su4 on sc.SCHD_ID = su4.SCHD_ID and su4.COL_NUM = 99
) q1
Which is the best way to do it?
The dbms IS birt
I temp table on a query will work?
Thanks
Trying to update the fact table with late coming dimension data. See Code below
UPDATE FactVehicleStock
SET
FactVehicleStock.[TOL_BidDateTime] = B.Bid_Date_and_Time,
FactVehicleStock.[TOL_AuctionDate] = B.Date_opened_for_tender,
FactVehicleStock.[TOL_OriginalLoadDate] = B.Original_Load_date,
FactVehicleStock.[TOL_ServiceHistory] = B.Service_History,
FactVehicleStock.[TOL_ReservedPrice] = B.Reserve_price,
FactVehicleStock.[TOL_BidPrice] = B.Bid_Price,
FactVehicleStock.[TOL_OriginalReservedPrice] = B.Original_Reserve_Price,
FactVehicleStock.[TOL_NoOfTimesReloaded] = B.Number_of_times_reloaded
FROM BMR_DWH.dbo.FactVehicleStock as A
INNER JOIN BMR_STAGE.dbo.STG_Traders_Online as B
ON A.StockbookNumber = B.Stock_Number
INNER JOIN BMR_DWH.[dbo].[DimDealership] as C
ON A.DEALERSHIP_KEY IN (SELECT Distinct [DEALERSHIP_KEY]
FROM BMR_DWH.[dbo].[DimDealership]
INNER JOIN [BMR_STAGE].[dbo].[STG_Traders_Online] E
ON LTRIM(RTRIM(C.MOLNUMBER)) = LTRIM(RTRIM(E.MOL_Number))
)
Try to use the right UPDATE SELECT Syntaxe.
If you look at the code I did, I had to change a bit your query to do the joins. See if it fits to you.
UPDATE BMR_DWH.dbo.FactVehicleStock as a
INNER JOIN BMR_STAGE.dbo.STG_Traders_Online as B
ON A.StockbookNumber = B.Stock_Number
INNER JOIN
(SELECT Distinct [DEALERSHIP_KEY]
FROM BMR_DWH.[dbo].[DimDealership] as C
INNER JOIN [BMR_STAGE].[dbo].[STG_Traders_Online] E
ON LTRIM(RTRIM(C.MOLNUMBER)) = LTRIM(RTRIM(E.MOL_Number)) ) D
ON A.DEALERSHIP_KEY = D.DEALERSHIP_KEY
SET
FactVehicleStock.[TOL_BidDateTime] = B.Bid_Date_and_Time,
FactVehicleStock.[TOL_AuctionDate] = B.Date_opened_for_tender,
FactVehicleStock.[TOL_OriginalLoadDate] = B.Original_Load_date,
FactVehicleStock.[TOL_ServiceHistory] = B.Service_History,
FactVehicleStock.[TOL_ReservedPrice] = B.Reserve_price,
FactVehicleStock.[TOL_BidPrice] = B.Bid_Price,
FactVehicleStock.[TOL_OriginalReservedPrice] = B.Original_Reserve_Price,
FactVehicleStock.[TOL_NoOfTimesReloaded] = B.Number_of_times_reloaded