我有两个查询,一个使用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
另一个只是一个简单的左/右外连接。
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
我希望能够将第二个查询合并到第一个查询中,以创建Column" Purchased_By"。然而,由于UNION ALL,我正在努力实施它。
基本上,每张票都有一个与之关联的user_id。每个user_id都附加一个first_name。我把这两个部分分开工作了。
例如,user_id是Bob。这在第二个查询中工作正常。我只想让它现在显示鲍勃写的票#12345
编辑:这是创建表语句
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");
第二张表:
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");
答案 0 :(得分:0)
purchase_list_items
表已包含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
...