mtfurlan
mtfurlan

Reputation: 1062

Combining two distinct Oracle queries

So I have two queries

SELECT DISTINCT ReservationGroupName FROM EventDataTable

and

SELECT DISTINCT BookingEventType FROM EventDataTable

I'm making a form that lets you filter by one or both of these, so I need to print out each distinct result from each in a select dropdown.

I tried a UNION and a UNION ALL, but I just get an array of the results of ReservationGroupName, that also contained the results of BookingEventType. There should be 540 results from ReservationGroupName, and 63 from BookingEventType, and that returns an array called ReservationGroupName with 603 elements. My problem is that I need to differentiate between the results

It would be possible to just do two queries seperately, but it seemed like I should be able to combine it, but a few hours of looking at old forums has just left me confused.

Current code:

SELECT DISTINCT ReservationGroupName AS \"group\", '' AS \"type\" FROM EventDataTable
    UNION
SELECT DISTINCT BookingEventType AS \"type\", '' AS \"group\" FROM EventDataTable

Returns a ReservationGroupName array with all of the data, but also an array of the same length populated by null values called EventDataTable.

Upvotes: 0

Views: 959

Answers (2)

Bill
Bill

Reputation: 4585

This may sound dumb, but the UNION may be relying on the field names and giving you a resultset that has a bunch of empty strings in the field you are attaching to the dropdown. Try something like this...

SELECT DISTINCT ReservationGroupName AS \"entry\", 'GroupName' AS \"type\" FROM EventDataTable
    UNION
SELECT DISTINCT BookingEventType AS \"entry\", 'EventType' AS \"type\" FROM EventDataTable

Upvotes: 1

Shaz
Shaz

Reputation: 33

The UNION should work. Please find below the tables I created and I get the distinct values from both columns. Hope your design is same as below.

create table EventDataTable ( ReservationGroupName VARCHAR2(20) ,BookingEventType VARCHAR2(20) );

insert into EventDataTable values ('EMP','S');

insert into EventDataTable values ('ABC','T');

SELECT DISTINCT ReservationGroupName FROM EventDataTable union SELECT DISTINCT BookingEventType FROM EventDataTable;

DIS_VAL

ABC EMP S T

Upvotes: 0

Related Questions