在 SQL 语句中结合 UNION ALL 和 OUTER JOIN

Combining UNION ALL and OUTER JOIN in SQL Statement

我有两个查询,一个使用 union all 函数。

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

而另一个只是一个简单的left/right外连接。

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

我希望能够将第二个查询合并到第一个查询中,以创建列 "Purchased_By"。然而,由于 UNION ALL,我正在努力实现它。

基本上,每张票都有一个 user_id 与之关联。每个 user_id 都附有一个 first_name。我将两部分分开工作正常。

例如,user_id 是 Bob。这在第二个查询中工作正常。我现在只想让它显示 Bob 写了 #12345

编辑:这是创建 table 语句

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");

第二个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");

purchase_list_items table 已经包含 user_id。 查找相应名称的查询如下所示:

SELECT first_name
FROM users
WHERE user_id = ?

这可以作为 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
...