In the below query I need to take only one record which is the latest phDate.
WITH TEMP_PAT_PIP_AddedBy
AS (
SELECT S.ppId
,(
SELECT PersonID
FROM [PFMADestination].[dbo].PERSON
WHERE SourceType = 'user_Users'
AND SourceId = S.uuId
) AS AddedByToStore
,phDate
FROM [PFMADestination].[dbo].PatientPIP D
INNER JOIN [PFMAOldProd].[dbo].pat_History S ON S.ppId = D.PatientId
AND phEvent = 'PIP-info uppdaterad'
)
--select * from TEMP_PAT_PIP_AddedBy order by ppId, phDate desc
UPDATE P
SET AddedBy = ISNULL(AddedByToStore, 1), Date=phDate
FROM TEMP_PAT_PIP_AddedBy T
INNER JOIN [PFMADestination].[dbo].[PatientPIP] P ON T.ppId = P.PatientId
Expected Output for CTE Take only latest phDate for each ppId and ignore other if its more than 1 record then update it.
Using partition by clause you can get expected output. Try following query :
WITH TEMP_PAT_PIP_AddedBy
AS (
SELECT S.ppId
,(
SELECT PersonID
FROM [PFMADestination].[dbo].PERSON
WHERE SourceType = 'user_Users'
AND SourceId = S.uuId
) AS AddedByToStore
,phDate , ROW_NUMBER() OVER(PARTITION BY S.ppId ORDER BY phDate DESC) AS NUM
FROM [PFMADestination].[dbo].PatientPIP D
INNER JOIN [PFMAOldProd].[dbo].pat_History S ON S.ppId = D.PatientId
AND phEvent = 'PIP-info uppdaterad'
)
UPDATE P
SET AddedBy = ISNULL(AddedByToStore, 1), Date=phDate
FROM TEMP_PAT_PIP_AddedBy T
INNER JOIN [PFMADestination].[dbo].[PatientPIP] P
ON T.ppId = P.PatientId AND T.NUM=1
Add phDate = max phDate condition to the where clause:
WITH TEMP_PAT_PIP_AddedBy
AS (
SELECT S.ppId
,(
SELECT PersonID
FROM [PFMADestination].[dbo].PERSON
WHERE SourceType = 'user_Users'
AND SourceId = S.uuId
) AS AddedByToStore
,phDate
FROM [PFMADestination].[dbo].PatientPIP D
INNER JOIN [PFMAOldProd].[dbo].pat_History S ON S.ppId = D.PatientId
AND phEvent = 'PIP-info uppdaterad'
)
SELECT *
FROM TEMP_PAT_PIP_AddedBy
where phDate = (select max(phDate) from TEMP_PAT_PIP_AddedBy
where ppId = TEMP_PAT_PIP_AddedBy.ppId)
ORDER BY ppId
,phDate DESC
Edited version, is phDate a P column? (If not, edit my answer.)
WITH TEMP_PAT_PIP_AddedBy
AS (
SELECT S.ppId
,(
SELECT PersonID
FROM [PFMADestination].[dbo].PERSON
WHERE SourceType = 'user_Users'
AND SourceId = S.uuId
) AS AddedByToStore
,phDate
FROM [PFMADestination].[dbo].PatientPIP D
INNER JOIN [PFMAOldProd].[dbo].pat_History S ON S.ppId = D.PatientId
AND phEvent = 'PIP-info uppdaterad'
AND phDate = (select max(phDate) from P
where ppId = P.ppId)
)
--select * from TEMP_PAT_PIP_AddedBy order by ppId, phDate desc
UPDATE P
SET AddedBy = ISNULL(AddedByToStore, 1), Date=phDate
FROM TEMP_PAT_PIP_AddedBy T
INNER JOIN [PFMADestination].[dbo].[PatientPIP] P ON T.ppId = P.PatientId
Svensk?
Related
WITH CTEDOC AS
(
SELECT
ROW_NUMBER() OVER (PARTITION BY d.CaseDocumentID ORDER BY PageSequence) [rowN],
d.DocumentID
FROM tblCaseDocument cd WITH(NOLOCK)
JOIN tblDocument d WITH(NOLOCK) ON d.CaseDocumentID = cd.CaseDocumentID
WHERE NOT EXISTS
(
SELECT * FROM tblDocument (NOLOCK)
WHERE PageSequence = 1
and SourceTableID = cd.CaseDocumentID
)
AND cd.DocumentCount > 0
)
UPDATE d SET PageSequence = cte.rowN
FROM CTEDOC CTE
JOIN tblDocument d (NOLOCK) ON d.DocumentID = cte.DocumentID
uodate Occured for both files?
I want to select only the first record from each group. So basically I only want the data for first processing date column and don't want the rest of the date for each product.
My current code as below
SELECT
C.AccountID as ABN_ACC ,
C.ProductSymbol,
C.ProductShortName,
S.ReactorProduct AS REC_PROD,
C.CurrencyCode AS PROD_CCY,
C.CountryOfPayment AS PAYCONTRY,
C.DividendValueCur AS PAYCCY,
C.DividendValue AS DIV_AMNT,
CONCAT ((IIF (C.QuantitySettledNoTax_LS = 'S' ,(-1*C.QuantitySettledNoTax),C.QuantitySettledNoTax )),(IIF(C.QuantitySettledTax_LS = 'S',(-1*C.QuantitySettledTax),QuantitySettledTax))) AS DIVQTY ,
C.ExdividendDate AS EXDATE,
C.Recorddate AS RECDATE,
C.DividendPayDate AS PAYDATE
From "LiquidCDW". [Staging].[CA_CorporateActions] AS C
RIGHT JOIN "LiquidCDW".[Staging].[POS_SettledPositions] AS P ON C.Recorddate = P.ProcessingDate AND C.AccountID = p.AccountID AND C.ProductSymbol = P.ProductSymbol AND C.DividendValueCur = P.CurrencyCode
INNER JOIN "LiquidCDW".[Transformation].[Mapping_Product_ABNReactor] AS S ON C.ProductSymbol = S.ReactorProduct AND S.ValidTo IS NULL AND S.ProductType = 'E' AND c.DividendValueCur = S.Currency
where C.ExdividendDate >= '20210201'
ORDER by C.ProductSymbol , C.CurrencyCode
One approach uses ROW_NUMBER:
WITH cte AS (
SELECT
ROW_NUMBER() OVER (PARTITION BY C.ProductSymbol ORDER BY processingDate) rn,
C.AccountID AS ABN_ACC,
C.ProductSymbol,
C.ProductShortName,
S.ReactorProduct AS REC_PROD,
C.CurrencyCode AS PROD_CCY,
C.CountryOfPayment AS PAYCONTRY,
C.DividendValueCur AS PAYCCY,
C.DividendValue AS DIV_AMNT,
CONCAT((IIF(C.QuantitySettledNoTax_LS = 'S',
(-1*C.QuantitySettledNoTax), C.QuantitySettledNoTax)),
(IIF(C.QuantitySettledTax_LS = 'S',
(-1*C.QuantitySettledTax),QuantitySettledTax))) AS DIVQTY,
C.ExdividendDate AS EXDATE,
C.Recorddate AS RECDATE,
C.DividendPayDate AS PAYDATE
FROM [LiquidCDW].[Staging].[CA_CorporateActions] AS C
RIGHT JOIN "LiquidCDW".[Staging].[POS_SettledPositions] AS P
ON C.Recorddate = P.ProcessingDate AND
C.AccountID = p.AccountID AND
C.ProductSymbol = P.ProductSymbol AND
C.DividendValueCur = P.CurrencyCode
INNER JOIN [LiquidCDW].[Transformation].[Mapping_Product_ABNReactor] AS S
ON C.ProductSymbol = S.ReactorProduct AND
S.ValidTo IS NULL AND
S.ProductType = 'E' AND
C.DividendValueCur = S.Currency
WHERE
C.ExdividendDate >= '20210201'
)
SELECT *
FROM cte
WHERE rn = 1
ORDER BY ProductSymbol, CurrencyCode;
Below query is just taking long time and the below predicate is used only to get unique records, as such was wondering is there a different way to rewrite the same query without calling the below predicate multiple times, to get the unique ID.
select max(c.id) from plocation c where c.ids = y.ids and c.idc = y.idc)
select max(cr.id) from plocation_log cr where cr.ids = yt.ids and cr.idc = yt.idc)
select max(pr.id) from patentpr where pr.ids = p.ids and pr.idc = p.idc)
My full sample query
SELECT to_char(p.pid) AS patentid,
p.name,
x.dept,
y.location
FROM patent p
JOIN pdetails x ON p.pid = x.pid AND x.isactive = 1
JOIN plocation y
ON y.idr = p.idr
AND y.idc = p.idc
AND y.id = *(select max(c.id) from plocation c where c.ids = y.ids and c.idc = y.idc)*
AND y.idopstype in (36, 37)
JOIN plocation_log yt
ON yt.idr = y.idr
AND yt.idc= y.idc
AND yt.id = *(select max(cr.id) from plocation_log cr where cr.ids = yt.ids and cr.idc = yt.idc)*
AND yt.idopstype in (36,37)
WHERE
p.idp IN (10,20,30)
AND p.id = *(select max(pr.id) from patent pr where pr.ids = p.ids and pr.idc = p.idc)*
AND p.idopstype in (36,37)
Consider joining to aggregate CTEs to calculate MAX values per group once as opposed to rowwise MAX calculation for every outer query row. Also, be sure to use more informative table aliases instead of a, b, c or x, y, z style.
WITH loc_max AS
(select ids, idc, max(id) as max_id from plocation group ids, idc)
, log_max AS
(select ids, idc, max(id) as max_id from plocation_log group by ids, idc)
, pat_max AS
(select ids, idc, max(id) as max_id from patent pr group by ids, idc)
SELECT to_char(pat.pid) AS patentid
, pat.name
, det.dept
, loc.location
FROM patent pat
JOIN pdetails det
ON pat.pid = det.pid
AND det.isactive = 1
JOIN plocation loc
ON loc.idr = pat.idr
AND loc.idc = pat.idc
AND loc.idopstype IN (36, 37)
JOIN loc_max -- ADDED CTE JOIN
ON loc.id = loc_max.max_id
AND loc.ids = loc_max.ids
AND loc.idc = loc_max.idc
JOIN plocation_log log
ON log.idr = log.idr
AND log.idc = log.idc
AND log.idopstype in (36,37)
JOIN log_max -- ADDED CTE JOIN
ON log.id = log_max.max_id
AND log.ids = log_max.ids
AND log.idc = log_max.idc
JOIN pat_max -- ADDED CTE JOIN
ON pat.id = pat_max.max_id
AND pat.ids = pat_max.ids
AND pat.idc = pat_max.idc
WHERE pat.idp IN (10, 20, 30)
AND pat.idopstype IN (36, 37)
As commented by The Impaler, one option is to use analytic functions instead of correlated subqueries. The idea is to rank records within subqueries using RANK(), then filter in the outer query (join conditions or WHERE clause).
Consider:
SELECT to_char(p.pid) AS patentid,
p.name,
x.dept,
y.location
FROM (SELECT p.*, RANK() OVER(PARTITION BY ids, idc ORDER BY id) rn FROM patinet) p
JOIN pdetails x ON p.pid = x.pid AND x.isactive = 1
JOIN (SELECT y.*, RANK() OVER(PARTITION BY ids, idc ORDER BY id) rn FROM plocation y) y
ON y.idr = p.idr
AND y.idc = p.idc
AND y.idopstype in (36, 37)
AND y.rn = 1
JOIN (SELECT y.*, RANK() OVER(PARTITION BY ids, idc ORDER BY id) rn FROM plocation_log yt) yt
ON yt.idr = y.idr
AND yt.idc= y.idc
AND yt.idopstype in (36,37)
AND yt.rn = 1
WHERE
p.idp IN (10,20,30)
AND p.idopstype in (36,37)
AND p.rn = 1
I have a HISTORY table that has multiple rows for the same record and I am trying to get the latest (closest to today's date) record. I am attempting to group by the closest date but am having a difficult time. Please check out the query below and advise me.
SELECT DISTINCT *
FROM
(SELECT etc.Complaint.Complaint_ID AS Complaint_ID
FROM etc.Complaint) AS Qry1
LEFT JOIN
(SELECT etc.Complaint.Complaint_ID AS Complaint_ID,
o.Action_User AS Resolved_User,
o.Action_Date AS LastActionDate
FROM etc.Complaint
LEFT OUTER JOIN etc.History as o
ON SUBSTRING(Primary_Key,15,LEN(Primary_Key) - 15) = etc.Complaint.Complaint_ID
AND TABLE_NAME = 'Resolution' AND o.Field_Name = 'Resolved_Ind'
AND New_Value = 1) AS Qry2
ON Qry1.Complaint_ID = Qry2.Complaint_ID
ORDER BY Qry1.Complaint_ID, MAX(Qry2.LastActionDate)
does this change help?
SELECT DISTINCT *
FROM
(SELECT etc.Complaint.Complaint_ID AS Complaint_ID FROM etc.Complaint) AS Qry1
LEFT JOIN
(SELECT etc.Complaint.Complaint_ID AS Complaint_ID,
o.Action_User AS Resolved_User,
o.Action_Date AS LastActionDate
FROM etc.Complaint
LEFT OUTER JOIN
(
SELECT SUBSTRING(Primary_Key,15,LEN(Primary_Key) - 15) as hist_Complaint_ID , MAX(Action_Date) as Action_Date
FROM etc.History
WHERE Field_Name = 'Resolved_Ind'
GROUP BY SUBSTRING(Primary_Key,15,LEN(Primary_Key) - 15)
) as o
ON o.hist_Complaint_ID = etc.Complaint.Complaint_ID
AND TABLE_NAME = 'Resolution' AND o.Field_Name = 'Resolved_Ind'
AND New_Value = 1) AS Qry2
ON Qry1.Complaint_ID = Qry2.Complaint_ID
ORDER BY Qry1.Complaint_ID, Qry2.LastActionDate
You can use ROW_NUMBER and a CTE to get it:
WITH cte AS (
SELECT etc.Complaint.Complaint_ID AS Complaint_ID,
o.Action_User AS Resolved_User,
o.Action_Date AS LastActionDate
row_number() over (partition by etc.Complaint.Complaint_ID order by o.Action_Date desc) AS rn
FROM etc.Complaint
LEFT OUTER JOIN etc.History as o
ON SUBSTRING(Primary_Key,15,LEN(Primary_Key) - 15) = etc.Complaint.Complaint_ID
AND TABLE_NAME = 'Resolution' AND o.Field_Name = 'Resolved_Ind'
AND New_Value = 1
)
SELECT * FROM cte
WHERE rn = 1
SELECT P.*,R.PlaceCategory,Pr.RatingAverage,
ROW_NUMBER() OVER (PARTITION BY R.PlaceCategoryId ORDER BY Pr.RatingAverage DESC) AS RatingRank
FROM dbo.Places AS P
INNER JOIN
dbo.Cities AS C
ON P.CityId = C.CityId
LEFT JOIN
(SELECT SUM(Rat.Rating) / Count(Rat.Rating) AS RatingAverage,
Rat.PlaceId
FROM PlaceReview AS Rat
GROUP BY PlaceId) AS Pr
ON Pr.PlaceId = P.PlaceId
INNER JOIN
(SELECT TOP 2 PlaceCategoryId,PlaceCategory
FROM dbo.PlaceCategory
WHERE [Status] = 1
ORDER BY DisplayOrder) AS R
ON R.PlaceCategoryId = P.PlaceCategoryId
WHERE (P.CityId = #cityId OR C.City LIKE '%'+#cityName+'%')
AND
(P.[Status]=1 AND P.IsVerified = 1);
I want to add WHERE RatingRank<5. Is it possible without making this a subQuery? Sorry for the direct question.
Add the condition in an outer block since it can't be specified in the same query.
SELECT * FROM
(
SELECT P.*,R.PlaceCategory,Pr.RatingAverage,
ROW_NUMBER() OVER (PARTITION BY R.PlaceCategoryId ORDER BY Pr.RatingAverage DESC) AS RatingRank
FROM dbo.Places AS P
INNER JOIN
dbo.Cities AS C
ON P.CityId = C.CityId
LEFT JOIN
(SELECT SUM(Rat.Rating) / Count(Rat.Rating) AS RatingAverage,
Rat.PlaceId
FROM PlaceReview AS Rat
GROUP BY PlaceId) AS Pr
ON Pr.PlaceId = P.PlaceId
INNER JOIN
(SELECT TOP 2 PlaceCategoryId,PlaceCategory
FROM dbo.PlaceCategory
WHERE [Status] = 1
ORDER BY DisplayOrder) AS R
ON R.PlaceCategoryId = P.PlaceCategoryId
WHERE (P.CityId = #cityId OR C.City LIKE '%'+#cityName+'%')
AND
(P.[Status]=1 AND P.IsVerified = 1)
)x WHERE RatingRank<5;