Declare #Status nvarchar(25) = Null
Select * From Products
Where Type = 1
And
If Status is null, Don't add clause(means return all)
If Status = 'C', Add Status = 'Completed'
If Status = 'U', Add Status = 'Uncompleted'
How can I do this?
Don't call your variable the same as your column
Declare #MyStatus nvarchar(25) = Null
Select * From Products
Where Type = 1
AND (
(MyStatus is null)
OR (MyStatus = 'C' AND Status = 'Completed')
OR (MyStatus = 'U' AND Status = 'Uncompleted')
)
It would be better to simply assign the desired value to your #Status variable then you can simply check for equality -
where Type = 1 and (#status = status or #Status is null);
Code is showing an error
Merge statements with a WHEN NOT MATCHED [BY TARGET] clause must target a hash distributed table
Also please verify that the overall syntax is fine or not?
MERGE IR_CREDITREQUEST_SPTESTING AS T
USING (
SELECT
TT.TaxYear
,TT.TaxPayerSSN
,TT.EngagementID
,TT.CreditRequestType
,TT.BorrowerID
,TT.NameSuffix
,TT.FirstName
,TT.MiddleName
,TT.LastName
,TT.SpouseName
,TT.SpouseSSN
,TT.PrintPositionType
,TT.MaritalStatusType
,TT.StreetAddress
,TT.City
,TT.State
,TT.PostalCode
,TT.Type
,TT.Value
,TT.RequestDateTime
,TT.CreatedDateTime
,TT.RequestProcessed
,TT.BorrowerResidencyType
,TT.isActive
FROM
IR_CREDITREQUEST_SPTESTING TT
INNER JOIN (
select DISTINCT
A.TaxYear,
A.[TaxPayer-SSN] AS TaxPayerSSN,
A.EngagementID AS EngagementID,
CASE
WHEN A.[Filing Status] = 'MFJ' THEN 'Joint'
ELSE A.[Filing Status]
END AS [CreditRequestType],
'' AS BorrowerID ,
A.[TaxPayer-Title/Suffix] AS NameSuffix,
A.[TaxPayer-FirstName] AS FirstName,
'' AS MiddleName,
A.[TaxPayer-LastName] AS LastName,
B.[Spouse-FirstName] + ' ' + B.[Spouse-LastName] AS [SpouseName],
B.[Spouse-SSN] AS [SpouseSSN],
CASE
WHEN A.[Filing Status] = 'MFJ' THEN 'CoBorrower'
WHEN A.[Filing Status] = 'Single' THEN 'Borrower'
ELSE A.[Filing Status]
END AS [PrintPositionType],
CASE
WHEN B.[Spouse-FirstName] IS not null THEN 'Married'
ELSE 'Unmarried'
END AS [MaritalStatusType],
C.[Address] AS StreetAddress,
C.City AS City,
C.[State] AS State,
C.[Postal Code] AS PostalCode,
A.[Primary Contact] AS Type,
A.[TaxPayer-Home/Evening Telephone Number] AS Value,
NULL AS RequestDateTime,
GETDATE() AS CreatedDateTime,
NULL AS RequestProcessed,
NULL AS BorrowerResidencyType,
0 AS IsActive
from DimTaxPayerInfo A
LEFT join DimTaxPayerSpouseInfo B on A.[TaxPayer-SSN] = B.[TaxPayer-SSN] AND B.TaxSoftwareId = 4
LEFT join stg.stg_DimTaxPayerAddress C on A.[TaxPayer-SSN] = C.[TaxPayer-SSN]
WHERE A.[TaxPayer-SSN] != ''
) as Y
ON TT.[EngagementID] = Y.[EngagementID]
) DD
ON T.[EngagementID] = DD.[EngagementID]
WHEN MATCHED THEN
Update
SET T.TaxYear = DD.TaxYear
,T.TaxPayerSSN = dd.TaxPayerSSN
,T.EngagementID = dd.EngagementID
,T.CreditRequestType = dd.CreditRequestType
,T.BorrowerID = dd.BorrowerID
,T.NameSuffix = dd.NameSuffix
,T.FirstName = dd.FirstName
,T.MiddleName = dd.MiddleName
,T.LastName = dd.LastName
,T.SpouseName = dd.SpouseName
,T.SpouseSSN = dd.SpouseSSN
,T.PrintPositionType = dd.PrintPositionType
,T.MaritalStatusType = dd.MaritalStatusType
,T.StreetAddress = dd.StreetAddress
,T.City = dd.City
,T.State = dd.State
,T.PostalCode = dd.PostalCode
,T.Type = dd.Type
,T.Value = dd.Value
,T.RequestDateTime = dd.RequestDateTime
,T.CreatedDateTime = dd.CreatedDateTime
,T.RequestProcessed = dd.RequestProcessed
,T.BorrowerResidencyType = dd.BorrowerResidencyType
,T.IsActive = dd.IsActive
WHEN NOT MATCHED THEN
INSERT
( TaxYear
,TaxPayerSSN
,EngagementID
,CreditRequestType
,BorrowerID
,NameSuffix
,FirstName
,MiddleName
,LastName
,SpouseName
,SpouseSSN
,PrintPositionType
,MaritalStatusType
,StreetAddress
,City
,State
,PostalCode
,Type
,Value
,RequestDateTime
,CreatedDateTime
,RequestProcessed
,BorrowerResidencyType
,IsActive
)
values (
dd.TaxYear
,dd.TaxPayerSSN
,dd.EngagementID
,dd.CreditRequestType
,dd.BorrowerID
,dd.NameSuffix
,dd.FirstName
,dd.MiddleName
,dd.LastName
,dd.SpouseName
,dd.SpouseSSN
,dd.PrintPositionType
,dd.MaritalStatusType
,dd.StreetAddress
,dd.City
,dd.State
,dd.PostalCode
,dd.Type
,dd.Value
,dd.RequestDateTime
,dd.CreatedDateTime
,dd.RequestProcessed
,dd.BorrowerResidencyType
,dd.IsActive
);
Your Merge Statement looks correct. This error is mostly seen in Azure Synapse. Make sure your target table is hash distributed to avoid this error.
Refer to the answer posted in the thread for a similar error.
The parameter has two values direct reports and indirect report.
I have tried:
select distinct cch.emp_person_id,
cch.lvl_num Empl_Lvl,
cch_lv.lvl_num MGR_LVL
from cmp_cwb_hrchy cch, cmp_cwb_hrchy cch_lv
where cch.mgr_person_id = :Mgr_id and
cch_lv.emp_person_id = :Mgr_id and
cch.MGR_PERSON_EVENT_ID= cch_lv.MGR_PERSON_EVENT_ID and
cch.EMP_PERSON_EVENT_ID = cch_lv.EMP_PERSON_EVENT_ID and
cch.lvl_num = cch_lv.lvl_num and
( 1=1 and cch.lvl_num = case when :direct_ALL = 'Direct' then cch_lv.lvl_num+1 else NULL end) and
( 1=1 and cch.lvl_num > case when :direct_ALL = 'ALL' then cch_.lvl_num else NULL end)
I have the following update scrpit..
UPDATE LIMRA_Retail_Current
SET AnnualizedWOExcessPremium = (SELECT
ISNULL(ul.AnnualizedWOExcessPremium, 0)-
isnull((select iul.AnnualizedWOExcessPremium
from dbo.LIMRA_Retail_Current iul
where ul.Distribution = iul.distribution
and ul.company = iul.company
and ul.Year = iul.year
and ul.Quarter = iul.quarter
and iul.Market = 'Retail'
and replace(ul.Product, 'Universal Life', 'Index Universal Life') = iul.product
),0) UL_MINUS_IUL
FROM dbo.LIMRA_Retail_Current UL
WHERE ul.Product like 'Universal Life%'
AND ul.Market = 'Retail')
How can I update the column AnnualizedWOExcessPremium using ALL the values of UL_MINUS_IUL. I want to use ALL the values in my update.
Looks like your sub select just needs a SUM:
(SELECT
SUM(ISNULL(ul.AnnualizedWOExcessPremium, 0)- isnull((select iul.AnnualizedWOExcessPremium )
from dbo.LIMRA_Retail_Current iul
where ul.Distribution = iul.distribution
and ul.company = iul.company
and ul.Year = iul.year
and ul.Quarter = iul.quarter
and iul.Market = 'Retail'
and replace(ul.Product, 'Universal Life', 'Index Universal Life') = iul.product
),0) UL_MINUS_IUL
I'm trying to assign a NULL value when another field is NULL using a CASE statement.
So far this is what I got:
UPDATE PunchintervalFinal
SET INDTTIME_LUNCH = (SELECT CASE
WHEN PunchintervalFinal.INDTTIME2 IS NULL THEN
NULL
END
FROM PUNCHBRIDGE A
WHERE A.EMPLOYEE = PunchintervalFinal.EMPLOYEE
AND A.PUNCHDATE = PunchintervalFinal.PUNCHDATE
AND PunchintervalFinal.EMPLOYEE = '500018')
This should do what you are trying to do:
UPDATE PunchintervalFinal
SET INDTTIME_LUNCH = NULL
FROM PunchintervalFinal
INNER JOIN PUNCHBRIDGE A ON A.EMPLOYEE = PunchintervalFinal.EMPLOYEE
WHERE PunchintervalFinal.EMPLOYEE = '500018'
AND A.PUNCHDATE = PunchintervalFinal.PUNCHDATE
AND PunchintervalFinal.INDTTIME2 IS NULL