How to apply order by on Update? - sql-server

I am trying to update values in Temporary table but before update i want to order by the records on the basis of date.
UPDATE INS
set ins.PrefferedEmail = IC.CntcInfoTxt
From #Insured INS
Inner Join InsuredContact IC
on IC.InsuredId = INS.Insuredid and IC.ExpDt < Getdate() And (INS.InsuredStatus = 'Expired' or INS.InsuredStatus = 'Merged')
Where IC.CntcTypeCd = 'EML' and IC.InsuredId = #InsuredId and MAX(IC.ExpDt) ExpDt
I want to update on the basis of this column IC.ExpDt
Thanks in advance

I think you are confusing an UPDATE with SELECT(ing) the correct data to update with.
I solved this problem with a common table expression (cte) and the rank() function. The cte is a nice way to get a sub-query results. The rank is needed to find the most recent contact info.
-- 1 - Get id, contact text, expired date, with a rank by expired date
-- 2 - Join with table to update, select rank = 1
;
WITH cteRecentContactInfo
AS
(
SELECT
ic.InsuredId,
ic.CntcInfoTxt,
ic.ExpDt,
RANK() OVER (ORDER BY ic.ExpDt DESC) as RankByDt
FROM
InsuredContact as ic
WHERE
ic.CntcTypeCd = 'EML' and ic.ExpDt < getdate()
)
UPDATE ins
FROM #Insured ins INNER JOIN cteRecentContactInfo rci
ON ins.Insuredid = rci.Insuredid and ins.ExpDt = rci.ExpDt
WHERE
(ins.InsuredStatus = 'Expired' OR ins.InsuredStatus = 'Merged') AND
rci.RankByDt = 1

Update and sorting doesn't work that way. The rows are not necessarily stored in any particular order, so sorting and updating are completely independent.
If you only want to update based on MAX(ExpDt) you need a subquery that pulls that up.
UPDATE INS
SET INS.PrefferedEmail = tempOutside.CntcInfo
FROM (SELECT IC.CntcInfo,IC.InsuredID FROM
InsuredContact IC INNER JOIN
(SELECT MAX(IC.ExpDt) AS MaxExpDt,IC.InsuredID
FROM IC
WHERE IC.ExpDt < Getdate()
AND IC.CntcTypeCd = 'EML'
GROUP BY IC.InsuredID) AS tempInside
ON tempInside.InsuredID = IC.InsuredID
AND IC.ExpDt = tempInside.MaxExpDt) AS tempOutside
INNER JOIN INS ON tempOutside.InsuredID = INS.InsuredId
WHERE (INS.InsuredStatus = 'Expired' OR INS.InsuredStatus = 'Merged')
AND INS.InsuredID = #InsuredID
On unrelated notes, for the good of whoever is doing maintenance on your code you might want to consider fixing the spelling errors (e.g. should be Preferred instead of 'Preffered') and giving the tables more meaningful names. Also since you're only working with one ID from #InsuredID you could simplify the code and remove an inner join or two but what I have should work for updating several records at once not just the one selected by #InsuredID.

Thanks for your time and comments. I have done this and its working fine
UPDATE INS
set ins.PrefferedEmail = ICC.CntcInfoTxt
From #Insured INS
Inner Join
(
SELECT
InsuredId,
CntcInfoTxt,
CntcTypeCd
From InsuredContact ICC
Where ExpDt = (select MAX(ExpDt) from InsuredContact where ExpDt < GETDATE() and CntcTypeCd = 'EML' and InsuredId = 10)
) As ICC
on ICC.InsuredId = INS.InsuredId And (INS.InsuredStatus = 'Expired' or INS.InsuredStatus = 'Merged')
Where ICC.InsuredId = #InsuredId

Related

How to update multiple rows in a temp table with multiple values from another table using only one ID common between them?

I am trying to reconcile the IDs in a temp table (top) from another DB table (bottom). Since I only have one ID that's common between the two, I am only getting the top result for all the rows (ReconGlobalRemunerationGrantID) in my temp table. I am aiming to get each of the unique ID and update my temp table as such.
Right now, my update query is simple and I update using the ID common between the 2 tables. Is there a function or another command statement I can use to get the result intended?
update tgrg set ReconGlobalRemunerationGrantID = grg.GlobalRemunerationGrantID from #GlobalRemunerationGrant tgrg
join #GlobalRemuneration tgr on tgr.GlobalRemunerationID = tgrg.GlobalRemunerationID
join DataCore..GlobalRemuneration gr on gr.CompanyID = #CompanyID and gr.FiscalYearID = tgr.FiscalYearID and gr.DirectorDetailID = tgr.DirectorDetailID and tgr.GlobalRoleIDCODE = gr.GlobalRoleID
join DataCore..GlobalRemunerationGrant grg on gr.GlobalRemunerationID = grg.GlobalRemunerationID
Thank you.
Based on the comment - you have 2 values to match on, not just one? e.g., both GlobalRemunerationID and GlobalRemunerationGrantID?
Here's an example using tables 'temptable' and 't1'
UPDATE temptable
SET ReconGlobalRemunerationGrantID = t1.GlobalRemunerationGrantID
FROM temptable
INNER JOIN t1 ON temptable.GlobalRemunerationID = t1.GlobalRemunerationID
AND temptable.GlobalRemunerationGrantID = t1.GlobalRemunerationGrantID
Update below
The below version takes the two data sets
Partitions them by GlobalRemunerationID and orders them by ReconGlobalRemunerationGrantID to get the 'row numbers' (rn)
Joins them on GlobalRemunerationID and rn to get them in order
Key code is below (with slightly different tables than your full set sorry - matches the data set you gave though).
; WITH tgrg AS
(SELECT GlobalRemunerationID, ReconGlobalRemunerationGrantID,
ROW_NUMBER() OVER (PARTITION BY GlobalRemunerationID ORDER BY GlobalRemunerationGrantID) AS rn
FROM #GlobalRemunerationGrant
)
UPDATE tgrg
SET ReconGlobalRemunerationGrantID = tgr.GlobalRemunerationGrantID
FROM tgrg
INNER JOIN
(SELECT GlobalRemunerationID, GlobalRemunerationGrantID,
ROW_NUMBER() OVER (PARTITION BY GlobalRemunerationID ORDER BY GlobalRemunerationGrantID) AS rn
FROM GlobalRemuneration
) AS tgr ON tgrg.GlobalRemunerationID = tgr.GlobalRemunerationID AND tgrg.rn = tgr.rn
A db<>fiddle with the full set is there - note that I changed some of the IDs to demonstrate that it wasn;t using them to match.

VIEWS with SELECT inside conditions delaying the query

In one of my SQL views I am using an inline select statement with a where clause.
The outline of my view is like
ALTER VIEW [dbo].[vw_autumn]
AS
SELECT
BookNumber, Title, shopNo
FROM
(SELECT
BookNumber, Title, shopNO
FROM
(SELECT DISTINCT
(sum_vnr) AS BookNumber,
navn1 AS Title,
tik AS ShopNO,
ROW_NUMBER() OVER (PARTITION BY sum_vnr, tik ORDER BY sum_vnr DESC) AS rownumber
FROM
sum s
INNER JOIN
hod h ON s.tik = h.tik
WHERE
s.aar = (SELECT currentyear
FROM SemesterInfo
WHERE SemName = 'Autumn')
AND CAST(s.sum_vnr AS BIGINT) > 10000
AND (s.id LIKE 'h%' OR s.id LIKE 'H%' OR s.id LIKE 'j%'
OR s.id LIKE 'J%')) a
WHERE rownumber = 1
) b
LEFT JOIN (
------
) p ON b.ShopNO = p.tikk
AND b.ISBN = p.vnr
LEFT JOIN table_k k ON p.aar = k.aar
GO
And if I remove the WHERE clause of
WHERE
s.aar = (SELECT currentyear
FROM SemesterInfo
WHERE SemName = 'Autumn')
and shorten it to
WHERE s.aar =19
I am getting the result of view very quickly. But I am trying to add some dynamic nature to this query and selecting this constant from a settings table
Any thoughts on this? Why is the query taking an indefinite time to load with an inline Where clause?
:try with IN insted of =
WHERE
s.aar in (SELECT currentyear
FROM SemesterInfo
WHERE SemName = 'Autumn')
Rewrite the subquery as a join.
INNER JOIN SemesterInfo si
ON s.aer = si.currentYear
WHERE si.SemName = 'Autumn'
If that doesn't do it, consider keeping this syntax and creating an index on SemName

Replacement for UPDATE statement with ORDER BY clause

I am having a hard time trying to execute an update query that should contain ORDER BY clause, but I'm unable to find a proper solution yet.
UPDATE I
SET RefItemID = AQ.ID,
I.MagParamNum = AQ.MagParamNum
FROM SRO_VT_SHARD.._Items I
JOIN SRO_VT_SHARD.._Inventory INV ON INV.ItemID = I.ID64
JOIN SRO_VT_SHARD.._RefObjCommon ROC ON ROC.ID = I.RefItemID
JOIN _AEQItems AQ ON AQ.TypeID3 = ROC.TypeID3
AND AQ.TypeID4 = ROC.TypeID4
WHERE
INV.Slot BETWEEN 0 AND 13
AND INV.Slot != 8
AND AQ.ReqLevel1 <= #Data2
AND INV.CharID = #CharID
ORDER BY AQ.ReqLevel1 DESC
Basically my query should work this way if ORDER BY clause is usable inside an update statement, but it doesn't. Is there something I can do which should solve this?
Thanks a lot in advance.
You need to determine the exact row to update for each TypeID3 / TypeID4 combination, and you can't do that in the outer query. You may need to add additional ORDER BY clauses here to break ties. You may also want to specify only a subset of columns if you have an index that covers the columns in _AEQItems used to search and the columns you're updating.
;WITH AQ AS
(
SELECT *, rn = ROW_NUMBER() OVER
(PARTITION BY TypeID3, TypeID4 ORDER BY ReqLevel1 DESC)
FROM _AEQItems
)
UPDATE I
SET RefItemID = AQ.ID,
MagParamNum = AQ.MagParamNum
FROM SRO_VT_SHARD.._Items I
JOIN SRO_VT_SHARD.._Inventory INV ON INV.ItemID = I.ID64
JOIN SRO_VT_SHARD.._RefObjCommon ROC ON ROC.ID = I.RefItemID
JOIN AQ ON AQ.TypeID3 = ROC.TypeID3 AND AQ.TypeID4 = ROC.TypeID4
WHERE AQ.rn = 1
AND INV.Slot BETWEEN 0 AND 13
AND INV.Slot!=8
AND AQ.ReqLevel1 <= #Data2
AND INV.CharID = #CharID;
Use a subquery:
UPDATE I
SET RefItemID=AQ.ID,I.MagParamNum=AQ.MagParamNum
FROM SRO_VT_SHARD.._Items I
JOIN SRO_VT_SHARD.._Inventory INV ON INV.ItemID=I.ID64
JOIN SRO_VT_SHARD.._RefObjCommon ROC ON ROC.ID=I.RefItemID
JOIN (SELECT TypeID3, TypeID4, MAX(ReqLevel1) AS ReqLevel1 FROM _AEQItems GROUP BY TypeID3, TypeID4) AQ
ON AQ.TypeID3=ROC.TypeID3
AND AQ.TypeID4=ROC.TypeID4
WHERE INV.Slot BETWEEN 0 AND 13 AND INV.Slot!=8 AND AQ.ReqLevel1<=#Data2 AND INV.CharID=#CharID

Turn Date into Column with Pivot

Im not good at using Pivot but i think that's the only way to solve my Problem.
I have this SQL
SELECT DISTINCT ADR_Adressen.AdressNrADR
, LEFT(ADR_Adressen.Name, 3) AS Name
, LEFT(ADR_Adressen.Vorname, 3) AS Vorname
, CRM_Aufgaben.TerminVon
, LAG_Artikel.ArtikelNrLAG
, CRM_AufgabenLink.MitNrPRO
FROM ADR_Adressen
INNER JOIN PRO_Auftraege ON ADR_Adressen.AdressNrADR = PRO_Auftraege.Kunde
INNER JOIN CRM_Aufgaben ON PRO_Auftraege.AuftragNrPRO = CRM_Aufgaben.AuftragNrPRO
INNER JOIN CRM_Status ON CRM_Aufgaben.StatusCRM = CRM_Status.StatusCRM
INNER JOIN LAG_Artikel ON CRM_Aufgaben.ArtikelNrLAG = LAG_Artikel.ArtikelNrLAG
INNER JOIN ADR_GruppenLink ON ADR_Adressen.AdressNrADR = ADR_GruppenLink.AdressNrADR
INNER JOIN ADR_Gruppen ON ADR_GruppenLink.GruppeADR = ADR_Gruppen.GruppeADR
INNER JOIN CRM_AufgabenLink ON CRM_Aufgaben.AufgabenNrCRM = CRM_AufgabenLink.AufgabenNrCRM
WHERE { d '2016-03-07'} <= CRM_Aufgaben.TerminVon
AND { d '2016-03-11'} + 1 >= CRM_Aufgaben.TerminBis
AND CRM_AufgabenLink.MitNrPRO != 0
AND ADR_Gruppen.GruppeADR IN ( 'KIND' )
This is my result:
My wish is to get a Output like this:
The different Dates in TerminVon has to be Columns with the Values from ArtikelNrLAG+MitNrPRO. If the same AdressNrADR has more then one TerminVon on the same Date i have to make more rows. (Example where Name = Boc,Alt)
Can someone help me please =)
To PIVOT what you have, you can use a query similar to this.
SELECT AdressNrADR,
Name,
Vorname,
[2016-03-07],
[2016-03-08],
[2016-03-09],
[2016-03-10],
[2016-03-11]
FROM (
SELECT DISTINCT
ADR_Adressen.AdressNrADR,
LEFT(ADR_Adressen.Name,3) AS Name,
LEFT(ADR_Adressen.Vorname,3) AS Vorname,
CONVERT(VARCHAR(10), CRM_Aufgaben.TerminVon, 120) AS TerminVon, -- Convert date to yyyy-mm-dd format
LAG_Artikel.ArtikelNrLAG + '+' + CRM_AufgabenLink.MitNrPRO AS [Value], -- Combine column values
ROW_NUMBER() OVER
(PARTITION BY AdressNrADR,
LEFT(ADR_Adressen.Name,3),
LEFT(ADR_Adressen.Vorname,3),
CAST(CRM_Aufgaben.TerminVon AS DATE)
ORDER BY CRM_Aufgaben.TerminVon) Rn -- So we can get 1 row per time value
FROM ADR_Adressen
INNER JOIN PRO_Auftraege ON ADR_Adressen.AdressNrADR = PRO_Auftraege.Kunde
INNER JOIN CRM_Aufgaben ON PRO_Auftraege.AuftragNrPRO = CRM_Aufgaben.AuftragNrPRO
INNER JOIN CRM_Status ON CRM_Aufgaben.StatusCRM = CRM_Status.StatusCRM
INNER JOIN LAG_Artikel ON CRM_Aufgaben.ArtikelNrLAG = LAG_Artikel.ArtikelNrLAG
INNER JOIN ADR_GruppenLink ON ADR_Adressen.AdressNrADR = ADR_GruppenLink.AdressNrADR
INNER JOIN ADR_Gruppen ON ADR_GruppenLink.GruppeADR = ADR_Gruppen.GruppeADR
INNER JOIN CRM_AufgabenLink ON CRM_Aufgaben.AufgabenNrCRM = CRM_AufgabenLink.AufgabenNrCRM
WHERE { d '2016-03-07'} <= CRM_Aufgaben.TerminVon
AND { d '2016-03-11'} + 1 >= CRM_Aufgaben.TerminBis
AND CRM_AufgabenLink.MitNrPRO != 0
AND ADR_Gruppen.GruppeADR IN ('KIND')
) t
PIVOT (
MAX([Value])
FOR TerminVon IN ([2016-03-07],[2016-03-08],[2016-03-09],[2016-03-10],[2016-03-11])
) p
If you get that query to work. Your next step would be to make it Dynamic.
The difficult part of using t-sql's pivot functionality is that the output column names have to be hard coded. In your example we would need to know the value of each date and use that in the query in order to get the matching values by date. Fortunately other fine developers have experienced this frustration for us and have created scripts that will generate a dynamic pivot. I have included two links that will help you on your way.
https://www.mssqltips.com/sqlservertip/2783/script-to-create-dynamic-pivot-queries-in-sql-server/
http://sqlhints.com/2014/03/18/dynamic-pivot-in-sql-server/

SQL Server: breaking a multiple join query into smaller ones and creating a function

I have a complex query with multiple joins in it, which runs multiple times in my application. I want to write this query as a function by breaking this query into smaller pieces inside the function. As a newbie, I have limited knowledge on SQL Server.
The following is the query:
SELECT
ts.lable as label,
ts.percentage as rate
FROM
TaxSet ts
JOIN
UserInfo u ON u.userID = ?
AND u.countryID = ts.countryId
AND (ts.stateId IS NULL OR ts.stateId = 0 OR LEN(ts.stateId) < 1)
JOIN
Users us ON u.userID = us.id
JOIN
Users p ON us.parentID = p.id
AND ts.ispID = p.id
JOIN
ProductType pt ON ts.productTypeID = pt.id
WHERE
startDate <= getutcdate()
AND getutcdate() <= endDate
AND pt.identifier = ?
AND ts.id NOT IN (SELECT eu.ispTaxSettingId
FROM ExemptUsers eu
WHERE eu.ExemptUserId = ?)
Now, how can I write a function by breaking this query into smaller ones.
Thanks in advance.
May I ask why you want to split it into functions? I reformatted your code and have put it into a stored procedure for now. My thinking is that you want to pass through a Identifier and UserID which are the parameters of your query.
I have modified the query and removed the Not In statement. This has been replaced by a LEFT JOIN to ExemptUsers on u.id = eu.ExemptUserID and then an addition to the WHERE clause to ensure eu.ExemptUserID is NULL. This is basically a clearer way of saying "If the userID exists in table ExemptUsers do not bring back results for that user".
In addition I have removed the join to Users p as I can't see that this was being used in any way, unless you want to ensure that the user has a parent?
CREATE PROCEDURE wsp_StoredProcName
(#UserID int,
#Identifier int)
AS
BEGIN
SELECT
ts.lable as label,
ts.percentage as rate
FROM
TaxSet ts
INNER JOIN UserInfo u ON u.userID = ts.UserID
AND u.countryID = ts.countryId
INNER JOIN Users us on u.userID = us.id
INNER JOIN ProductType pt on ts.productTypeID = pt.id
LEFT JOIN ExemptUsers eu on u.id = eu.ExemptUserID
WHERE
(
ts.UserID = #UserID
and pt.identifier = #Identifier
and startDate <= getutcdate()
and getutcdate() <= endDate
AND eu.ExemptUserID IS NULL
and
(
ts.stateId is null or ts.stateId = 0 or len(ts.stateId) < 1
)
)
END
After all you select from TaxSet where certain conditions must be met: The date range, the state, a relation to a particular user and its parent, a relation to a particular product type, and the non-existence for a particular exempt user. So use EXISTS and NOT EXISTS throughout your query in order to make it plain to the reader and dbms what you want to achieve. The more straight-forward a query, the easier it often is for the optimizer to deal with it.
select
ts.lable as label,
ts.percentage as rate
from taxset ts
where getutcdate() between ts.startdate and ts.enddate
and (stateid is null or stateid between 0 and 9)
and exists
(
select *
from users u
join userinfo ui on ui.userid = u.id
where u.id = ?
and ui.countryid = ts.countryid
and u.parentid = ts.ispid
)
and exists
(
select *
from producttype pt
where pt.identifier = ?
and pt.id = ts.producttypeid
)
and not exists
(
select *
from exemptusers eu
where eu.exemptuserid = ?
and eu.isptaxsettingid = ts.id
);
As others have mentioned: When splitting a query into smaller parts and execute these separately, it usually becomes slower not faster. This is due to the fact that a dbms is made to do exactly this in the most efficient way internally. Of course sometimes a dbms' optimizer fails to find a good execution plan, though. You may want to look at the execution plan and check whether you find that plan appropriate.

Resources