简体   繁体   中英

Select multiple results from sub query into single row (as array datatype)

I'm trying to solve a small problem with a SQL query in an oracle database. Let's assume I have these tables:

One table that holds information about cars:

tblCars
ID    Model    Color
--------------------
1     Volvo    Red
2     BMW      Blue
3     BMW      Green

And another one containing information about drivers:

tblDrivers
ID    fID_tblCars    Name
---------------------------
1     1              George
2     1              Mike
3     2              Jason
4     2              Paul
5     2              William
6     3              Steve

Now, let's pretend that to find out the popularity of the cars, I want to create reports that contain the data about the cars and the people that are driving them (which seems a very reasonable thing one would accomplish with a database).

This "ReportObject" would have a string for the model, a string for the color and an array (or a list) of strings for the drivers.

Currently, I do this with two queries, in the first I select the cars

SELECT ID, Model, Color FROM tblCars

and create a report object for each result.

Then, I would take each result and get the drivers for each specific car

SELECT Name FROM tblDrivers WHERE fID_tblCars = ResultObject.ID

Basically, step one gives me a resulting data set that looks like this:

Result
------------------------------------------
ColumnID        ColumnModel    ColumnColor
Type Integer    Type String    Type String

and now, if I will have more cars in the future, I will have to make a lot of additional queries, one for each row in the resulting table.

When I try this:

SELECT Model, Color, (SELECT Name FROM tblDrivers WHERE tblDrivers.fID_tblCars = tblCars.ID) as Name FROM tblCars

I get some error message telling me that one result in the row contains multiple elements (which is what I want!).

I want the result to look like this:

Result
--------------------------------------------------------
ColumnID        ColumnModel    ColumnColor    ColumnName
Type Integer    Type String    Type String    Type Array

So when I build my report object, I could do something like this:

foreach (var Row in Results)
{
    ReportObject.Model = Row.Model;
    ReportObject.Color = Row.Color;
    foreach (string Driver in Row.Name)
    {
        ReportObject.Drivers.Add(Driver);
    }
}

Am I completely missing my basics here or do I have to split this up in multiple queries?

Thanks!

This works in Oracle. In the SQL Fiddle example I couldn't get the IDENTITY or the PRIMARY KEYS to work when creating the table (never used Oracle SQL before)

SELECT  c.id, 
    c.model, 
    c.color,
    LISTAGG(d.name, ',') WITHIN GROUP (ORDER BY d.name) AS "Drivers"
FROM tblCars c
JOIN tblDrivers d
ON c.id = d.fID_TblCars
GROUP BY c.id,
    c.model,
    c.color
ORDER BY c.Id

SQL Fiddle Example

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM