Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Get certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now

Reply
sabrinekalbousi
New Member

Update a column in SQL based on field frequency

Hello SQL query  team ,

 

I need to create a dimension in my database ( to create later a report in Power BI) and I am struggling with the stored procedure. In fact, I need to have the following table dim_table as a result.

passport numberpassport_expiry_dateis_valid_expiry_dateconsolidated_expiry_date
ALJDLJZ7/10/202017/10/2020
ALJDLJZ7/10/202017/10/2020
ALJDLJZNULL17/10/2020
ALJDLJZ8/10/202117/10/2020

DONE: I have already the passport numbers and passport expiry dates in the table and and I have updated the columns 'is_valid_expiry_date' to consider NULL and dates in the future as valid..

TO BE DONE: I need to update the consolidated column based on these rules:

- we consider just records with is_valid_expiry_date=1

-if count( distinct(passport_expiry_date))=1 (just one valid date)then, this latter should be in each line in the consolitaed column.

- if we have different dates ( as in our example), we need to take the most frequent one (7/10/2020 (repeated twice)) and put it in each line as a consolidated value.

 

--------------------------

update t 

[consolidated_passport_expiry_date]=

CASE WHEN count(distinct [passport_exipiry_date])=1 OR count(distinct [passport_exipiry_date])=0 THEN t.passport_exipiry_date

          WHEN.........................

           WHEN..................

ELSE

END

FROM dim_table t
where t.passport_expiry_dateis_valid_expiry_date=1 
GROUP BY t.[passport_number], [passport_exipiry_date]

------------------------

 

 

Thanks in advance for any help or tips to complete this update section.

Sabrina

 

1 ACCEPTED SOLUTION
lbendlin
Super User
Super User

That's not a valid SQL syntax

 

UPDATE table

SET field= CASE WHEN THEN ELSE END

WHERE <filter condition>

 

The FROM statement is only needed when you update one table's columns from another related table's content.

View solution in original post

2 REPLIES 2
v-xuding-msft
Community Support
Community Support

Hi @sabrinekalbousi ,

 

If you think lbendlin's answer is helpful, please accept it as a solution. And if you still need help, it is better to get dedicated support from the database forum.

 

Best Regards,
Xue Ding
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
lbendlin
Super User
Super User

That's not a valid SQL syntax

 

UPDATE table

SET field= CASE WHEN THEN ELSE END

WHERE <filter condition>

 

The FROM statement is only needed when you update one table's columns from another related table's content.

Helpful resources

Announcements
November Carousel

Fabric Community Update - November 2024

Find out what's new and trending in the Fabric Community.

Live Sessions with Fabric DB

Be one of the first to start using Fabric Databases

Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.

Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

Check out the November 2024 Power BI update to learn about new features.