where condition in merge condition

Shambhu Rai 1,411 Reputation points
2023-12-08T06:58:46.8633333+00:00

Hi Expert,

how to add where condition in merge condition in databricks below

where at. Salesman2 = 'CVCLBS'

merge into Authority at using customer c

on (at.CustomerId = c.CustomerId )

when (at.OpuCode='D' and at.issuerNumber=700669) or (at.OpuCode='A' and at.CustomerId = c.CustomerId) then 'KO' when LEN(TRIM (at.cardOffertype))=0 then 'ZZ' else at.cardoffertype end)

when matched then update

set cardoffertype=(case

when (at.OpuCode='D' and at.issuerNumber=700669) or (at.OpuCode='A' and at.CustomerId = c.CustomerId) then 'KO'

when LEN(TRIM(at.cardOffertype))=0 then 'ZZ'

else at.cardoffertype 

end)

Azure Data Lake Storage
Azure Data Lake Storage
An Azure service that provides an enterprise-wide hyper-scale repository for big data analytic workloads and is integrated with Azure Blob Storage.
Azure Databricks
Azure Databricks
An Apache Spark-based analytics platform optimized for Azure.
Azure Data Factory
Azure Data Factory
An Azure service for ingesting, preparing, and transforming data at scale.
0 comments No comments
{count} votes

Answer accepted by question author
  1. Wilko van de Velde 2,241 Reputation points
    2023-12-08T07:37:30.0966667+00:00

    Hi @Shambhu Rai ,

    I think it would be something like:

    MERGE INTO Authority as at USING customer as c
      ON (at.CustomerId = c.CustomerId )
      WHEN MATCHED AND at.Salesman2 = 'CVCLBS' THEN UPDATE SET 
       cardoffertype=(case
        when (at.OpuCode='D' and at.issuerNumber=700669) or (at.OpuCode='A') then 'KO'
        when LEN(TRIM(at.cardOffertype))=0 then 'ZZ'
        else at.cardoffertype 
    end)
    

    More info about merge:
    https://docs.databricks.com/en/sql/language-manual/delta-merge-into.html

    Kind Regards,

    Wilko


    Please do not forget to "Accept the answer” wherever the information provided helps you, this can be beneficial to other community members. If you have extra questions about this answer, please click "Comment".


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.