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

  • Home
  • SEARCH
  • 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 3277388
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:21:46+00:00 2026-05-17T19:21:46+00:00

Whilst trying to pivot a sql table I came across this post Here .

  • 0

Whilst trying to pivot a sql table I came across this post Here . By using this method I have created a query. However i have now realised that it of course aggregates the results with the MAX function. However I need the Colum to pivot but for all occurrences to be show. Code taken from above post.

  SELECT dy,
         MAX(CASE WHEN period = 1 THEN subj ELSE NULL END) AS P1,
         MAX(CASE WHEN period = 2 THEN subj ELSE NULL END) AS P2,    
    FROM Classes
GROUP BY dy

So in essence I wish to use this but without the max function ? Any Ideas?

Edit Example data

Day   Period    Subject

Mon   1         Ch
Mon   2         Ph
Tue   1         Ph
Tue   2         Ele
Mon   1         Ch
Mon   2         Ph
Tue   1         Ph
Tue   2         Ele

example output

Day   P1   P2   

Mon   Ch   Ph   
Mon   Ch   Ph   
Tue   Ph   Ele  
Tue   Ph   Ele  

so basicly if the data is entered twice it appears twice…

Edit actual sql..

  SELECT other
         MAX(CASE WHEN period = 1 THEN table2.subj ELSE NULL END) AS P1,
         MAX(CASE WHEN period = 2 THEN table2.subj ELSE NULL END) AS P2    
    FROM table1
left join table2 on table2.ID = subject

GROUP BY other

Example data

Table1

Dy   Period    Subject other

Mon   1         1       1
Mon   2         2       1
Tue   1         3       2
Tue   2         4       2
Mon   1         5       3
Mon   2         6       3
Tue   1         7       4
Tue   2         8       4

table2

ID  Subj
1 ch
2 ph
3 ph
4 ele
5 ch
6 ph
7 ph
8 Ele

example output

Day   P1   P2   other

Mon   Ch   Ph   1
Mon   Ch   Ph   3
Tue   Ph   Ele  2
Tue   Ph   Ele  4
  • 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-17T19:21:47+00:00Added an answer on May 17, 2026 at 7:21 pm

    Your example data and your desired output don’t match up. Where are the Mth, CS2, Lab, and Hu values coming from in the output? They’re not in the input.

    It’s not entirely clear what you’re trying to do, but assuming that the separate rows in your output for (for instance) Mon representing two different students, you need to include the distinguishing column (StudentID) in your SELECT list and the GROUP BY clause. Then you can safely use MAX not to aggregate (since there will only be one value per output cell) but rather to trick the engine into doing the pivot for you.

    Here’s the SQL with the student ID included:

    SELECT student_id, dy,
         MAX(CASE WHEN period = 1 THEN subj ELSE NULL END) AS P1,
         MAX(CASE WHEN period = 2 THEN subj ELSE NULL END) AS P2,    
    FROM Classes
    GROUP BY student_id, dy
    

    This will return one record for each student for each day showing their daily class schedule. If you want the output sorted by day then student, reverse the order of student_id and dy in the SELECT and GROUP BY clauses.

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

Sidebar

Related Questions

Whilst refactoring some code I came across some getter methods that returns a std::string.
Whilst trawling through some old code I came across something similar to the following:
Whilst trying to parse MS Excel file using POI-HSSF v3.2 I am getting IndexOutOfBoundsException.
Gday All, I have a baffling problem whilst trying to insert some chinese characters
According to my research whilst trying to solve this problem, it turns out that
Whilst trying to get our app working in Firefox (I'm a big proponent of
Whilst starting to learn lisp, I've come across the term tail-recursive . What does
Whilst trying to update through the Android AVD Manager, I receive the message: No
I'm getting an error whilst trying to install psycopg2 on ubuntu 9.10 64 bit.
I'm new to VBA and Access in general and ran into this problem whilst

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.