Sum Twice on Same Table?

Posts   
 
    
psandler
User
Posts: 540
Joined: 22-Feb-2005
# Posted on: 09-Jan-2007 23:40:24   

Is this SQL possible in LLBL, or is it re-writable so that it is? I could do it with a CASE statement in SQL, but AFAIK that can't be done with the framework.

The key is that the SUM columns in the select list need to be filtered by the current row's project_id. I can't see a way of doing this in LLBL.

Seems like this should be easy to rewrite by having multiple references to the expense_report_data table in the FROM clause, but I can't work it out somehow.


DECLARE
    @periodStartID  int,
    @periodEndId    int

SELECT
    @periodStartID = 109,
    @periodEndId = 111  

SELECT DISTINCT
    p.project_id,
    p.project_name,
    c.client_name,
    (SELECT SUM(Report_Amount) FROM Expense_Report_Data e WHERE Billed_To_Client = 1 and project_id = p.project_id),
    (SELECT SUM(Report_Amount) FROM Expense_Report_Data e WHERE Billed_To_Client = 0 and project_id = p.project_id)
FROM
    project_master p
    INNER JOIN expense_report_data e ON p.project_id = e.project_id 
    INNER JOIN client_master c ON c.client_id = p.client_id
e2.Billed_To_Client = 0)
WHERE
    e.Time_Period_Id BETWEEN @periodStartID AND @periodEndID
    and project_office_id = 1
ORDER BY
    c.client_name

Any help would be appreciated.

Thanks,

Phil

psandler
User
Posts: 540
Joined: 22-Feb-2005
# Posted on: 10-Jan-2007 00:10:14   

Hmm, I think I have it. I had a typo in the query when I tried to reference the expense table multiple times. flushed

Still, I'd like to know if this is possible:

The key is that the SUM columns in the select list need to be filtered by the current row's project_id. I can't see a way of doing this in LLBL.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 10-Jan-2007 08:22:22   

For this query I would either use a database view, Or implement IExpression (once for each sum column): http://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=3829

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39927
Joined: 17-Aug-2003
# Posted on: 10-Jan-2007 09:33:54   

Yes that's possible (in v2.0), using a ScalarQueryExpression object as the field expression in a dyn. list simple_smile

Frans Bouma | Lead developer LLBLGen Pro
psandler
User
Posts: 540
Joined: 22-Feb-2005
# Posted on: 10-Jan-2007 17:02:58   

Otis wrote:

Yes that's possible (in v2.0), using a ScalarQueryExpression object as the field expression in a dyn. list simple_smile

Ahhhhhhh, that worked. smile I actually tried this before posting but gave up quickly as I thought it wasn't possible (I had the alias code wrong).

Any new thoughts on adding CASE statement support?

Thanks!

Phil

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39927
Joined: 17-Aug-2003
# Posted on: 10-Jan-2007 17:44:24   

Won't add it for now.

Frans Bouma | Lead developer LLBLGen Pro