Sql-server – Select images where ALL images are greater than pixel width

group bysql server

I will try to explain this clearly.

I have a table of 200,000 records that stores pixel sizes for our product images. A variety of image sizes have been saved over 20 years, and products do not necessarily have images in all folders.

I am trying to find out how many products have all of their images over 800 pixels wide to inform design decisions for a new website.

The fields are:

  • ID (unique reference of product)
  • ImgNo (id of each image under product: 1 to 36)
  • CDN_W (width of image on CDN storage)
  • LG_W (width of image in LARGE folder)
  • LGR_W (width of image in LARGER folder)

Data looks like this:

  • ID 123
  • ImgNo 1
  • CDN_W 800
  • LG_W 400
  • LGR_W 1280

  • ID 123
  • ImgNo 2
  • CDN_W 600
  • LG_W 600
  • LGR_W 1280

  • ID ABC
  • ImgNo 1
  • CDN_W null
  • LG_W 800
  • LGR_W null

etc…


THE TASK

I need to query the data to find all products where ALL of the images in a particular folder are over 800 pixels wide. There are 3 folders, any of which could provide the best set of images over 800 pixels. I only want products where all images are over 800 – if there are 5 images and 1 is below 800 then that product should not be valid.

Ideally I'd like to identify the TOTAL number of products that are valid, and separately the actual list of products IDs.

Is there a way to do this in MS SQL?

Best Answer

SELECT id
FROM table
GROUP BY id
HAVING MIN(CDN_W) > 800
   AND MIN(LG_W) > 800
   AND MIN(LGR_W) > 800

?