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

  • SEARCH
  • Home
  • 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 7643849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:32:49+00:00 2026-05-31T09:32:49+00:00

I have this database table ‘market’ id market_id shows clicks event data 1 158

  • 0

I have this database table ‘market’

id       market_id       shows       clicks        event       data
 1          158          1000         300           on       2012-03-01
 2          158          1500         600           off      2012-03-14
 3          158          1500         600           on       2012-03-14
 4          158          2500         700           off      2012-03-20            

I want to select it and get array like this:

$array  = array (
            array('from 2012-03-01 to 2012-03-14' => array('shows' => 500, 'clicks' => 300)
            array('from 2012-03-14 to 2012-03-20' => array('shows' => 1000, 'clicks' => 100)  

How to do that? Have I do that in Mysql or have I do that in php?

edit:

‘shows’ in array calculates like that 1500 – 1000, 1500 is from table where ‘event’ is off and 1000 is from table where ‘event’ is on, click’s calculate same.

  • 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-05-31T09:32:50+00:00Added an answer on May 31, 2026 at 9:32 am

    Caveat: I can’t really work out what your intention is and hence there may be much better solutions that this but…
    Here’s a query that works on your sample data.

    SELECT
        t2.data as from_date,
        t1.data as to_date,
        (t1.shows-t2.shows) as shows,
        (t1.clicks - t2.clicks) as clicks
    FROM `market` t1
        inner join market t2 
            on (t1.market_id=t2.market_id and t1.event='off' and t2.event='on' and t1.data>t2.data)
        left join market t3 
            on (t1.data>t3.data and t3.event='on' and t3.data>t2.data)
    WHERE t3.id is null
    

    To explain a little bit: the query joins the table onto it self to find all pairs of rows where one represents an “off” event that is later than an “on” event. This picks up all pairs (even 2012-03-20 ‘off’ and 2012-03-01 ‘on’) so then we do a LEFT JOIN again to check there are no other ‘on’ events between the ‘on’ row and the ‘off’ row.

    btw, I think there’s an error in your sample results. The values for “shows” in your second result should be 1000.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this database table that contains emails like 300 records, emails{email,name,join_date); and I
I have this data in database: table: games_related game_id1 | game_id2 3 | 2
i have database table like this +-------+--------------+----------+ | id | ip | date |
I have a database table using an enum. This is already working with hibernate
So I have this table in my database Item Cat1 Cat2 -------------------- Aaa Red
I have written a function to print database table to an array like this
I always forget how to do things like this. I have a database table
I have a table like this in my database (SQL Server 2008) ID Type
I have a table in my postgresql 8.4 database like this: id(serial), event_type_id(id, foreign
I have created a table on an Oracle 10g database with this structure :

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.