Oct 02 2024 07:56 AM
Hi All,
I have a account table which has parent acct number, Customer name, Account number.
the issue is 1 parent account number should have only one customer name even though account number is different like below
1 parent acc num // 2 customer name // 2 Accounts
1123456789 - NIKE - AAAAA1
1123456789 - NIKES -AAAAA2
but i need to update output like below for all parent account numbers which has different names,
=>> write the query to update that the data for customer name remains the same
1 parent acc num // 1 customer name // 2 Accounts
1123456789 - NIKE - AAAAA1
1123456789 - NIKE -AAAAA2
Could you please assist me to write an update quey to update 1 parent account number has 1 customer name.
Thanks in advance for your support.
Regards,
Bhaskar
Oct 02 2024 09:03 AM - edited Oct 02 2024 09:04 AM
SQL is below, you can replace the aggregate function MIN by others to get the customer name you want.
CREATE TABLE #account
(
p_acct_no varchar(10),
cust_name varchar(50),
acct_no varchar(10)
)
GO
INSERT INTO #account
VALUES
('1123456789', 'NIKE', 'AAAAA1')
,('1123456789', 'NIKES', 'AAAAA2')
GO
SELECT * FROM #account
UPDATE A
SET
A.cust_name = B.cust_name
FROM #account A
INNER JOIN
(
SELECT p_acct_no, MIN(cust_name) AS cust_name FROM #account
GROUP BY p_acct_no
)B
ON A.p_acct_no = B.p_acct_no
GO
SELECT * FROM #account
DROP TABLE #account
GO
Oct 02 2024 10:09 AM
Hi @rodgerkong,
Thanks for your support and i think the solution will work for all parent account numbers in account table which has different customer names.
i will check and update you if any further assistance needed.
Kudos for valuable assistance and support,
Regards,
Bhaskar
Oct 02 2024 11:00 AM
Oct 02 2024 07:15 PM
In result B, you have give a column alias cust_name to the function MIN. So when you referring it, you must use cust_name instead of Customer Name. @bhaskarpbi99
Oct 02 2024 09:36 PM