How to write Update with join in where clause - sql

I am trying to run this SQL to update a table. The issue is that I need a join in order to update the correct record. This is what I have come up with so far:
Update UserProfile
set UserProfile.PropertyValue = 'Test'
join ProfilePropertyDefinition
on ProfilePropertyDefinition.Id = UserProfile.PropertyDefinitionId
where UserProfile.UserId = 11
and ProfilePropertyDefinition.PortalID = 0
and ProfilePropertyDefinition.PropertyName = 'Address1'
and ProfilePropertyDefinition.PropertyCategory = 'Address'
This is the message I get:
Incorrect syntax near the keyword 'join'.

You are almost there, you forgot the from clause:
Update UserProfile
set UserProfile.PropertyValue = 'Test'
from UserProfile
join ProfilePropertyDefinition
on ProfilePropertyDefinition.Id = UserProfile.PropertyDefinitionId
where UserProfile.UserId = 11
and ProfilePropertyDefinition.PortalID = 0
and ProfilePropertyDefinition.PropertyName = 'Address1'
and ProfilePropertyDefinition.PropertyCategory = 'Address'

Update UserProfile
set UserProfile.PropertyValue = 'Test'
from UserProfile
join ProfilePropertyDefinition
on ProfilePropertyDefinition.Id = UserProfile.PropertyDefinitionId
where UserProfile.UserId = 11
and ProfilePropertyDefinition.PortalID = 0
and ProfilePropertyDefinition.PropertyName = 'Address1'
and ProfilePropertyDefinition.PropertyCategory = 'Address'
You have to repeat the table to update in the from clause - even iof this syntax looks a little strange.

You are missing a FROM clause:
Update a
set PropertyValue = 'Test'
FROM UserProfile as a
inner join ProfilePropertyDefinition as b
on b.Id = a.PropertyDefinitionId
where a.UserId = 11
and b.PortalID = 0
and b.PropertyName = 'Address1'
and b.PropertyCategory = 'Address';

Related

SQL query with multiple CTE but getting "ERROR: relation "t" does not exist"

I'm trying to run a SQL query with multiple CTE but getting "ERROR: relation "t" does not exist"
Here is the code. What am I doing wrong?
WITH tmp AS (
Select a.tx_id, a.txo_index, a.address,b.tx_in_id from txo as a
inner join txin as b ON (a.tx_id = b.tx_out_id and a.txo_index= b.tx_out_index)
where a.tx_in_id = 0
),
tmp2 AS
(
Select tmp.tx_id,tmp.txo_index,tmp.address,tmp.tx_in_id,b.value as value_next,c.epoch_no as consumed_epoch,c.date as consumed_date from tmp
inner join txo as b ON
tmp.tx_in_id = b.tx_id and
tmp.address = b.address
inner join tx as c ON
tmp.tx_in_id = c.id
)
Update t
SET
t.tx_in_id = tmp2.tx_in_id,
t.value_next = tmp2.value_next,
t.consumed_epoch = tmp2.epoch_no,
t.consumed_date = tmp2.date
FROM txo as t
inner join tmp2 ON
t.tx_id = tmp2.tx_id and t.txo_index = tmp2.txo_index and t.address = tmp2.address
where t.tx_in_id =0
I believe referring to the table as an alias in the UPDATE and SET clause isn't allowed in postgres. Instead:
WITH tmp AS (
Select a.tx_id, a.txo_index, a.address,b.tx_in_id from txo as a
inner join txin as b ON (a.tx_id = b.tx_out_id and a.txo_index= b.tx_out_index)
where a.tx_in_id = 0
),
tmp2 AS
(
Select tmp.tx_id,tmp.txo_index,tmp.address,tmp.tx_in_id,b.value as value_next,c.epoch_no as consumed_epoch,c.date as consumed_date from tmp
inner join txo as b ON
tmp.tx_in_id = b.tx_id and
tmp.address = b.address
inner join tx as c ON
tmp.tx_in_id = c.id
)
Update txo
SET
tx_in_id = tmp2.tx_in_id,
value_next = tmp2.value_next,
consumed_epoch = tmp2.epoch_no,
consumed_date = tmp2.date
FROM tmp2
WHERE txo.tx_id = tmp2.tx_id
and txo.txo_index = tmp2.txo_index
and txo.address = tmp2.address
and txo.tx_in_id =0

'Order By' with 'Union' and 'GroupBy' (Oracle)

Good Morning. I have a query that has Group By and Union tables. The problem is when I try to order the final result. I'm always getting an error:
SELECT {Account}.[Id], {Account}.[AccountName], {Account}.[Project], {Account}.[Initiative], {Account}.[AccountName], {Application}.[Id], {Application}.[ApplicationName]
FROM {Account}
INNER JOIN {ApprovalWorkUnitDetail} ON {ApprovalWorkUnitDetail}.[AccountID] = {Account}.[Id]
INNER JOIN {Issue} ON {Issue}.[Id] = {ApprovalWorkUnitDetail}.[IssueID]
INNER JOIN {ServiceRequest} ON {ServiceRequest}.[Id] = {Issue}.[ServiceRequestId]
INNER JOIN {ServiceRequestProduct} ON {ServiceRequestProduct}.[ServiceRequestId] = {ServiceRequest}.[Id]
INNER JOIN {Product} ON {Product}.[Id] = {ServiceRequestProduct}.[ProductId]
LEFT JOIN {Application} ON {Application}.[Id] = {Account}.[ApplicationID]
WHERE ({ApprovalWorkUnitDetail}.[ContractID] = #ContractID)
AND ({ServiceRequest}.[Id] = #ServiceRequestID OR #ServiceRequestID = #NullIdentifier)
AND ({Product}.[Id] = #ProductID OR #ProductID = #NullIdentifier)
AND ({ApprovalWorkUnitDetail}.[MonthIdentifier] = #MonthIdentifier)
AND ({ApprovalWorkUnitDetail}.[YearIdentifier] = #YearIdentifier)
AND ({Issue}.[PaymentStatusID] = #PaymentStatusApprovedID)
UNION
SELECT {Account}.[Id], {Account}.[AccountName], {Account}.[Project], {Account}.[Initiative], {Account}.[AccountName], {Application}.[Id], {Application}.[ApplicationName]
FROM {Account}
INNER JOIN {ApprovalRefinementDetail} ON {ApprovalRefinementDetail}.[AccountID] = {Account}.[Id]
INNER JOIN {Worklog} ON {Worklog}.[Id] = {ApprovalRefinementDetail}.[WorklogID]
INNER JOIN {Issue} ON {Worklog}.[IssueId] = {Issue}.[Id]
INNER JOIN {ServiceRequest} ON {ServiceRequest}.[Id] = {Issue}.[ServiceRequestId]
INNER JOIN {ServiceRequestProduct} ON {ServiceRequestProduct}.[ServiceRequestId] = {ServiceRequest}.[Id]
INNER JOIN {Product} ON {Product}.[Id] = {ServiceRequestProduct}.[ProductId]
LEFT JOIN {Application} ON {Application}.[Id] = {Account}.[ApplicationID]
WHERE ({ApprovalRefinementDetail}.[ContractID] = #ContractID)
AND ({Product}.[Id] = #ProductID OR #ProductID = #NullIdentifier)
AND ({ServiceRequest}.[Id] = #ServiceRequestID OR #ServiceRequestID = #NullIdentifier)
AND ({ApprovalRefinementDetail}.[MonthIdentifier] = #MonthIdentifier)
AND ({ApprovalRefinementDetail}.[YearIdentifier] = #YearIdentifier)
AND ({Worklog}.[PaymentStatusID] = #PaymentStatusApprovedID)
GROUP BY {Account}.[Id], {Account}.[AccountName], {Account}.[Project], {Account}.[Initiative], {Account}.[AccountName], {Application}.[Id], {Application}.[ApplicationName]
ORDER BY {Account}.[AccountName]
I don't know why I am always getting error ORA-00904 and it indicates that AccountName is an invalid identifier.
I am using ORACLE database.
Many thanks
I would recommend moving your union query to a subquery, and sorting in the outer scope:
select *
from (
-- your big query
) t
order by accountname

SQL Query - How to suppress repeating values in the result set?

I'm trying to suppress the repeating values in TotalCarton column. Have tried to replace the value either blank or null but went failed. Any help?
Here is the SQL Script:
SELECT ORDERS.StorerKey,
ORDERS.OrderKey,
PackKey = (SELECT MAX(PackKey) FROM BAX_PACK_DTL WITH (NOLOCK) WHERE ORderKey = ORDERS.OrderKey),
PackHU = BAX_PACK_DTL.OuterPackID,
SalesOrderNum = ( SELECT Upper(Max(ORDERDETAIL.CustShipInst01)) FROM ORDERDETAIL WITH (NOLOCK) WHERE OrderKey = ORDERS.OrderKey),
DeliveryNum = Upper(ORDERS.ExternOrderKey),
TotalCarton = ( CASE BAX_PACK_DTL.PackType WHEN 'C' THEN Count(DISTINCT(BAX_PACK_DTL.OuterPackID))
ELSE 0 END ),
TotalPallet = ( CASE BAX_PACK_DTL.PackType WHEN 'P' THEN Count(DISTINCT(BAX_PACK_DTL.OuterPackID))
ELSE 0 END ),
SumCarton = (SELECT COUNT(DISTINCT(OuterPackSeq)) FROM BAX_PACK_DTL WITH (NOLOCK) WHERE PackType = 'C' AND PackKey = '0000000211'),
SumPallet = (SELECT COUNT(DISTINCT(OuterPackSeq)) FROM BAX_PACK_DTL WITH (NOLOCK) WHERE PackType = 'P' AND PackKey = '0000000211'),
AddWho = Upper(ORDERS.EditWho),
ORDERS.AddDate
FROM ORDERS WITH (NOLOCK) INNER JOIN ORDERDETAIL WITH (NOLOCK) ON ORDERS.StorerKey = ORDERDETAIL.StorerKey
AND ORDERS.OrderKey = ORDERDETAIL.OrderKey
INNER JOIN PICKDETAIL WITH (NOLOCK) ON ORDERDETAIL.StorerKey = PICKDETAIL.StorerKey
AND ORDERDETAIL.OrderKey = PICKDETAIL.OrderKey
AND ORDERDETAIL.OrderLineNumber = PICKDETAIL.OrderLineNumber
INNER JOIN BAX_PACK_DTL WITH (NOLOCK) ON PICKDETAIL.OrderKey = BAX_PACK_DTL.OrderKey
AND PICKDETAIL.PickDetailKey = BAX_PACK_DTL.PickDetailKey
WHERE (SELECT COUNT(DISTINCT(ORDERKEY)) FROM PICKDETAIL WITH (NOLOCK) WHERE OrderKey = ORDERS.OrderKey ) > 0
AND BAX_PACK_DTL.PackKey = '0000000211'
AND BAX_PACK_DTL.OuterPackID IN
('P111111111',
'P22222222',
'P33333333')
GROUP BY ORDERS.StorerKey,
ORDERS.OrderKey,
ORDERS.ExternOrderKey,
ORDERS.HAWB,
ORDERS.SO,
ORDERS.EditWho,
ORDERS.AddDate,
PICKDETAIL.WaveKey,
BAX_PACK_DTL.OuterPackID,
BAX_PACK_DTL.PackKey,
BAX_PACK_DTL.PackType
ORDER BY BAX_PACK_DTL.OuterPackID ASC
Below is the current result set based on the query above.
Your code looks really strange. I would expect the query to use conditional aggregation and look more like this:
SELECT ORDERS.StorerKey, ORDERS.OrderKey,
PackKey = (SELECT MAX(PackKey) FROM BAX_PACK_DTL WITH (NOLOCK) WHERE ORderKey = ORDERS.OrderKey),
PackHU = BAX_PACK_DTL.OuterPackID,
SalesOrderNum = ( SELECT Upper(Max(ORDERDETAIL.CustShipInst01)) FROM ORDERDETAIL WITH (NOLOCK) WHERE OrderKey = ORDERS.OrderKey),
DeliveryNum = Upper(ORDERS.ExternOrderKey),
TotalCarton = COUNT(DISTINCT CASE BAX_PACK_DTL.PackType WHEN 'C' THEN BAX_PACK_DTL.OuterPackID END),
TotalPallet = COUNT(DISTINCT CASE BAX_PACK_DTL.PackType WHEN 'P' THEN BAX_PACK_DTL.OuterPackID END),
SumCarton = (SELECT COUNT(DISTINCT(OuterPackSeq)) FROM BAX_PACK_DTL bpd WHERE pbd.PackType = 'C' AND pbd.PackKey = '0000000211'),
SumPallet = (SELECT COUNT(DISTINCT(OuterPackSeq)) FROM BAX_PACK_DTL bpd WHERE pbd.PackType = 'P' AND pbd.PackKey = '0000000211'),
AddWho = Upper(ORDERS.EditWho),
ORDERS.AddDate
FROM . . .
GROUP BY ORDERS.StorerKey, ORDERS.OrderKey, Upper(ORDERS.ExternOrderKey),
Upper(ORDERS.EditWho), ORDERS.AddDate;
This may not be exact. You have not qualified column names, given the table structure, and are using very arcane query syntax, mixing subqueries and aggregations. But it should give an idea.

How to select multiple roles from sql database?

Is it possible to rewrite this SQL code?
In this case the user is assigned to multiple roles. I want all Arrangemang ID's for the user's different roles in one list.
I want to refactor the hardcoded #rollx in these lines...
(kk_aj_tbl_Arrangemang.KonstformID = #roll1) OR
If it's possible?
The following is my existing code...
SELECT kk_aj_tbl_Arrangemang.ArrID,
kk_aj_tbl_Arrangemang.Datum,
(SELECT TOP (1) kk_aj_tbl_content.Rubrik
FROM kk_aj_tbl_content
INNER JOIN kk_aj_tbl_arridtoContent ON kk_aj_tbl_content.Contentid = kk_aj_tbl_arridtoContent.contentid
WHERE (kk_aj_tbl_arridtoContent.arrid = kk_aj_tbl_Arrangemang.ArrID AND
kk_aj_tbl_arridtoContent.Version = 1
)
) AS Rubrik,
(SELECT TOP (1) kk_aj_tbl_content_1.Underrubrik
FROM kk_aj_tbl_content AS kk_aj_tbl_content_1
INNER JOIN kk_aj_tbl_arridtoContent AS kk_aj_tbl_arridtoContent_1 ON kk_aj_tbl_content_1.Contentid = kk_aj_tbl_arridtoContent_1.contentid
WHERE (kk_aj_tbl_arridtoContent_1.arrid = k_aj_tbl_Arrangemang.ArrID AND
kk_aj_tbl_arridtoContent_1.Version = 1
)
) AS UnderRubrik,
kk_aj_tbl_Arrangemang.Publicerad,
kk_aj_tbl_Arrangemang.LookedAt,
kk_aj_tbl_ArrangemangStatus.ArrangemangStatus,
kk_aj_tbl_Arrangemangtyp.arrangemangtyp,
kk_aj_tbl_Konstformtyp.konstform,
kk_aj_tbl_utovare.Organisation,
Users.Username
FROM kk_aj_tbl_Arrangemang
INNER JOIN kk_aj_tbl_utovare ON kk_aj_tbl_Arrangemang.UtovarID = kk_aj_tbl_utovare.UtovarID
INNER JOIN kk_aj_tbl_ArrangemangStatus ON kk_aj_tbl_Arrangemang.ArrangemangStatusID = kk_aj_tbl_ArrangemangStatus.ArrangemangStatusID
INNER JOIN kk_aj_tbl_Konstformtyp ON kk_aj_tbl_Arrangemang.KonstformID = kk_aj_tbl_Konstformtyp.KonstformID
INNER JOIN kk_aj_tbl_Arrangemangtyp ON kk_aj_tbl_Arrangemang.ArrangemangstypID = kk_aj_tbl_Arrangemangtyp.ArrangemangstypID
INNER JOIN Users ON kk_aj_tbl_Arrangemang.AdminuserID = Users.UserID
WHERE (kk_aj_tbl_ArrangemangStatus.ArrangemangStatusID = #arrStatusTyp)
AND (kk_aj_tbl_Arrangemang.VisningsPeriod = #visningsperiod)
AND ((kk_aj_tbl_Arrangemang.KonstformID = #roll1) OR
(kk_aj_tbl_Arrangemang.KonstformID = #roll2) OR
(kk_aj_tbl_Arrangemang.KonstformID = #roll3) OR
(kk_aj_tbl_Arrangemang.KonstformID = #roll4) OR
(kk_aj_tbl_Arrangemang.KonstformID = #roll5) OR
(kk_aj_tbl_Arrangemang.KonstformID = #roll6) OR
(kk_aj_tbl_Arrangemang.KonstformID = #roll7) OR
(kk_aj_tbl_Arrangemang.KonstformID = #roll8) OR
(kk_aj_tbl_Arrangemang.KonstformID = #roll9) OR
(kk_aj_tbl_Arrangemang.KonstformID = #roll10)
)
I think you might be looking for the IN operator:
...AND kk_aj_tbl_Arrangemang.KonstformID IN (#roll1, #roll2, ...#rolln)

SQL Server Update Statement

Here is my stored procedure for updating tables in SQL Server, but I can not seem to get it working what is wrong with the statement.
Update pc.PatientCopayId, pc.amount, pc.patientid, pc.apptid ,p.PaymentId,p.PaymentDate,p.PayorType,p.PaymentMethod,
p.RefNumber,p.PaymentAmount,p.PayorId,pt.LastFirstName As PatientName,
ISNULL((SELECT note FROM dbo.PatientNote WHERE NoteTypeId = 28 AND KeyValue = pc.PatientCopayId),'') AS Note
from [dbo].[PatientCopay] pc, dbo.pymt_Payment p, dbo.Patient pt
where ApptId = #ApptId
and p.PaymentId = pc.Paymentid
And pt.PatientId = p.PayorId
Values and meaning
pc.amount, = #PaymentAmount
pc.patientid, = #PatientId
pc.apptid , = #ApptId
p.PaymentId, = #PaymentId
p.PaymentDate, = #PaymentDate
p.PayorType, = #PayorType
p.PaymentMethod, = #PaymentMethod
p.RefNumber, = #RefNumber
p.PaymentAmount, = #PaymentAmount
p.PayorId, = #PayorId
The UPDATE statement shoudl have the following form
UPDATE TableName SET....
Your update statement doesn't have the tablename or the SET keyword.
More Info HERE