Get count on two different date columns and group by date

非 Y 不嫁゛ 提交于 2020-12-12 07:05:33

问题


I have table containing two DATE columns. TS_customer and TS_verified

I am searching for a way to get a result where in the first column I have dates where either someone created a user (TS_customer) or someone got verified (TS_verified).

In the second column I want count(TS_customer) grouped by the first column. The third column I want count(TS_verified) grouped by the first column.

It might be 0 customers verified on a sign up date, and in another case 0 signups on a date someone got verified.

I guess it should be an easy one, but I've spent so many hours on it now. Would really appreciate some help. I need this for a graph in excel, so i basicly want how many customers signed up and how many got verified one day without having the hassle to have two selects and combinding them manually.

EDIT: link to SQLfiddle http://sqlfiddle.com/#!2/b14fc/1/0

Thanks


回答1:


First, we need the list of days.

That looks like this http://sqlfiddle.com/#!2/b14fc/14/0:

   SELECT DISTINCT days
     FROM (
       SELECT DISTINCT DATE(TS_customer) days
         FROM customer
        UNION 
       SELECT DISTINCT DATE(TS_verified) days
         FROM customer

     ) AS alldays
 WHERE days IS NOT NULL
 ORDER BY days

Next we need a summary of customer counts by day. That's pretty easy http://sqlfiddle.com/#!2/b14fc/16/0:

SELECT DATE(TS_customer) days, COUNT(TS_customer)
  FROM customer
 GROUP BY days

The summary of verifications by day is similarly easy.

Next we need to join these three subqueries together http://sqlfiddle.com/#!2/b14fc/29/0.

SELECT alldays.days, custcount, verifycount
  FROM (
           SELECT DISTINCT DATE(TS_customer) days
             FROM customer
            UNION 
           SELECT DISTINCT DATE(TS_verified) days
             FROM customer
       ) AS alldays
   LEFT JOIN (
      SELECT DATE(TS_customer) days, COUNT(TS_customer) custcount
        FROM customer
       GROUP BY days
      ) AS cust ON alldays.days = cust.days
   LEFT JOIN (
      SELECT DATE(TS_verified) days, COUNT(TS_verified) verifycount
        FROM customer
       GROUP BY days
      ) AS verif ON alldays.days = verif.days
  WHERE alldays.days IS NOT NULL
  ORDER BY alldays.days

Finally, if you want 0 displayed rather than (null) for days when there weren't any customers and/or verifications, change the SELECT line to this http://sqlfiddle.com/#!2/b14fc/30/0.

SELECT alldays.days, 
       IFNULL(custcount,0) AS custcount, 
       IFNULL(verifycount,0) AS verifycount

See how that goes? We build up your result set step by step.




回答2:


I'm a bit confused on why you created a fiddle that can not hold null values on the TS_Customer and then mention that the field can hold null values.

Having said that, I've modified the solution to work with null values and still be pretty efficient and simple:

SELECT days, sum(custCount) custCount, sum(verifCount) verifCount FROM (
  SELECT DATE(TS_customer) days, count(*) custCount, 0 verifCount
  FROM customer
  WHERE TS_customer IS NOT NULL
  GROUP BY days
  UNION ALL
  SELECT DATE(TS_verified) days, 0, count(*)
  FROM customer
  WHERE TS_verified IS NOT NULL
  GROUP BY days
) s
GROUP BY days

I've also created a different fiddle containing some null values here.



来源:https://stackoverflow.com/questions/19613434/get-count-on-two-different-date-columns-and-group-by-date

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!