سؤال

I have two columns

product    productactual

and the following data:

shoes        NULL
slippers     NULL
shoes        sandals
slippers     NULL
sandals      shoes

I have to count the Number of products I am using Count (coalesce(Productactual,product)) but its not working any changes in query or new idea to count products in two columns.

هل كانت مفيدة؟

المحلول

If you want to count entries per product, you need to use your COALESCE expression as a grouping item:

SELECT
  COALESCE(productactual, product) AS product,
  COUNT(*) AS productcount
FROM atable
GROUP BY
  COALESCE(productactual, product)
;
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top