Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PostgreSQL: how to combine multiple rows?

Tags:

sql

postgresql

I have a table like this to save the results of a medical checkup and the date of the report sent and the result. Actually the date sent is based on the clinic_visit date. A client can have one or more reports (date may varies)

---------------------------------------
|  client_id  |  date_sent  | result |
---------------------------------------
| 1           |   2001      |    A   |
| 1           |   2002      |    B   |
| 2           |   2002      |    D   |
| 3           |   2001      |    A   |
| 3           |   2003      |    C   |
| 3           |   2005      |    E   |
| 4           |   2002      |    D   |
| 4           |   2004      |    E   |
| 5           |   2004      |    B   |
---------------------------------------

I want to extract the following report from the above data.

---------------------------------------------------
|  client_id  |  result1  |  result2  |   resut3  |
---------------------------------------------------
|      1      |    A      |    B      |           |
|      2      |    D      |           |           |
|      3      |    A      |    C      |     E     |
|      4      |    D      |    E      |           |
|      5      |    B      |           |           |
---------------------------------------------------

I'm working on Postgresql. the "crosstab" function won't work here because the "date_sent" is not consistent for each client.

Can anyone please give a rough idea how it should be queried?

like image 834
thinzar00 Avatar asked Sep 30 '10 02:09

thinzar00


People also ask

How do I combine multiple rows into one column in SQL?

You can concatenate rows into single string using COALESCE method. This COALESCE method can be used in SQL Server version 2008 and higher. All you have to do is, declare a varchar variable and inside the coalesce, concat the variable with comma and the column, then assign the COALESCE to the variable.

What is crosstab in PostgreSQL?

The crosstab function produces one output row for each consecutive group of input rows with the same row_name value. The output row_name column, plus any “extra” columns, are copied from the first row of the group. The output value columns are filled with the value fields from rows having matching category values.

How do I concatenate strings and integers in PostgreSQL?

PostgreSQL allows you to directly concatenate strings, columns and int values using || operator. Here is the SQL query to concatenate columns first_name and last_name using || operator. You can even concatenate string with int using || operator.


1 Answers

I suggest the following approach:

SELECT client_id, array_agg(result) AS results
    FROM labresults
    GROUP BY client_id;

It's not exactly the same output format, but it will give you the same information much faster and cleaner.

If you want the results in separate columns, you can always do this:

SELECT client_id,
       results[1] AS result1,
       results[2] AS result2,
       results[3] AS result3
FROM
(
    SELECT client_id, array_agg(result) AS results
        FROM labresults
        GROUP BY client_id 
) AS r
ORDER BY client_id;

although that will obviously introduce a hardcoded number of possible results.

like image 92
Peter Eisentraut Avatar answered Oct 12 '22 23:10

Peter Eisentraut