Americo
Americo

Reputation: 919

SQL -- Adding a constraint to a results row in an existing query

The following query gives me a one row result

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')
 )

I now want to add in a constraint to the result where: only users that have created an account before Sep 12,2012.

In my database language: only users that have a trunc(create_dtime) < To_Date('2012-sep-12','yyyy-mon-dd') . This trunc(create_dtime) date comes from the player table.

Could I map this player table to the current table by using another inner join?

Upvotes: 0

Views: 98

Answers (1)

APC
APC

Reputation: 146349

Your requirement isn't completely clear and obviously I have had to guess about your data model. I'm assuming the Player table has one row per Player_Id i.e. it is the primary key end of foreign key relationships to the tables you're querying.

I have changed the inner query so it just returns a set of rows rather than aggregations. Then I join the inline view (aliased q) with the Player table and use a COUNT() rather a SUM() to get the figure.

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')

This may not be the exact result you need, for the reasons I gave at the top, but it should point you in the right directon.

Upvotes: 1

Related Questions