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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:31:49+00:00 2026-06-17T04:31:49+00:00

Im using a HSLQDB (2.2.9) database which I connect to via java JDBC. Person

  • 0

Im using a HSLQDB (2.2.9) database which I connect to via java JDBC.

Person A has received vaccinations 1 and 3, person B has received 2 and 4.
Using this simple query:

SELECT std.*,vac.Date FROM Students AS std
LEFT JOIN ValuesVaccination AS vac
ON vac.ID_Students = std.ID

I get a result set like:

 ID |  Name    | ID_Vaccination | Date
 1  | PersonA  | 1              | 2011-06-01
 1  | PersonA  | 3              | 2012-03-21
 1  | PersonB  | 2              | 2012-08-11
 2  | PersonB  | 4              | 2012-09-08

What I want to get is:

 ID |  Name    | Vaccination1 | Vaccination2 | Vaccination3 | Vaccination4  
 1  | PersonA  |  2011-06-01  |    NULL      |  2012-03-21  |    NULL
 2  | PersonB  |    NULL      |  2012-08-11  |    NULL      |  2012-09-08

One way to do this is:

SELECT std.*,
       vac1.Date AS "Vaccination1",
       vac2.Date AS "Vaccination2",
       vac3.Date AS "Vaccination3",
       vac4.Date AS "Vaccination4"
FROM Students AS std
LEFT JOIN ValuesVaccination AS vac1
ON vac1.ID_Students = std.ID AND vac1.ID_Vaccinations = 1
LEFT JOIN ValuesVaccination AS vac2
ON vac2.ID_Students = std.ID AND vac2.ID_Vaccinations = 2
LEFT JOIN ValuesVaccination AS vac3
ON vac3.ID_Students = std.ID AND vac3.ID_Vaccinations = 3
LEFT JOIN ValuesVaccination AS vac4
ON vac4.ID_Students = std.ID AND vac4.ID_Vaccinations = 4

The problem is the number of different vaccinations is variable. Is there a decent way to do this without building the query with the JOINs in a java String using a loop?

I figured out the PIVOT function is what I need, but it seems HSQLDB does not support it and searching stackoverflow and google did not bring up useful information.

Thank you for any information

  • 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-17T04:31:50+00:00Added an answer on June 17, 2026 at 4:31 am

    PIVOT is not standard SQL, it’s an extension to syntax supported only by Microsoft SQL Server 2005 (and later), and Oracle 11g.

    Here’s a solution that uses standard SQL and should work in any RDBMS:

    SELECT s.ID, s.Name, 
        MAX(CASE WHEN ID_Vaccination=1 THEN Date END) AS Vaccination1,
        MAX(CASE WHEN ID_Vaccination=2 THEN Date END) AS Vaccination2,
        MAX(CASE WHEN ID_Vaccination=3 THEN Date END) AS Vaccination3,
        MAX(CASE WHEN ID_Vaccination=4 THEN Date END) AS Vaccination4
    FROM Students AS s
    LEFT JOIN ValuesVaccination AS v
      ON s.ID = v.ID_Students
    GROUP BY s.ID, s.Name;
    

    Re your comment:

    SQL does not allow data values to generate new columns dynamically in the same query. So
    any pivot query requires that you know how many distinct vaccinations you want to show in the output before you prepare the query.

    You could run two queries: one to “discover” the set of distinct vaccination values:

    SELECT DISTINCT ID_Vaccination FROM ValuesVaccination;
    

    Then fetch that back into the application and loop over it, adding column expressions as you go, to build the pivot query.

    The only alternative is to fetch all the data joined to Students, as in your first query, and post-process it into a pivoted format using application code.

    Either way, to produce a dynamic pivot query you have to supplement it with application code, either before you prepare the query or after you fetch the results.

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

Sidebar

Related Questions

I have a simple Java web application using HSQLDB embedded database. The application is
i am using hsqldb as database. i create LmexPostParamDao which has a method insertLmexPostParam(NameValuePostParamVO
I am using JDBC to connect to MySQL for a small application. In order
We're using hdsqldb in memory to run junit tests which operate against a database.
I wish to store UUIDs created using java.util.UUID in a HSQLDB database. The obvious
I am using hsqldb standalone as my database. i have a hsqldb.jar(hsqldb-2.0.0) which i
I'm facing JDBC connection problem while trying to connect MySQL DB using WEKA. Here's
I'm using hsqldb for my Spring-based java webapp. I put database files (mydb.lck, mydb.properties,..)
I'm new to clojure. Have been playing with jdbc using hsqldb. Did this function
I'm using BoneCP for connection pooling jdbc connections to my mysql database. I'm using

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.