SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Thread: Join queries

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

    Join queries

    hi there and happy new year.

    I need join two queries for output only, it's possible?... I tried with syntax CASE WHEN but I fail... can you help me?
    thank you.
    Code:
    SELECT
    	sum(visitorcount) AS total
    FROM
    	tbl_session;
    
    SELECT
    	SUM(visitorcount) AS tot_CURRENT_MONTH,
    	MONTH (visitdate) AS MONTH
    FROM
    	tbl_session
    WHERE
    	MONTH (visitdate) = MONTH (CURRENT_DATE())
    AND YEAR (visitdate) = YEAR (CURRENT_DATE())
    GROUP BY
    	MONTH (visitdate);

  2. #2
    SitePoint Evangelist cms9651's Avatar
    Join Date
    Mar 2010
    Posts
    407
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    this solution working, it's correct?
    Code:
    SELECT
    	SUM(
    		CASE
    		WHEN visitorcount THEN
    			visitorcount
    		END
    	) AS total,
    	SUM(
    		CASE
    		WHEN MONTH (visitdate) = MONTH (CURRENT_DATE())
    		AND YEAR (visitdate) = YEAR (CURRENT_DATE()) THEN
    			visitorcount
    		END
    	) AS tot_CURRENT_MONTH
    FROM
    	tbl_session;

  3. #3
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,408
    Mentioned
    149 Post(s)
    Tagged
    4 Thread(s)
    You don't need the CASE in the first SUM, and I'd put an ELSE 0 in the second CASE:
    Code:
    SELECT
        SUM(visitorcount) AS total
      , SUM(
          CASE
            WHEN MONTH (visitdate) = MONTH (CURRENT_DATE()) AND
                 YEAR (visitdate) = YEAR (CURRENT_DATE()) 
              THEN visitorcount
            ELSE 0
          END
        ) AS tot_CURRENT_MONTH
    FROM
    	tbl_session;

  4. #4
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,021
    Mentioned
    53 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by guido2004 View Post
    ...I'd put an ELSE 0 in the second CASE:
    i wouldn't
    r937.com | rudy.ca | Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  5. #5
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,408
    Mentioned
    149 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by r937 View Post
    i wouldn't

  6. #6
    SitePoint Evangelist cms9651's Avatar
    Join Date
    Mar 2010
    Posts
    407
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    thank you very much and happy new year!
    Thanks you very much for your help
    I'm really happy for your quickly answer.
    Good bye


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
  •