Use BIT_COUNT function : BIT Count : Procedure Function SQL / MySQL


SQL / MySQL » Procedure Function » BIT Count »

 

Use BIT_COUNT function



/*
mysql> /*  how many different days in each month these visits occur
mysql>  */
mysql> SELECT year, month, BIT_COUNT(BIT_OR(1<<day)) AS days FROM timeTable
    ->        GROUP BY year,month;
+------+-------+------+
| year | month | days |
+------+-------+------+
2001 |    01 |    |
2002 |    06 |    |
2003 |    05 |    |
2004 |    02 |    |
2005 |    04 |    |
2006 |    03 |    |
+------+-------+------+
rows in set (0.02 sec)

*/  
Drop table timeTable;

CREATE TABLE timeTable (
    year YEAR(4)
    month INT(2UNSIGNED ZEROFILL,
    day INT(2UNSIGNED ZEROFILL
);

INSERT INTO timeTable VALUES(2001,1,1),
                            (2002,6,20),
                            (2003,5,30),
                            (2004,2,2),
                            (2005,4,23),
                            (2006,3,23);

/*  how many different days in each month these visits occur
 */

SELECT year, month, BIT_COUNT(BIT_OR(1<<day)) AS days FROM timeTable
       GROUP BY year,month;

           
       



Leave a Comment / Note


 
Verification is used to prevent unwanted posts (spam). .

Follow Navioo On Twitter

SQL / MySQL

 Navioo Procedure Function
» BIT Count