Return Counts over past 30 days, even if number is zero

Here is what I have so far.

SELECT COUNT(*) AS totalViews, create_dt
FROM bc_listing_views
WHERE member_id = 1
AND create_dt > ADDDATE( CURDATE( ) , INTERVAL -30 DAY ) 
GROUP BY EXTRACT(DAY FROM create_dt)

Open in new window


i would like it to run the past 30 days, even if the count is zero.  Is this possible?  Using mySQL 5.5
theideabulbAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Terry WoodsIT GuruCommented:
If you create an integers table, it makes it pretty easy. I'm assuming you want a count by day of listing views.

CREATE TABLE IF NOT EXISTS `integers` (
  `num` int(11) NOT NULL auto_increment,
  PRIMARY KEY  (`num`)
) COMMENT='Table of integers';

INSERT INTO `integers` (`num`) VALUES
(1),
(2),
(3),
(4),
(5),
(6),
(7),
(8),
(9),
(10),
(11),
(12),
(13),
(14),
(15),
(16),
(17),
(18),
(19),
(20),
(21),
(22),
(23),
(24),
(25),
(26),
(27),
(28),
(29),
(30),
(31),
(32);

select adddate(curdate(), interval -(num-1) day) as my_date,
  (select count(*) FROM bc_listing_views
  WHERE member_id = 1 
  and create_dt = adddate(curdate(), interval -(num-1) day)) as view_count
FROM integers 
WHERE num  between 1 and 31
order by num asc

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
theideabulbAuthor Commented:
Excellent.  I changed it to this because I was getting zeros for everything. I guess it was the formatting of the dates.

select adddate(curdate(), interval -(num-1) day) as my_date,
  (select count(*) FROM bc_listing_views
  WHERE member_id = 1
  and DATE_FORMAT(create_dt,'%y-%m-%d') = adddate(curdate(), interval -(num-1) day)) as view_count
FROM integers
WHERE num  between 1 and 30
order by num asc
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
MySQL Server

From novice to tech pro — start learning today.