簡體   English   中英

將多個 select 語句插入到 Temp 表中

[英]Insert Into Temp table multiple select statements

我有 2 個 select 語句,我想在下面顯示 output 如下所示。 目前,我無法將第一個查詢與第二個查詢結合起來。

fanme   lname  JobTitle   AcceptanceRate
Jim    Cary     Manager    0.666666
   select fname, lname, JobTitle
   from [User]
   where userID=8

Select convert(decimal, x.QuoteAccepted) / y.AllQuotes as  AcceptanceRate 
from  (
    select count(*) as QuoteAccepted
    from Quote
    where ProjectManager_UserID=8 and QuoteStatusID=6
) x
join (
    select count(*) as AllQuotes
    from Quote
    where ProjectManager_UserID=8
) 
y on 1=1 

我嘗試創建臨時表並插入,但它一直顯示錯誤:

列名或提供的值的數量與表定義不匹配。

有沒有辦法做到這一點?

您可以使用條件聚合:

select u.*,
       (select avg(case when q.QuoteStatusID = 6 then 1.0 else 0 end) as QuoteAccepted
        from Quote q
        where q.ProjectManager_UserID = u.userId
       ) as AcceptanceRate
from users u
where u.UserID = 8

如果您希望所有項目經理都這樣做:

select ProjectManager_UserID,
       avg(case when QuoteStatusID = 6 then 1.0 else 0 end) as AcceptanceRate
from Quote
group by ProjectManager_UserID;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM