sql - Delete all but 3 images for any product -


i have table products mapping pictures. there products have 1 n mappings pictures. view mappings nicely i've build sql can view them grouped productid.

 select [id] ,[productid] ,[pictureid] ,[displayorder] [landogfritid23].[dbo].[product_picture_mapping] productid in (select productid [landogfritid23].[dbo].product p, [landogfritid23].[dbo].[product_picture_mapping] pm p.id = pm.productid group productid having count(pm.pictureid) > 3 ) order productid 

the result:

id productid pictureid displayorder 2085 103 2388 2 2185 103 2488 1 7132 103 7468 1 7133 103 7469 1 2158 107 2461 0 320 107 415 1 3485 107 3816 1 3486 107 3817 1 3529 107 3860 1 

now, request let 3 pictures max each product. find hard create delete statement deleting row 4th, 5th, nth 1 mapping product.

do have ideea if possible?

so results after deleting, data be:

id productid pictureid displayorder 2085 103 2388 2 2185 103 2488 1 7132 103 7468 1 2158 107 2461 0 320 107 415 1 3485 107 3816 1 

thanks

using cte:

note: assuming want keep latest 3 pictures

with data ( select *, row_number() over(partition productid order pictureid desc) position [landogfritid23].[dbo].[product_picture_mapping] ) delete data position > 3 

Comments

Popular posts from this blog

javascript - backbone.js Collection.add() doesn't `construct` (`initialize`) an object -

php - Get uncommon values from two or more arrays -

Adding duplicate array rows in Php -