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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:42:29+00:00 2026-05-27T23:42:29+00:00

I cannot seem to get this MySQL query right. My table contains yearly inventory

  • 0

I cannot seem to get this MySQL query right. My table contains yearly inventory data for retail stores. Here’s the table schema:

CREATE TABLE IF NOT EXISTS `inventory_data` (
inventory_id int unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY,
store_id smallint unsigned NOT NULL,
inventory_year smallint unsigned NOT NULL,
shortage_dollars decimal(10,2) unsigned NOT NULL
)
engine=INNODB;

Every store is assigned to a district which in this table (some non-relevant fields removed):

CREATE TABLE IF NOT EXISTS `stores` (
store_id smallint unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY,
district_id smallint unsigned not null
)
engine=INNODB;

I want to be able to retrieve the shortage dollar amounts for two given years for all the stores within a given district. Inventory data for each store is only added to the inventory_data table when the inventory is completed, so not all stores within a district will all be represented all the time.

This query works to return inventory data for all stores within a given district for a given year (ex: stores in district 1 for 2012):

SELECT stores.store_id, inventory_data.shortage_dollars 
FROM stores 
LEFT JOIN inventory_data ON (stores.store_id = inventory_data.store_id) 
AND inventory_data.inventory_year = 2012 
WHERE stores.district_id = 1

But, I need to be able to get data for stores within a district for two years, such that the data looks something close to this:

store_id | yr2011 | yr2012
  • 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-27T23:42:29+00:00Added an answer on May 27, 2026 at 11:42 pm

    For the specific result format that you need, you may try the following query:

    SELECT `s`.`store_id`, `i`.`shortage_dollars` AS `yr2011`, `i1`.`shortage_dollars` AS `yr2012`
    FROM `stores` `s`
    LEFT JOIN `inventory_data` `i` ON `s`.`store_id` = `i`.`store_id`
        AND `i`.`inventory_year` = 2011
    LEFT JOIN `inventory_data` `i1` ON `s`.`store_id` = `i1`.`store_id`
        AND `i1`.`inventory_year` = 2012
    WHERE `s`.`district_id` = 1
    

    Alternatively, you may as well try the next simpler query.

    SELECT `s`.`store_id`, `i`.`inventory_year`, `i`.`shortage_dollars`
    FROM `stores` `s`
    LEFT JOIN `inventory_data` `i` ON `s`.`store_id` = `i`.`store_id`
    WHERE `s`.`district_id` = 1
    AND `i`.`inventory_year` IN (2011, 2012)
    ORDER BY `s`.`store_id`, `i`.`inventory_year`
    

    Hope it helps!

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

Sidebar

Related Questions

I cannot seem to get this query displayed properly. Here is the code: $select_stats
I have this design which I cannot seem to get right, I would like
I cannot seem to get this right, I am trying to modify a field
This is a very old problem, but I cannot seem to get my head
I need a function, but cannot seem to get it quite right, I have
I am trying to get this code to work but cannot seem to get
I know I must be missing something here, but I cannot seem to get
I cannot seem to get this to work. Any thoughts? <html xmlns=http://www.w3.org/1999/xhtml> <head> <meta
I cannot seem to get this. Everyone else examples or questions use different functions
I am trying this and cannot seem to get it to work. Can someone

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.