Combining UNION ALL and OUTER JOIN in SQL Statement

1.1k views Asked by At

I've got two queries, one uses a union all function.

select * from (

Select 

ticket_id as Ticket_Num
, name as Name
, approved_at as Approval_Date
, price as Price
, quantity as Quantity
, (price * quantity) as Cumulative_Price

From purchase_list_items

union all 

select
   ''
, ''
, datetime('now')
, ''
, 'Total'
,  sum(in1.Cumulative_Price) 


from (

select 
  ticket_id as Ticket_Num
, name as Name
, approved_at as Approval_Date
, price as Price
, quantity as Quantity
, (price * quantity) as Cumulative_Price

From purchase_list_items
    )
  in1 

  ) 
fullInner1



order by Approval_Date

And the other is just a simple left/right outer join.

select 
  uw.first_name as Purchased_By
from tickets t

left outer join ticket_work w on t.id = w.ticket_id
left outer join users uw on w.user_id = uw.id

where time_spent is not null

group by uw.id, uw.first_name, t.c_location

I want to be able to combine the second query into the first, to create the Column "Purchased_By". I'm struggling with implementing it however, due to the UNION ALL.

Essentially, every ticket has a user_id associated with it. Each user_id has a first_name attached to that. I have the two parts separated working properly.

For instance, user_id is Bob. And that works fine in the second query. I just want it to now show that Bob wrote ticket #12345

EDIT: Here are the create table statements

CREATE TABLE "purchase_list_items" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(200) NOT NULL, "user_id" integer NOT NULL, "purchased" boolean DEFAULT 'f', "ticket_id" integer, "created_at" datetime, "received" boolean DEFAULT 'f', "received_at" datetime, "purchased_at" datetime, "price" float DEFAULT 0.0, "approved" boolean DEFAULT 'f', "approved_at" datetime, "charge_to" varchar(255) DEFAULT '', "agreement_id" integer, "part_number" varchar(255), "purchase_order" varchar(255), "notes" text, "quantity" integer DEFAULT 1, "shipping_code" varchar(255), "updated_at" datetime, "purchased_for_id" integer, "purchased_for_type" varchar(255), "category" varchar(255) DEFAULT 'Miscellaneous', "subcategory" varchar(255) DEFAULT 'Uncategorized', "order_number" varchar(255), "quote_id" integer DEFAULT 0, "upc" varchar(255) DEFAULT '', "gid" varchar(255) DEFAULT '', "research_class" varchar(255) DEFAULT '', "research_code" varchar(255) DEFAULT '', "product_image" varchar(255) DEFAULT '', "purchase_link" varchar(255) DEFAULT '', "vendor_id" integer);

CREATE INDEX "index_purchase_list_items_on_user_id" ON "purchase_list_items" ("user_id");

CREATE INDEX "index_purchase_list_items_on_ticket_id" ON "purchase_list_items" ("ticket_id");

CREATE INDEX "index_purchase_list_items_on_agreement_id" ON "purchase_list_items" ("agreement_id");

CREATE INDEX "index_purchase_list_items_on_purchased_for" ON     "purchase_list_items" ("purchased_for_id", "purchased_for_type");

Second Table:

CREATE TABLE "tickets" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "summary" varchar(50) NOT NULL, "status" varchar(255) NOT NULL, "description" text, "priority" integer, "created_at" datetime, "updated_at" datetime, "closed_at" datetime, "created_by" integer, "assigned_to" integer, "viewed_at" datetime, "reopened" boolean, "requires_purchase" boolean, "category" varchar(255), "external_id" varchar(255), "email_message_id" varchar(255), "status_updated_at" datetime, "warning_alert_count" integer DEFAULT 0, "error_alert_count" integer DEFAULT 0, "muted" boolean, "site_id" integer, "master_ticket_id" integer, "c_location" varchar(255), "c_jobscope_case_id" text, "reported_by_id" integer, "due_at" datetime, "c_status" varchar(255) DEFAULT 'Active', "c_jobscope_database" varchar(255) DEFAULT 'ALL', "remote_id" integer, "synced_at" datetime, "sharer_id" integer, "parent_id" integer, "c_original_est_due_date" date, "c_jobscope_priority" varchar(255), "c_priority" integer, "c_sciforma_ticket_number" integer DEFAULT NULL);

CREATE INDEX "index_tickets_on_remote_id" ON "tickets" ("remote_id");

CREATE INDEX "index_tickets_on_synced_at" ON "tickets" ("synced_at");

CREATE INDEX "index_tickets_on_sharer_id" ON "tickets" ("sharer_id");

CREATE INDEX "index_tickets_on_parent_id" ON "tickets" ("parent_id");

CREATE INDEX "index_tickets_on_assigned_to" ON "tickets" ("assigned_to");

CREATE INDEX "index_tickets_on_status" ON "tickets" ("status");

CREATE INDEX "index_tickets_on_due_at" ON "tickets" ("due_at");
1

There are 1 answers

0
CL. On BEST ANSWER

The purchase_list_items table already contains the user_id. A query to look up the corresponding name would look like this:

SELECT first_name
FROM users
WHERE user_id = ?

This can be directly integrated into the larger query as a correlated subquery:

SELECT ...,
       (price * quantity) as Cumulative_Price,
       (SELECT first_name
        FROM users
        WHERE user_id = purchase_list_items.user_id
       ) AS Purchased_By
FROM purchase_list_items
...