question

billj avatar image
billj asked

How to run a query to manage duplicate rows

Hello, **First create the table.** DECLARE @TEST_Table table (Cat nvarchar(20), C1 nvarchar(20), C2 numeric(19, 2), C3 numeric(19, 2)) INSERT INTO @TEST_Table VALUES (22,'Apple',2,35),(28,NULL,1,11.25), (37,'Apple',1,18.5), (66,'Apple',1,38.5), (66,'Apple',3,38.5), (88,NULL,10,8), (88,NULL,50,8), (99,'Apple',1,74.5), (99,'Apple',2,74.5) **Then run this query.** select Cat, CASE WHEN C1 = 'Apple' THEN 'Y' ELSE 'N' END C1, CASE WHEN C1 = 'Apple' THEN Convert(varchar(5), C2, 2) ELSE '--' END C2 , C3 , C2/2 AS C4 FROM @TEST_Table Group By Cat, C1, C2, C3 ![alt text][1] Than you for your help. [1]: /storage/temp/3961-result6.png
sql-server-2012
result6.png (11.3 KiB)
result6.png (11.1 KiB)
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Kev Riley avatar image
Kev Riley answered
Try this... select Cat , case when C1 = 'Apple' then 'Y' else 'N' end C1 , case when C1 = 'Apple' then convert(varchar(5), (sum(C2)), 2) else '--' end C2, C3 , sum(C2 / 2) as C4 from @TEST_Table group by Cat, C1, C3
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

billj avatar image billj commented ·
Hi Kev, Your query works fine. Thanks for your help.
0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.