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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:53:19+00:00 2026-06-17T07:53:19+00:00

I have a table called items id month year **itemname** distmoney 1 12 2012

  • 0

I have a table called “items”

id       month       year       **itemname**       distmoney
 1        12         2012         chicken           20
 2        12         2012         pork              15
 3        11         2012         chicken           21
 4        11         2012         pork              15

I am trying to find the difference between the “distmoney” for the same itemname between two months.

Example:

ID 1, itemname chicken. month 12 distmoney is 20, while month 11 distmoney is 21. I want to be able to calculate the difference of 1 for id=1, itemname=chicken

Right now I have the php code to calculate the difference between two numbers, but I am having a tough time figuring out how to grab the previous month distmoney.

<?php foreach($rows as $row): ?>
<?php $number1 = htmlentities($row['distmoney']) ?>
<?php endforeach; ?>
<?php
$number1 = $row['distmoney'];
$number2 = ????????; // THIS NEEDS TO BE THE PREVIOUS MONTH DISTMONEY VALUE
if ($number1 <= $number2) {
$difference = "(Price Lowered) Price difference of $";
$result = $number2 - $number1;
$percent = round(100.0*($number2-$number1)/$number1);
echo $difference; echo $result; echo $percent; echo "%";
} elseif ($number1 > $number2) {
$result = $number1 - $number2;
$percent = round(100.0*($number2/$number1-1));
$addition = "(Price Higher) Price difference of $";
echo $addition; echo $result; echo $percent; echo "%";
} 

?>

  • 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-17T07:53:21+00:00Added an answer on June 17, 2026 at 7:53 am

    If you want to perform this in SQL, then you can transform the data in columns to then get the difference:

    select 
      itemname,
      max(case when month = 12 and year = 2012 then distmoney else 0 end) Dec2012,
      max(case when month = 11 and year = 2012 then distmoney else 0 end) Nov2012
    from yourtable
    group by itemname
    

    See SQL Fiddle with Demo

    The result of the query is:

    | ITEMNAME | DEC2012 | NOV2012 |
    --------------------------------
    |  chicken |      20 |      21 |
    |     pork |      15 |      15 |
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table called items id month year **itemname** distmoney 1 12 2012
In the table ogitems, I have a column called itemname, month, and price. There
let's say that I have a table called Items (ID int, Done int, Total
I working on a website and have a mysql-table called 'items', like: item_id |
I have a table called transactions with a many-to-many relationship to items through the
I have two table TempStore and Store with the same column called Items. There
I have a table called items. IN this table there are many columns. One
I have a table in my SQL Server database called items which has a
So I have an example table called items with the following columns: item_id (int)
I have a table called items. I want to change the description attribute (it's

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.