2015-09-30 81 views
0

我正在嘗試創建一個分子(num)和分母(den)列,我稍後將使用它創建度量標準值。在我的分子專欄中,我需要有一個我的分母專欄沒有的標準。當我添加where子句到我的子查詢時,我得到下面的錯誤。我不想將INRInRange添加到我的Group By子句中。因爲它不是在聚合函數或GROUP BY子句中包含子查詢選擇列表中的子句Where無效

列「dbo.PersonDetailB.INRInRange」在選擇列表中無效。」

SELECT 
     dbo.PersonDetailSpecialty.PracticeAbbrevName, 
     (SELECT COUNT(DISTINCT dbo.Problem.PID) WHERE PersonDetailB.INRInRange='True') AS num, 
     COUNT(DISTINCT dbo.Problem.PID) AS den 
FROM   
     dbo.PersonDetailB 
RIGHT OUTER JOIN 
     dbo.PersonDetailSpecialty ON dbo.PersonDetailB.PID = dbo.PersonDetailSpecialty.PID 
LEFT OUTER JOIN 
     dbo.Problem ON dbo.PersonDetailSpecialty.PID = dbo.Problem.PID 

GROUP BY 
     practiceabbrevname 
+0

替換的一個簡單的部分' dbo.PersonDetailSpecialty.PracticeAbbrevName' with'practiceabbrevname', – abatishchev

回答

1

創建計數PersonDetailB.INRInRange和LEFT OUTER與原始查詢加入它的子查詢。

SELECT Main.PracticeAbbrevName, InRange.Num AS num, Main.den 
FROM 
    (SELECT 
      dbo.PersonDetailSpecialty.PracticeAbbrevName, 
      COUNT(DISTINCT dbo.Problem.PID) AS den 
    FROM   
      dbo.PersonDetailB 
    RIGHT OUTER JOIN 
      dbo.PersonDetailSpecialty ON dbo.PersonDetailB.PID = dbo.PersonDetailSpecialty.PID 
    LEFT OUTER JOIN 
      dbo.Problem ON dbo.PersonDetailSpecialty.PID = dbo.Problem.PID 
    GROUP BY 
      practiceabbrevname) Main 
    LEFT OUTER JOIN 
      (SELECT practiceabbrevname, COUNT(DISTINCT dbo.Problem.PID) Num WHERE PersonDetailB.INRInRange='True' GROUP BY practiceabbrevname) InRange ON Main.practiceabbrevname = InRange.practiceabbrevname 
+0

沿着這些路線,我不能只加入一張關於PID = PID和INRInRange ='True'的表格 –

+0

是的,你可以做到這一點。 – coderboy

0

與此問題聲明:

SELECT dbo.PersonDetailSpecialty.PracticeAbbrevName, 
     (SELECT COUNT(DISTINCT dbo.Problem.PID) WHERE PersonDetailB.INRInRange = 'True') AS num, 
     COUNT(DISTINCT dbo.Problem.PID) AS den 

PersonDetailB.INRInRange1不必每個組中一個獨特的價值有可能是它的一種方法是將其添加到。:

GROUP BY practiceabbrevname, PersonDetailB.INRInRange 

另一種方法就是在子查詢中使用聚合函數:

SELECT dbo.PersonDetailSpecialty.PracticeAbbrevName, 
     (SELECT COUNT(DISTINCT dbo.Problem.PID) WHERE MAX(PersonDetailB.INRInRange) = 'True') AS num, 
     COUNT(DISTINCT dbo.Problem.PID) AS den 
+0

INRInRange有點大ta類型,所以Max運算符無效。 –

+0

@JohnS。 。 。將它添加到「GROUP BY」中怎麼樣? –

0

加入一個單獨的表作爲子查詢中where標準的不同名稱。

SELECT PersonDetailSpecialty.PracticeAbbrevName, 
      COUNT(PDTRUE.PID) as num, 
      COUNT(dbo.Problem.PID) AS den 

FROM  dbo.PersonDetailSpecialty LEFT OUTER JOIN 
      dbo.PersonDetailB as PDTRUE ON dbo.PersonDetailSpecialty.PID = PDTRUE.PID and PDTRUE.INRInRange='True' LEFT OUTER JOIN 
      dbo.PersonDetailB ON dbo.PersonDetailSpecialty.PID = PersonDetailB.PID LEFT OUTER JOIN 
      dbo.Medicate ON dbo.PersonDetailSpecialty.PID = dbo.Medicate.PID LEFT OUTER JOIN 
      dbo.Problem ON dbo.PersonDetailSpecialty.PID = dbo.Problem.PID 

GROUP BY PersonDetailSpecialty.PracticeAbbrevName 

這是需要得到部分

dbo.PersonDetailSpecialty LEFT OUTER JOIN dbo.PersonDetailB as PDTRUE 
ON dbo.PersonDetailSpecialty.PID = PDTRUE.PID and PDTRUE.INRInRange='True' 

相關的代碼這允許您添加

COUNT(PDTRUE.PID) as num, 

的整體選擇查詢