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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T16:57:38+00:00 2026-06-18T16:57:38+00:00

I have a simple database (using MySql) with which I need to generate a

  • 0

I have a simple database (using MySql) with which I need to generate a report.

I have a table that contains weekly logs which are submitted by users. Each of these logs have states, such as: submitted, approved, etc.

LogSubmissions Table
+----+--------+--------+---------+-------+
| id | weekId | userId | info... | state |
+----+--------+--------+---------+-------+
|  0 |      0 |     1  | data... |     0 |
|  1 |      0 |     2  | data... |     1 |
|  1 |      0 |     3  | data... |     1 |
|  2 |      1 |     1  | data... |     0 |

I also have a table which keeps track of the weeks.

WeekManager Table
+----+-----------+---------+---------+
| id | startDate | EndDate | ...     |
+----+-----------+---------+---------+
|  0 | Date1     | Date2   | ...     |
|  1 | Date3     | Date4   | ...     |
|  2 | Date5     | Date6   | ...     |

I need a report which displays the weeks as columns as well as the names from a user table. It should display the state of the weekly log the user submitted. For example Bob submission for Week 1 has a state of 0.

Report
+------+--------+--------+--------+-------+
| Name | Week 1 | Week 2 | Week 3 | ...   |
+------+--------+--------+--------+-------+
| Bob  |      0 |      1 |      0 | ...   |
| Joe  |      1 |      1 |      1 | ...   |
| Jim  |      0 |      0 |      0 | ...   |

I am having a bit of trouble getting this report working. My major obstacle is that weeks are added, so the amount of columns for report will vary.

This here helped quite a bit, however it only deals with a static amount of rows. I need something that varies depending on the amount of weeks. I was thinking of using a loop of some kind to achieve this, however I’m not sure if that is possible.

Any help would be appreciated, thank you.

User Table:

User Table
+----+------+---------+
| id | name | ...     |
+----+------+---------+
|  0 | ...  | ...     |
|  1 | ...  | ...     |
|  2 | ...  | ...     |
  • 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-18T16:57:39+00:00Added an answer on June 18, 2026 at 4:57 pm

    Your table structure is not exactly clear (for example I don’t see where you get user name).

    The basic set up for a pivot in MySQL uses an aggregate function and a CASE expression similar to this:

    select 
      l.userid,
      max(case when w.id =0 then l.state end) Week1,
      max(case when w.id =1 then l.state end) Week2,
      max(case when w.id =2 then l.state end) Week3
    from LogSubmissions l
    left join WeekManager w
      on w.id = l.weekid
    group by l.userid
    

    See SQL Fiddle with Demo.

    Edit, if you have an unknown number of weeks then you can use a prepared statement to generate dynamic sql:

    SET @sql = NULL;
    SELECT
      GROUP_CONCAT(DISTINCT
        CONCAT(
          'max(case when w.id = ',
          id,
          ' then l.state end) AS Week',
          id + 1
        )
      ) INTO @sql
    FROM  WeekManager;
    
    SET @sql = CONCAT('SELECT l.userid, ', @sql, ' 
                      from LogSubmissions l
                      left join WeekManager w
                        on w.id = l.weekid
                      group by l.userid');
    
    PREPARE stmt FROM @sql;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt;
    

    See SQL Fiddle with Demo

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

Sidebar

Related Questions

I have a very simple database table that joins two other tables in a
I currently have a very simple MySQL database (articlesDB) with 1 table (articles) and
I have a simple entity, Code, that I need to persist to a MySQL
I have a Database with a category table using the Nested Approach in MySql.
I have simple database - one table with 6 collumns. 3 of them i
I have a simple database named customer with a single table data.I want to
Consider the following scenario We have a simple database that involves two entities: user
I have a simple UserControl for database paging, that uses a controller to perform
We have a website that contains a database of places. For each place our
I have a web applications that stores data in a MySQL database on-line. It

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.