-1

I'm facing difficulty in generating top below table, using following data, please help!

text | date
-----------------
aa   | 25-08-2017
aa   | 26-08-2017
aa   | 26-08-2017
aa   | 26-08-2017
aa   | 27-08-2017
ab   | 25-08-2017
ab   | 25-08-2017
bb   | 25-08-2017
bb   | 26-08-2017
bb   | 26-08-2017
bb   | 26-08-2017

I need report like,

text | days
-----------
aa   | 3
bb   | 2
ab   | 1
Venkatesh
  • 1
  • 1

2 Answers2

-1
SELECT CASE WHEN t.ct = 1 THEN concat(t.text,  ' count ', t.ct, ' day') 
            ELSE concat(t.text,  ' days count ', t.ct) 
       END
FROM (
   SELECT text, count(date) AS ct
   FROM your_table 
   GROUP BY text
) t
Radim Bača
  • 10,646
  • 1
  • 19
  • 33
  • Maybe it should be `count(distinct date)` – sagi Oct 03 '17 at 11:31
  • Thank you for this code snippet, which might provide some limited, immediate help. A proper explanation [would greatly improve](//meta.stackexchange.com/q/114762) its long-term value by showing *why* this is a good solution to the problem, and would make it more useful to future readers with other, similar questions. Please [edit] your answer to add some explanation, including the assumptions you've made. – Toby Speight Oct 03 '17 at 12:33
-1

I recommend you using the fonction timediff() https://www.w3resource.com/mysql/date-and-time-functions/mysql-timediff-function.php

  • 1) The OP is not asking about time difference. 2) You can't recommend specific functions unless you know the SQL engine the OP uses. 3) If you link, link to credible sources, preferably the original documentation. `w3resource` is not a credible source. – Tomalak Oct 03 '17 at 11:33