Bangalore .Net User Group

Welcome to bdotnet.in
India's Largest and Most Active Microsoft User Group!!
Welcome to Bangalore .Net User Group Sign in | Join | Help
in Search

Help required on SQL query.

Last post 04-17-2008 3:50 PM by mmr_muthu. 2 replies.
Page 1 of 1 (3 items)
Sort Posts: Previous Next
  • 04-17-2008 9:28 AM

    • mmr_muthu
    • Top 150 Contributor
    • Joined on 10-16-2007
    • Mumbai
    • Posts 3
    • Points 45

    Help required on SQL query.

    Hi All,

    As per project design we have to save the sql query in the database table (say, SQL_Table) and this query will be taken into execution at the runtime.

    For example,

     The Value_Table table design is as follows.

    BusD UnqId Value
    21/12/2007 1 300
    21/12/2007 2 400
    21/12/2007 3 100
    22/12/2007 1 500
    22/12/2007 2 600
    22/12/2007 3 100

     

    I should return the value, sum(Value) of UnqId in (1,2) - Value of UnqId in 3 for the particular BusD.

    Looks simple? I am supposed save teh query with out the BusD condition. Bucase the BusD condtion ("and  BusD=@BusD") will be added up with my saved query and will be executed after the concatination. (Confusing?!?!)

    For example, forget about the minus in my calculation. I would save the following text in my SQL table.

    "declare @Num float

    select @Num=sum(Value) from Value_Table where UnqId in (1,2)"

    My calculation sp, would aceept the @BusD and in the sp, we have hard coded. "and BusD=@BusD". The hardcoded string will be concatenated with my SQL_Table record. So, the executing statements will be,

    declare @Num float

    select @Num=sum(Value) from Value_Table where UnqId in (1,2) and BusD=@BusD. So, I will get the proper result in @Num and return the same.

    Now my issue starts with minus value. I mean, sum(value) if UnqId in (1,2) - Sum(Value) if UnqId in (3). I have to write a single statement to achieve this. I dont know how to do that.

    I hope, I explained my issue clearly. 

    Looking forward for the response.

    M.Muthuraman
    • Post Points: 20
  • 04-17-2008 2:28 PM In reply to

    Re: Help required on SQL query.

    Hi Muthu,

    Please fin the following Query. Table name is Test1.

    Sum of values and specific valus has been calculated saperately two sub queries.

    Condition BusD=@BusD can be concatinated in the end of the query.

    In the SQL statements where clause will be executed first. So we will have filtered values and upon which these two sub queries will be executed.

    Please let me know if my understanding of problem is not correct.

     

    DECLARE @Num AS INT

    SELECT

    @Num = ((SELECT SUM([Value]) FROM Test1 WHERE UniqID in (1,2) AND BusD = tt.BusD)-(SELECT SUM([VALUE]) FROM Test1 WHERE UniqID = 3 AND BusD = tt.BusD))

    FROM Test1 tt

    WHERE BusD=@BusD

     

    Thanks,

    Gowtam

    • Post Points: 20
  • 04-17-2008 3:50 PM In reply to

    • mmr_muthu
    • Top 150 Contributor
    • Joined on 10-16-2007
    • Mumbai
    • Posts 3
    • Points 45

    Re: Help required on SQL query.

    Thanks Gowtam it works.

    We ahve come up with one another solution also,

    FYI,


    declare @Num1 float
    select @Num1 = sum(case when UnqId in (3) then -1*value
    else value
    end )
    from test1 where BusD =@BusD.

    M.Muthuraman
    • Post Points: 5
Page 1 of 1 (3 items)
Powered by Community Server (Commercial Edition), by Telligent Systems