Results 1 to 5 of 5

Thread: SQL Queries :Using Computed Columns as Filter

  1. #1

    Thread Starter
    Hyperactive Member oyad's Avatar
    Join Date
    Feb 2003
    Location
    PhoxWare MicroSystems
    Posts
    463

    SQL Queries :Using Computed Columns as Filter

    am trying to use a computed column in filter clause of an SQL query but its giving me errors, i guess its either not possible or am not doing it correctly. Any SQL guru 4 help?
    (just a sample below)

    select DisTable.*,s_name,fun_count(x_items) as myItems
    from DisTable,UserTable
    where DisTable.Name=s_name
    and myItems > 0
    Nobody is smarter than all of us!

  2. #2
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    Re: SQL Queries :Using Computed Columns as Filter

    Use HAVING Clause.
    sql Code:
    1. select DisTable.*,s_name,fun_count(x_items) as myItems
    2. from DisTable,UserTable
    3. where DisTable.Name=s_name
    4. GROUP BY DisTable.*,s_name --instead of *, you need to mention each column separately
    5. HAVING myItems > 0
    The only glitch is if you have way too many columns, adding them to Group by clause could be cumbersome.

    If fun_count() is user-defined function, another (possible) way could be that you can let it return records based on the conditions you want to put here. E.g. In your example, let it send you all records which will be greater than 0. Just a guess though. I don't know how this function looks like.
    Show Appreciation. Rate Posts.

  3. #3

    Thread Starter
    Hyperactive Member oyad's Avatar
    Join Date
    Feb 2003
    Location
    PhoxWare MicroSystems
    Posts
    463

    Re: SQL Queries :Using Computed Columns as Filter

    Quote Originally Posted by Harsh Gupta View Post
    Use HAVING Clause.
    sql Code:
    1. select DisTable.*,s_name,fun_count(x_items) as myItems
    2. from DisTable,UserTable
    3. where DisTable.Name=s_name
    4. GROUP BY DisTable.*,s_name --instead of *, you need to mention each column separately
    5. HAVING myItems > 0
    The only glitch is if you have way too many columns, adding them to Group by clause could be cumbersome.

    If fun_count() is user-defined function, another (possible) way could be that you can let it return records based on the conditions you want to put here. E.g. In your example, let it send you all records which will be greater than 0. Just a guess though. I don't know how this function looks like.

    from your sample, i get error on line 5, myItems is an invalid identifier
    Nobody is smarter than all of us!

  4. #4
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    Re: SQL Queries :Using Computed Columns as Filter

    Sorry for the typo. You cannot use alias names. It should be this:
    Code:
    HAVING fun_count(x_items) > 0
    Show Appreciation. Rate Posts.

  5. #5
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: SQL Queries :Using Computed Columns as Filter

    Moved To Database Development

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width