Sub Select

  • Would it be possible to use a sub-select to combine these two statements? I have tried to encapsulate each with parenthesis but that did not work. Any suggestions?  TIA.

     

    select

    (select cast (count (*)as decimal) from t_TRANSACTION where status_id = '15854' and active = 1) /

    (select cast (count(*) as decimal) from t_TRANSACTION where status_id in(15854,15750) and active = 1)*100  Card_Percentage

    Select                                                                                            (select cast (count (*)as decimal) from t_TRANSACTION where status_id = '15750' and active = 1) /

    (select cast (count(*) as decimal) from t_TRANSACTION where status_id in(15854,15750) and active = 1)*100  Check_Percentage

  • This could work :

    Select

    (select

    (select cast (count (*)as decimal) from t_TRANSACTION where status_id = '15854' and active = 1) /

    (select cast (count(*) as decimal) from t_TRANSACTION where status_id in(15854,15750) and active = 1)*100) as Card_Percentage

    ,

    (Select (select cast (count (*)as decimal) from t_TRANSACTION where status_id = '15750' and active = 1) /

    (select cast (count(*) as decimal) from t_TRANSACTION where status_id in(15854,15750) and active = 1)*100) as Check_Percentage

  • You could also look up the union operator :

    (union all)

    Select 1 as Demo

    union all

    Select 2 as Demo1

    union all

    Select 1 as Demo1

    ----

    1

    2

    1

    (union)

    Select 1 as Demo2

    union

    Select 2 as Demo1

    union

    Select 1 as Demo1

    --

    1

    2

    as you can see union actually does a distinct on the final recordset which can be bad for performance if there are no tuple to filter.

  • Right on Remi! I looked at the BOL but it isn't always clear. Eventually I'll get beyond my novice ability and hopefully I won't have to bother anyone. Thanks!

  • You're not bothering anyone... you're not the first one to wonder how to do this... and hopefully it'll help somebody else in the future.

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply