Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8319233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T22:15:30+00:00 2026-06-08T22:15:30+00:00

I’m trying to compare two values in the same table, and check if there

  • 0

I’m trying to compare two values in the same table, and check if there is a difference.
Right now, I have 1485 records in the cms_statistics_pages table, but when the query below:

SELECT
    cp.identifier,
    COUNT(csp1.statID) AS hits,
    COUNT(csp2.statID) AS hits_yesterday,
    IF(COUNT(csp1.statID)>COUNT(csp2.statID),1,0) AS growth
FROM cms_pages cp
LEFT JOIN cms_statistics_pages csp1
      ON csp1.pageID = cp.pageID
      AND DATE(csp1.datetime) = '2012-07-20'
LEFT JOIN cms_statistics_pages csp2
      ON csp2.pageID = cp.pageID
      AND DATE(csp2.datetime) = '2012-07-19'
GROUP BY cp.identifier

..is fired, I get these results:

identifier                  hits    hits_yesterday  growth
index                       13395   13395           0
siden-er-under-opdatering   638     638             0
vores-historie              0       3               0

Which is not correct for my purpose. Then if I change:

AND DATE(csp1.datetime) = '2012-07-20'

to a date that will match no records

AND DATE(csp1.datetime) = '2012-07-21'

My result now looks like this:

identifier                  hits   hits_yesterday   growth
index                       0      141              0
siden-er-under-opdatering   0      29               0
vores-historie              0      3                0

Now the hits are correct, so I’m wondering if the query counts the records multiple times when both the joins contains some data.

Example data from cms_pages:


    pageID  sectionID   templateID  identifier  default title exclude_title 
    1       1       1           index   1       Welcome to SiteTech Framework 2012

Example data from cms_statistics_pages:


    statID  frontend    backend     pageID  sectionID   panel   datetime
    1       0       1       34      6       admin   2012-07-17 12:34:14

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T22:15:32+00:00Added an answer on June 8, 2026 at 10:15 pm

    So I’ve been messing around a bit with the query, and found a solution which includes left joins and sub queries. My query now looks like:

    SELECT 
        cp.identifier,
        now.hits AS hits,
        yd.hits AS hits_yesterday,
        IF(now.hits>yd.hits,1,0) AS growth
    FROM cms_pages AS cp
    LEFT JOIN 
        (
            SELECT
                pageID,
                COUNT(pageID) AS hits
            FROM cms_statistics_pages
            WHERE DATE(datetime) = '2012-07-20'
            GROUP BY pageID
        ) now
    ON now.pageID = cp.pageID
    LEFT JOIN
        (
            SELECT  pageID,
                COUNT(pageID) AS hits
            FROM cms_statistics_pages
            WHERE DATE(datetime) = '2012-07-19'
            GROUP BY pageID
        ) yd
    ON yd.pageID = cp.pageID
    

    Which gave me this correct result!:

    identifier                  hits    hits_yesterday  growth
    index                       95      141             0
    siden-er-under-opdatering   22      29              0
    vores-historie              NULL    3               0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this is what i have right now Drawing an RSS feed into the php,
I have two tables with like below codes: Table: Accounts id | username |
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to loop through a bunch of documents I have to put
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am trying to render a haml file in a javascript response like so:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.