2012-09-18 78 views
0

下面的查詢給了我一個行的結果SQL - 添加約束的結果行中的現有查詢

Select Sum(Actions) From 
(
    Select 
     Count(t.Create_Dtime) As Actions 
    From Player_Tapjoy T 
      Inner Join Player_Source S 
       On (T.Player_Id = S.Player_Id) 
      Inner Join Feature_Group_Xref F 
      On (S.Group_Id=F.Group_Id 
       And F.Feature_Name ='Try  BC') 
    Where Trunc(t.Create_Dtime) = To_Date('2012-sep-17','yyyy-mon-dd') 
    Union All 
    Select 
     Count(a.Create_Dtime) As Actions 
    From Player_Aux_Pt A 
     Inner Join Player_Source S 
       On (A.Player_Id = S.Player_Id) 
     Inner Join Feature_Group_Xref F 
      On (S.Group_Id=F.Group_Id 
       and f.feature_name =  'TryBC') 
    Where A.Site = 'AppCircle' 
    And Trunc(A.Create_Dtime) = To_Date('2012-sep-17','yyyy-mon-dd') 
) 

我現在要在約束添加到結果中:只有那些用戶在2012年9月12日之前創建了一個賬戶。

在我的數據庫語言:只有有trunc(create_dtime) < To_Date('2012-sep-12','yyyy-mon-dd')用戶。這trunc(create_dtime)日期來自player表。

我能使用另一種內連接地圖這player表到當前的表?

回答

1

您的要求並不完全清楚,顯然我不得不猜測您的數據模型。我假設Player表每個Player_Id有一行,即它是您查詢表的外鍵關係的主鍵端。

我已經改變了內部查詢,以便它只返回一組行,而不是聚合的。然後我加入內聯視圖(別名爲q)和Player表,並使用COUNT()而不是SUM()來獲取數字。

Select count(Player_Id) From 
(
    Select 
     T.Player_Id 
    From Player_Tapjoy T 
      Inner Join Player_Source S 
       On (T.Player_Id = S.Player_Id) 
      Inner Join Feature_Group_Xref F 
      On (S.Group_Id=F.Group_Id 
       And F.Feature_Name ='Try  BC') 
    Where Trunc(t.Create_Dtime) = To_Date('2012-sep-17','yyyy-mon-dd') 
    Union All 
    Select 
     A.Player_Id 
    From Player_Aux_Pt A 
     Inner Join Player_Source S 
       On (A.Player_Id = S.Player_Id) 
     Inner Join Feature_Group_Xref F 
      On (S.Group_Id=F.Group_Id 
       and f.feature_name =  'TryBC') 
    Where A.Site = 'AppCircle' 
    And Trunc(A.Create_Dtime) = To_Date('2012-sep-17','yyyy-mon-dd') 
) q 
join player p 
     on (p.Player_Id = q.Player_Id) 
where Trunc(p.Create_Dtime) < To_Date('2012-sep-12','yyyy-mon-dd') 

這可能不是您需要的確切結果,因爲我在頂部給出的原因,但它應該指向您正確的方向。

+0

我自己回答了這個問題,查詢工作。 – Americo