Update Query Access 2007 Group into one row - sql

I'm playing around with access and vba. I'm struggleing to update and to group rows from tblLoadingListItems into tblLoadSummary using the query below
UPDATE tblLoadSummary INNER JOIN tblLoadingListItems ON (tblLoadSummary.salesno = tblLoadingListItems.salesno) AND (tblLoadSummary.loadnolink = tblLoadingListItems.loadnolink) SET tblLoadSummary.[Cust O/N] = [tblLoadingListItems].[Cust O/N], tblLoadSummary.traderid = [tblLoadingListItems].[traderid], tblLoadSummary.street = [tblLoadingListItems].[street], tblLoadSummary.[zone] = [tblLoadingListItems].[zone], tblLoadSummary.salesno = [tblLoadingListItems].[salesno], tblLoadSummary.[Customer Ref] = [tblLoadingListItems].[Customer Ref], tblLoadSummary.DeliveryAddress = [tblLoadingListItems].[DeliveryAddress], tblLoadSummary.Postcode = [tblLoadingListItems].[Postcode], tblLoadSummary.SumOfnoofpacks = Sum([tblLoadingListItems].[packs1]), tblLoadSummary.SumOfnoofboxes = Sum([tblLoadingListItems].[noofboxes]), tblLoadSummary.contact = [tblLoadingListItems].[contact], tblLoadSummary.telephone = [tblLoadingListItems].[telephone], tblLoadSummary.fax = [tblLoadingListItems].[fax], tblLoadSummary.email =
[tblLoadingListItems].[email], tblLoadSummary.deliverycontact = [tblLoadingListItems].[deliverycontact], tblLoadSummary.deliverytelephone = [tblLoadingListItems].[deliverytelephone], tblLoadSummary.deliveryfax = [tblLoadingListItems].[deliveryfax], tblLoadSummary.deliveryemail = [tblLoadingListItems].[deliveryemail], tblLoadSummary.acknowaddress = [tblLoadingListItems].[acknowaddress]
WHERE (((tblLoadSummary.salesno)="SM-100118") AND ((tblLoadSummary.loadnolink)=32232))
If i show datasheet view i get two rows returned, Im trying to group these rows into one.
When i try to add a GROUP BY after the WHERE i get syntax errors missing operators for all my fields.
Can you someone point me in the right direction?

I am not sure with access but starting update like this and maybe its wrong :
Update tblLoadSummary inner join...
try it like this
update t set .... from tblLoadSummary t inner join tblLoadSummary s on ...
this is the full query
UPDATE t-- tblLoadSummary
SET t.CustO/N = s.CustO/N,
t.traderid = s.traderid,
t.street = s.street,
t.zone = s.zone,
t.salesno = s.salesno,
t.CustomerRef = s.Customer Ref,
t.DeliveryAddress = s.DeliveryAddress,
t.Postcode = s.Postcode,
t.SumOfnoofpacks = Sum(s.packs1),
t.SumOfnoofboxes = Sum(s.noofboxes),
t.contact = s.contact,
t.telephone = s.telephone,
t.fax = s.fax,
t.email = s.email,
t.deliverycontact = s.deliverycontact,
t.deliverytelephone = s.deliverytelephone,
t.deliveryfax = s.deliveryfax,
t.deliveryemail = s.deliveryemail,
t.acknowaddress = s.acknowaddress
from tblLoadSummary as t
INNER JOIN tblLoadingListItems as s ON
(t.salesno = s.salesno) AND (t.loadnolink = s.loadnolink)
WHERE (((t.salesno) = "SM-100118") AND
((t.loadnolink) = 32232))

Related

Update a table from another table

I have got 2 tables "animal_breeds" and "ztmp.ztmp_509810_anims_out". In "animals breed" every animal has key and breed name and percentage. Few animals might have 2 different breeds with different percentage. Now based on the animals key in "animals_breeds" i want to update "ztmp.ztmp_509810_anims_out"
i am using this code which i know is wrong
update ztmp.ztmp_509810_anims_out
set
alt_id1 = ab.breed
,alt_id2 = pcnt
,alt_id3 = ab.breed
,alt_id4 = pcnt
,alt_id5 = ab.breed
,alt_id6 = pcnt
,alt_id7 = ab.breed
,alt_id8 = pcnt
from animal_breeds ab
where ab.soc_code = ztmp_509810_anims_out.soc_code and ab.animals_key = ztmp_509810_anims_out.animals_key
and ab.soc_code = 'AUNDB';
could i use a for loop inside an update statement?
UPDATE ztmp.ztmp_509810_anims_out AS z
SET soc_code = q.soc_code,
animals_key = q.animals_key,
alt_id1 = breeds[1],
alt_id2 = pcnts[1],
alt_id3 = breeds[2],
alt_id4 = pcnts[2]
FROM (SELECT soc_code, animals_key,
array_agg(breed) breeds, array_agg(pcnt) pcnts
FROM animal_breeds
GROUP BY soc_code, animals_key
) q
WHERE z.soc_code = q.soc_code
AND z.animals_key = q.animals_key;
If there can be more than 2 breeds per animals_key, add breeds[3] and pcnts[3] and so on.

Catching last datetime after update wont result

I have the following code:
UPDATE sm
SET sm_fecha_venc = (SELECT MIN(nd_fecha_venc) FROM [db_facturacion].[dbo].[tb_notas_debito]
WHERE sm_codigo = nd_num_servicio
-- AND nd_referencia = sm_cod_producto
AND nd_num_factura = sm_cod_factura
AND nd_estado = 'G')
,sm_fecha_ult_pago = (SELECT MAX(nd_fecha_pago) FROM [db_facturacion].[dbo].[tb_notas_debito]
WHERE sm_codigo = nd_num_servicio
-- AND nd_referencia = sm_cod_producto
AND nd_num_factura = sm_cod_factura
AND nd_estado = 'C')
,sm_fecha = GETDATE()
,sm_cod_factura_ren = #i_num_factura
OUTPUT DELETED.sm_fecha_venc AS FECHA_VENC_OLD,
DELETED.sm_fecha_ult_pago AS FECHA_ULT_PAGO_OLD,
DELETED.sm_fecha AS FECHA_OLD,
DELETED.sm_cod_factura_ren AS COD_FACTURA_REN_OLD
INTO #TABLATABLA --
FROM [db_facturacion].[dbo].[tb_servicios] sm --WITH(NOLOCK)
INNER JOIN #servicios AS T ON sm_codigo = num_servicio
WHERE sm_tipo_bien_protegido = 1
AND [sm_estado] = 1
--AND sm.sm_cod_forma_contrato = 1
AND sm.sm_tipo_inventario = tipo_inventario
/*===========================
INSERTED DELETED
=============================*/
UPDATE db_facturacion.[dbo].[tb_log_cambio_servicio]
SET cs_fecha_venc_old = FECHA_VENC_OLD
,cs_fecha_ult_pago_old = FECHA_ULT_PAGO_OLD
,cs_fecha_old = FECHA_OLD
,cs_cod_factura_ren_old = COD_FACTURA_REN_OLD
FROM #TABLATABLA
WHERE cs_codigo = #ID_ULTIMO_ING (LAST ##IDENTITY)
Im trying to save the last dates into a log table, just in case I have to return those dates back.
The code works good, just that it's catching the actual date and inserted in the log table.
What am I missing?

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