Hi Everyone,
I need a help in writing a bit tricky logic to write, I will explain the scenario with sample data.
I have two datasets (result set of two separate SQL Queries) -
Dataset 1: PolicyHeader
PolId PolHeader
D0A4EUX20A0Q EST
U00281620A0F EST
Dataset 2: PolicyAmount
PolId TrnType OrigPMAmt
D0A4EUX20A0Q FLAT 31250.00
U00281620A0F FLAT 699.75
U00281620A0F FLAT 949.29
U00281620A0F FLAT 36524.10
U00281620A0F FLAT 10462.05
U00281620A0F FLAT 140660.77
U00281620A0F FLAT 17430.43
U00281620A0F FLAT 68317.46
U00281620A0F EST 275043.85
Now following logic needs to be build to -
Select records from the Dataset PolicyAmount and condition is if PolHeader column value in for a PolId in PolicyHeader dataset is EST and in the PolicyAmount there is a corresponding records having TrnType = 'EST' then those records should be selected and if there is no EST row for the policy and from PolicyHeader it is coming as PolHeader = 'EST' then PolHeader value should be swiched to 'FLAT' and should select records from PolicyAmount table having TrnType = 'FLAT'
Expected output of the final SQL should be -
PolId TrnType OrigPMAmt
D0A4EUX20A0Q FLAT 31250.00
U00281620A0F EST 275043.85
Thanks in advance!
June 4, 2021 at 3:27 pm
No directly usable data to test with, but I think this should at least be close:
SELECT PH.*, PA.*
FROM dbo.PolicyHeader PH
OUTER APPLY (
SELECT TOP (1) PA.*
FROM dbo.PolicyAmount PA
WHERE PH.PolHeader = 'EST' AND PA.PolId = PH.PolId AND PA.TrnType = 'EST'
) AS PA_EST
INNER JOIN dbo.PolicyAmount PA ON PA.PolId = PH.PolId AND
(PH.PolHeader <> 'EST' OR ((PA_EST.PolId IS NOT NULL AND PA.TrnType = 'EST') OR (PA_EST.PolId IS NULL AND PA.TrnType = 'FLAT')))
SQL DBA,SQL Server MVP(07, 08, 09) A socialist is someone who will give you the shirt off *someone else's* back.
You could try something like this
select ph.PolId,
case when isnull(pa_est.est_count, 0)>0
then ph.PolHeader else 'FLAT' end TrnType,
case when isnull(pa_est.est_count, 0)>0
then pa_est.est_amnt
else isnull(pa_flat.flat_amnt, 0) end OrigPMAmt
from PolicyHeader ph
outer apply (select count(*), sum(OrigPMAmt)
from PolicyAmount pa
where pa.PolId=ph.PolId
and pa.TrnType=ph.PolHeader) pa_est(est_count, est_amnt)
outer apply (select sum(OrigPMAmt)
from PolicyAmount pa
where pa.PolId=ph.PolId
and pa.TrnType='FLAT') pa_flat(flat_amnt)
where ph.PolHeader='EST';
Aus dem Paradies, das Cantor uns geschaffen, soll uns niemand vertreiben können
June 15, 2021 at 10:38 am
Thank you so much Scott, you saved my day!
Sorry for the late reply as this was the part of the main SQL and I was testing this with live data for accuracy and performance wise and logic works perfectly but bit expensive on performance side but will work for me.
P.S. I am not sure what do you mean by "no directly usable data to test"
Cheers.
June 15, 2021 at 10:39 am
Thank you Steve!
This query also works from logic perspective
Cheers!
June 15, 2021 at 1:37 pm
P.S. I am not sure what do you mean by "no directly usable data to test"
He means "Readily Consumable" data. Please see the article at the first link below for some tips on posting in the future. "Readily Consumable" data will help those that try to help you a whole lot. What you posted in your original post isn't "Readily Consumable".
--Jeff Moden
Change is inevitable... Change for the better is not.
June 15, 2021 at 2:01 pm
vaibhavktiwari wrote:P.S. I am not sure what do you mean by "no directly usable data to test"
He means "Readily Consumable" data. Please see the article at the first link below for some tips on posting in the future. "Readily Consumable" data will help those that try to help you a whole lot. What you posted in your original post isn't "Readily Consumable".
Oh I see, my apologies!
That’s very helpful article, I will keep that mind for future posts and thanks again all for the help.
Cheers!
June 29, 2021 at 9:48 pm
You could try something like this
select ph.PolId,
case when isnull(pa_est.est_count, 0)>0
then ph.PolHeader else 'FLAT' end TrnType,
case when isnull(pa_est.est_count, 0)>0
then pa_est.est_amnt
else isnull(pa_flat.flat_amnt, 0) end OrigPMAmt
from PolicyHeader ph
outer apply (select count(*), sum(OrigPMAmt)
from PolicyAmount pa
where pa.PolId=ph.PolId
and pa.TrnType=ph.PolHeader) pa_est(est_count, est_amnt)
outer apply (select sum(OrigPMAmt)
from PolicyAmount pa
where pa.PolId=ph.PolId
and pa.TrnType='FLAT') pa_flat(flat_amnt)
where ph.PolHeader='EST';
Hi Steve,
I realized, your query gives correct result for the policy header 'FLAT' and 'EST ' both however Scott's query was not giving correct result for Policy header 'Flat' somehow.
Thanks.
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply