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 8478013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:37:38+00:00 2026-06-10T18:37:38+00:00

I have the following structure: CREATE TABLE IF NOT EXISTS `myTable` ( `ID` int(11)

  • 0

I have the following structure:

CREATE TABLE IF NOT EXISTS `myTable` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `time` int(11) NOT NULL,
  `item` varchar(255) NOT NULL,
  `price` int(11) NOT NULL,
  PRIMARY KEY (`ID`),
  KEY `time` (`time`),
  KEY `item` (`item`),
  KEY `price` (`price`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=8 ;


INSERT INTO `myTable` (`ID`, `time`, `item`, `price`) VALUES
(1, 100, 'cat', 10),
(2, 101, 'cat', 10),
(3, 102, 'cat', 9),
(4, 103, 'cat', 11),
(5, 104, 'cat', 10),
(6, 105, 'cat', 10),
(7, 106, 'cat', 11);

I’m trying to figure out a query that would return the following:

time   item   price
----   ----   -----
100    cat    10
102    cat    9
103    cat    11
104    cat    10
106    cat    11

It’s ordered by time, and shows only rows where the price is different to the price before it (Edit: *According to time*).

EDIT:

Maybe I made the original problem too simple, what if it was more complex with data like this:

INSERT INTO `myTable` (`ID`, `time`, `item`, `price`) VALUES
(1111, 100, 'cat', 10),
(21, 105, 'cat', 10),
(31, 108, 'cat', 9),
(411, 109, 'cat', 11),
(512, 110, 'cat', 10),
(61, 114, 'cat', 10),
(72, 120, 'cat', 11),
(8, 100, 'dog', 20),
(93, 102, 'dog', 20),
(104, 108, 'dog', 29),
(111, 109, 'dog', 31),
(122, 155, 'dog', 20),
(13, 165, 'dog', 20),
(1445, 172, 'dog', 31);

In the above, the ID column is all messed up, perhaps there are other records in between. Also the time no longer increments by 1 and I’ve thrown a dog into the mix.

  • 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-10T18:37:40+00:00Added an answer on June 10, 2026 at 6:37 pm

    The query you need is this:

    select curr.time  ,
           curr.item  ,
           curr.price 
    from myTable curr
    join myTable prev on prev.time = ( select max(time)
                                       from myTable p
                                       where p.time < curr.time
                                     )
    where curr.price != prev.price
    order by t.time
    

    It takes advantage of the fact that time is an alternate key for the table. We take all the rows from myTable (let’s call it current) such that its price is different than that of the previous row based on the the value of the alternate key time (lets call this set previous).

    To do this, we need a correlated subquery that identifies the highest value of time less than or equal to the value of of time for the current row. So…we wind up referencing myTable thrice:

    • First, for the set of ‘current’ rows,
    • Second, for the set of ‘previous’ rows’
    • Third, to identify the ‘previous’ row based on the data in the ‘current’ row.

    Easy!

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

Sidebar

Related Questions

I have the following table structure: CREATE TABLE [Report].[MesReport]( [MesReportID] [int] IDENTITY(1,1) NOT NULL,
I have the following table structure CREATE TABLE `table` ( `id` int(11) NOT NULL
I have the following structure SET SQL_MODE=NO_AUTO_VALUE_ON_ZERO; CREATE TABLE IF NOT EXISTS `sis_param_tax` (
Given the following table structure: CREATE TABLE IF NOT EXISTS `roles` ( `id` int(11)
I have following table structure: CREATE TABLE pilot_groups ( id INT PK, name VARCHAR(50),
I have the following table structure: CREATE TABLE a ( a_id int(10) unsigned NOT
I have DB2 table having following structure CREATE TABLE DUMMY ( ID CHARACTER(10) NOT
Lets say I have the following MySQL structure: CREATE TABLE `domains` ( `id` INT(10)
I have the following data structure and data: CREATE TABLE `parent` ( `id` int(11)
I have created a table with the following structure- $sql = CREATE TABLE followers

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.