update not working - sql

UPDATE dwh.product_in_offer AS t
SET (t.PRODUCT_BUCKET_TYPE_CODE,t.PRODUCT_BUCKET_TYPE_DESC,t.LAST_EDIT_TYPE,t.CREATE_ID,t.UPDATE_ID) =
(SELECT s.PRODUCT_BUCKET_TYPE_CODE, s.PRODUCT_BUCKET_TYPE_DESC, s.LAST_EDIT_TYPE, s.CREATE_ID, s.UPDATE_ID
FROM dwh.product_in_offer_vw AS s
WHERE create_id = 0
AND t.PRODUCT_KEY = s.PRODUCT_KEY
AND t.OFFER_KEY=s.OFFER_KEY)
INNER JOIN dwh.product_in_offer_vw p on t.PRODUCT_KEY=p.PRODUCT_KEY and t.OFFER_KEY=p.OFFER_KEY
WHERE create_id = 0;
ERROR:Syntax error near INNER, offset 386 "..ND t.OFFER_KEY=s.OFFER_KEY) -->INNER<--"
Any idea ??

The INNER JOIN clause has to go before the SET clause.
UPDATE dwh.product_in_offer AS t
INNER JOIN dwh.product_in_offer_vw p on t.PRODUCT_KEY=p.PRODUCT_KEY and t.OFFER_KEY=p.OFFER_KEY
SET (t.PRODUCT_BUCKET_TYPE_CODE,t.PRODUCT_BUCKET_TYPE_DESC,t.LAST_EDIT_TYPE,t.CREATE_ID,t.UPDATE_ID) =
(SELECT s.PRODUCT_BUCKET_TYPE_CODE, s.PRODUCT_BUCKET_TYPE_DESC, s.LAST_EDIT_TYPE, s.CREATE_ID, s.UPDATE_ID
FROM dwh.product_in_offer_vw AS s
WHERE create_id = 0
AND t.PRODUCT_KEY = s.PRODUCT_KEY
AND t.OFFER_KEY=s.OFFER_KEY)
WHERE create_id = 0;

Related

How to avoid union all

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

Convert to linq sql query error

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?

Adding a Multiplies joins

I have a problema with this proc.
if #p_func = '1'
begin
SELECT
isNull(ts.num_reserva,0) as num_reserva
,ts.des_dia
,ts.des_mes
,ts.des_ano
,ts.num_sala
,th.des_hora_inil
,th.des_hora_finl
,ts.des_nome
,ts.des_rama
--,ss.des_sala
,ts.num_cent_cust
,ts.des_tipo
,ts.num_pess
,ts.des_text
,ts.des_conf
,ts.dat_manu
,ts.des_logi_manu
,tu.nom_comp
,edit = '<img src="images/bt_edit.gif" style=cursor:hand border=0 onclick=manu("pagi_agen_sala_edit.aspx?func=U&des_dia='+ltrim(rtrim(#p_des_dia))+'&des_mes='+ltrim(rtrim(#p_des_mes))+'&des_ano='+ltrim(rtrim(#p_des_ano))+'&num_sala='+ltrim(rtrim(convert(varchar(10),#p_num_sala)))+'&des_hora_inil='+ltrim(rtrim(th.des_hora_inil))+'&des_hora_finl='+ltrim(rtrim(th.des_hora_finl))+'")>'
,dele = '<img src="images/bt_excl.gif" style=cursor:hand border=0 onclick=manu("pagi_agen_sala_edit.aspx?func=D&des_dia='+ltrim(rtrim(#p_des_dia))+'&des_mes='+ltrim(rtrim(#p_des_mes))+'&des_ano='+ltrim(rtrim(#p_des_ano))+'&num_sala='+ltrim(rtrim(convert(varchar(10),#p_num_sala)))+'&des_hora_inil='+ltrim(rtrim(th.des_hora_inil))+'&des_hora_finl='+ltrim(rtrim(th.des_hora_finl))+'")>'
FROM tbl_agen_sala_hora th
LEFT JOIN tbl_agen_sala ts
ON th.des_hora_inil = ts.des_hora_inil
AND th.des_hora_finl = ts.des_hora_finl
AND ts.des_dia = #p_des_dia
AND ts.des_mes = #p_des_mes
AND ts.des_ano = #p_des_ano
AND ((ts.num_sala = #p_num_sala) OR (#p_num_sala = 0))
AND ts.num_sala = #p_num_sala
LEFT JOIN TBL_INTR_USUA tu
on tu.des_logi=ts.des_logi_manu
WHERE th.des_hora_inil >= #p_des_hora_inil
AND th.des_hora_inil < #p_des_hora_finl
AND th.des_hora_finl > #p_des_hora_inil
AND th.des_hora_inil < #p_des_hora_finl
ORDER BY th.des_hora_inil, th.des_hora_finl
end
I have to add another inner join in this proc but I have Already tried to put in every where.
This inner joins that a I have to ADD:
INNER JOIN tbl_agen_sala_sala ss
on ts.num_sala = ss.num_sala
Someone can help me
You need to add it as a left join, because the conditions are on the second table in the existing left join. So this FROM clause should work:
FROM tbl_agen_sala_hora thLEFT JOIN
tbl_agen_sala ts
ON th.des_hora_inil = ts.des_hora_inil AND
th.des_hora_finl = ts.des_hora_finl AND
ts.des_dia = #p_des_dia AND
ts.des_mes = #p_des_mes AND
ts.des_ano = #p_des_ano AND
((ts.num_sala = #p_num_sala) OR (#p_num_sala = 0)) AND
ts.num_sala = #p_num_sala LEFT JOIN
TBL_INTR_USUA tu
ON tu.des_logi = ts.des_logi_manu LEFT JOIN
tbl_agen_sala_sala ss
ON ts.num_sala = ss.num_sala

SQL UPDATE using two joined subqueries

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

Query not updating the correct Rows

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