1
votes

some time ago a big Excel file was imported into a PostgreSQL table. The layout in Excel was like this (see list numbers as row numbers):

  1. Some Column | Some other Column | Yet another one | ...
  2. Descriptiontext foo | Descriptiontext2 | bar foo bar | ...
  3. Value a | Value b | Value c | ...
  4. Value aa | Value bb | Value cc | ...

So there was the header row with the column names, then a row with a description text for each column and after that hundreds of rows with all the values.

In PostgreSQL two tables where created.

Table1:

CREATE TABLE t1 (
    t1_id integer NOT NULL
    col1 inet,
    some_other_col character varying,
    another_one integer,
    ...
);

Table2:

CREATE TABLE mapping (
    mapping_id integer NOT NULL
    colname character varying,
    mapping character varying,
    description character varying,
);

INSERT INTO mapping VALUES (1, 'col1', 'Some Column', 'Descriptiontext foo');
INSERT INTO mapping VALUES (2, 'some_other_col', 'Some other Column', 'Descriptiontext2');
INSERT INTO mapping VALUES (3, 'another_one', 'Yet another one', 'bar foo bar');

My question now is, how to merge these tables again to export an Excel sheet with the same format as the old one? And would I need PHP for that or is this possible using only SQL?

2
It's probably going to be a lot simpler with 2 queries in PHP. A first one to get the data from mapping and display the first two rows (one loop over the results for each), and another one to fetch the rest of the data and display them as individual rows.jcaron
@jcaron Seems reasonable to me - add it as an answer.TML

2 Answers

0
votes

Just create a FUNCTION to merge both table with your desire format

Then use COPY to create the excel file. No need to use php here

0
votes

It's probably going to be a lot simpler with 2 queries in PHP:

  1. A first one to get the data from mapping and display the first two rows (one loop over the results for each)

  2. Another one to fetch the rest of the data and display them as individual rows