SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Evangelist cms9651's Avatar
    Join Date
    Mar 2010
    Posts
    407
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    how to count rows of week

    hello.

    i need count rows of week and tried this query with correct output:
    Code:
    mysql> SELECT
    	zn,
    	COUNT(*) AS q,
    	CURRENT_DATE () AS today,
    	DATE_SUB(
    		CURRENT_DATE (),
    		INTERVAL 1 WEEK
    	) AS WEEK
    FROM
    	tbl_cc80
    WHERE
    	1
    AND Day_of_the_event_s BETWEEN DATE_SUB(
    	CURRENT_DATE (),
    	INTERVAL 1 WEEK
    )
    AND CURRENT_DATE ()
    GROUP BY
    	Zn,
    	DATE_SUB(
    		CURRENT_DATE (),
    		INTERVAL 1 WEEK
    	)
    ORDER BY
    	Day_of_the_event_s DESC;
    +------------------+----+------------+------------+
    | zn               | q  | today      | WEEK       |
    +------------------+----+------------+------------+
    | LIXXX            | 31 | 2013-01-10 | 2013-01-03 |
    +------------------+----+------------+------------+
    1 rows in set
    Now i tried this other query version, but the output is wrong: in my table i have 31 rows for Zn equal to LIXXX, why this query extract 51 rows?
    Can you help me?
    thank you.
    Code:
    mysql> SELECT
            zn,
    	CURRENT_DATE () AS today,
    	DATE_SUB(
    		CURRENT_DATE (),
    		INTERVAL 1 WEEK
    	) AS WEEK,
    	COUNT(
    		IF (
    			Day_of_the_event_s BETWEEN DATE_SUB(
    				CURRENT_DATE (),
    				INTERVAL 1 WEEK
    			)
    			AND CURRENT_DATE (),
    			1,
    			0
    		)
    	) AS `output`
    FROM
    	tbl_cc80
    WHERE
    	1
    GROUP BY
    	Zn,
    	DATE_SUB(
    		CURRENT_DATE (),
    		INTERVAL 1 WEEK
    	)
    ORDER BY
    	Day_of_the_event_s DESC;
    +------------------+------------+------------+--------+
    | zn               | today      | WEEK       | output |
    +------------------+------------+------------+--------+
    | LIXXX            | 2013-01-10 | 2013-01-03 |     51 |
    +------------------+------------+------------+--------+
    1 rows in set
    Thanks you very much for your help
    I'm really happy for your quickly answer.
    Good bye

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,015
    Mentioned
    53 Post(s)
    Tagged
    2 Thread(s)
    your IF function decides whether to assign a 1 or a 0 based on the day, right?

    so you have a bunch of 1's and 0's, and then you count them all

    so no matter whether it's a 1 or a 0, it'll get counted

    does this make sense?
    r937.com | rudy.ca | Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    SitePoint Evangelist cms9651's Avatar
    Join Date
    Mar 2010
    Posts
    407
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    no it no sense it's true... You have right...
    Changed this:
    Code:
    	
    COUNT(
    		IF (
    			Day_of_the_event_s BETWEEN DATE_SUB(
    				CURRENT_DATE (),
    				INTERVAL 1 WEEK
    			)
    			AND CURRENT_DATE (),
    			1,
    			0
    		)
    	) AS `output`
    to:
    Code:
    	COUNT(
    		CASE
    		WHEN Day_of_the_event_s BETWEEN DATE_SUB(
    			CURRENT_DATE (),
    			INTERVAL 1 WEEK
    		)
    		AND CURRENT_DATE () THEN
    			1
    		ELSE
    			NULL
    		END
    	) AS `output`
    Now the output is correct.
    Thanks, I've learnt something today!
    Thanks you very much for your help
    I'm really happy for your quickly answer.
    Good bye

  4. #4
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    or to stick the horse's nose in it; you've confused COUNT and SUM.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •