Posts

Showing posts from 2021

MySQL Get all categories using Cross join

--  Each category with each status will be retrieved using CROSS JOIN (between categories and statuses without any join condition), then the LEFT JOIN for comparing and calculation the actual complaint counts.  select c.name as category, ss.name as status, Count(aa.complaint_id) as ComplaintCount from categories c cross join statuses ss left join ( select cc.id as complaint_id, cc.category_id, cc.status_id,cc.consulate_office_id from  complaints cc ) aa on aa.category_id=c.id and aa.status_id=ss.id -- and aa.consulate_office_id=12   group by c.name ,ss.name   

MySQL Calendar Generation Between Two Dates

 -- Creation of datedim  table  CREATE TABLE IF NOT EXISTS datedim  (     date_id INT NOT NULL auto_increment,     fulldate date,     dayofmonth int,     dayofyear int,     dayofweek int,     dayname varchar(10),     monthnumber int,     monthname varchar(10),     year    int,     quarter tinyint,     PRIMARY KEY(date_id) ) ENGINE=InnoDB AUTO_INCREMENT=1000; -- Procedure that will insert data into datedim table. -- delimiter // -- DROP PROCEDURE IF EXISTS datedimbuild; -- CREATE PROCEDURE datedimbuild (p_start_date DATE, p_end_date DATE) -- BEGIN --     DECLARE v_full_date DATE; --     DELETE FROM datedim; --     SET v_full_date = p_start_date; --     WHILE v_full_date < p_end_date DO --         INSERT INTO datedim ( --         ...