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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T12:07:01+00:00 2026-06-16T12:07:01+00:00

I have a Table Like Below CREATE TABLE Statistics(Stat_Id INT NOT NULL PRIMARY KEY

  • 0

I have a Table Like Below

CREATE TABLE Statistics(Stat_Id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
                        Period  VARCHAR(55),
                        Location VARCHAR(255),
                        Rate_per_SqFt INT)

The Datas in the Table are

INSERT INTO Statistics(Period, Location, Rate_per_SqFt)
                VALUES('June', 'Location A', 2500),
                      ('June', 'Location B', 2740),
                      ('June', 'Location C', 3200),
                      ('July', 'Location A', 2650),
                      ('July', 'Location B', 2800),
                      ('July', 'Location C', 3250),
                      ('August', 'Location A', 2750),
                      ('August', 'Location B', 2950),
                      ('August', 'Location C', 3230),
                      ('October', 'Location A', 2950),
                      ('October', 'Location B', 3950),
                      ('October', 'Location C', 3530);

I want the Rows for Particular Month Displayed in Output as Separate Columns as Below

 Period   Location A     Location B     Location C 
 June        2500          2740           3200
 July        2650          2800           3250
 August      2750          2950           3230
 October     2950          3950           3530  

How can I do this using Query

  • 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-16T12:07:02+00:00Added an answer on June 16, 2026 at 12:07 pm
    SELECT Period,
            MAX(CASE WHEN Location = 'Location A' THEN Rate_per_SqFt ELSE NULL END) `Location A`,
            MAX(CASE WHEN Location = 'Location B' THEN Rate_per_SqFt ELSE NULL END) `Location B`,
            MAX(CASE WHEN Location = 'Location C' THEN Rate_per_SqFt ELSE NULL END) `Location C`
    FROM statistics
    GROUP BY Period
    
    • SQLFiddle Demo Link

    if you have unknown number of locations, a Dynamic SQL is preferred

    SET @sql = NULL;
    SELECT
      GROUP_CONCAT(DISTINCT
        CONCAT(
          'MAX(CASE WHEN Location = ''',
          Location,
          ''' then Rate_per_SqFt ELSE NULL end) AS ',
          CONCAT('`',Location,'`')
        )
      ) INTO @sql
    FROM statistics;
    
    SET @sql = CONCAT('SELECT Period, ', @sql, ' 
                       FROM Statistics 
                       GROUP BY Period');
    
    PREPARE stmt FROM @sql;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt;
    
    • SQL Fiddle Demo Link
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table like below CREATE TABLE Customers_History(Row_Id INT NOT NULL PRIMARY KEY
I have two tables Like Below CREATE TABLE projectlist(ProjectId INT NOT NULL PRIMARY KEY,
e.g. If I have a table like below:- create table Categories(CategoryId int primary key,
I have a table like the one below. CREATE TABLE People(PeopleId INT NOT NULL
I have mysql database structure like below: CREATE TABLE test ( id int(11) NOT
I have a table like below CREATE TABLE `fruits` ( `fruit_id` int(11) default NULL,
I have a Table Like Below CREATE TABLE Products(Product_id INT, ProductName VARCHAR(255), Featured enum('Yes',
I have a history table like below CREATE TABLE [dbo].[t_PettyCash_History] ( [TableName] [varchar] (500)
I have a table structure like below : categoryID bigint , primary key ,
i have a table like below: create table info (username varchar(30),otherinfo varchar(100)); now i

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.