order by calculated value

Ask a Question related to Coldfusion Database Access, Design and Development.

  1. #1

    Default order by calculated value

    i'm running 2 count(*) subqueries and want to order by the difference between
    the 2.

    select
    column_a,
    (select count(*) from blah where x = y) as test_1,
    (select count(*) from blah where x = z) as test_2
    where
    a = b
    order by
    [the difference between test_1 and test_2] desc


    please help! :) if you need clarification let me know.

    tragik Guest

  2. Similar Questions and Discussions

    1. Calculated Value on HTML Page?
      Please help...! I am in need of a solution that would allow me to display a numberical value on the homepage of a site I'm developing - with that...
    2. Problems with using a calculated value in my WHERE clause
      Hi All If I run the below query: SELECT s.THUMBPIC, sd.STOCKNAME, s.STOCKID, sm.MANUNAME, s.QTYINSTOCK, (sp.NETAMOUNT + sp.TAXAMOUNT) AS PRICE...
    3. Q. How to format a calculated result
      FileMaker 6, OS X 10.2.x I can not seem to get the exported text from a calculated field to use the thousands separator (comma). The export is...
    4. Insert TAB into calculated field
      I'm trying to create a function with FM Pro 4.1 where a user can click a button on a screen causing a group of five fields to be copied into the...
    5. Calculated Field in a Form
      I have a query that calculates a special total by account number. I would like to create a text box in the maintenance form so that when someone...
  3. #2

    Default Re: order by calculated value

    You have to order by a field in one of the tables in your select clause. You
    can't order by a number, unless that number represents the 1st, 2nd, etc field
    in your select clause.

    In your example, if test1 turned out to be 50, and test2 was 5, your query
    would effectively be:

    select column_a, 50, 5
    from mytable
    order by 45

    On the bright side, if your db supports sub-queries in the select clause, you
    have the right idea. Your query needs a from clause of course.

    Dan Bracuk Guest

  4. #3

    Default Re: order by calculated value

    sorting by a static variable won't produce any actual sorting; sorting by a
    calculated field that changes based on the relational value will produce a
    difference calculation for each row, will work fine.

    maybe you just misunderstood what i was after..and yah i left out the from by
    mistake ;)

    select
    z.column_a,
    (select count(*) from blah where x = z.y) as test_1,
    (select count(*) from blah where x = z.z) as test_2
    from
    table_c z
    where
    z.a = '#b#'
    order by
    [the difference between test_1 and test_2] desc

    hope that clears up any confusion!


    tragik Guest

  5. #4

    Default Re: order by calculated value

    Try this:
    select
    z.column_a,
    (select count(*) from blah where x = z.y) as test_1,
    (select count(*) from blah where x = z.z) as test_2, test_1 - test_2 as diff
    from
    table_c z
    where
    z.a = '#b#'
    order by diff


    Dan Bracuk Guest

  6. #5

    Default Re: order by calculated value

    Upon further review, having the subqueries in the select clause makes no sense
    because they return the same number in every row. You have to put them in the
    where clause, and include a field on which to join.

    select field1, thecount
    from table1 join
    (select field1, count(field1) as the count
    from
    sometables
    group by field1) x using (field1)
    etc

    The syntax is db specific.

    Dan Bracuk Guest

  7. #6

    Default Re: order by calculated value

    Just create a 4th column in your SELECT where you determine the difference
    between column 2 and 3, then use positional notation in the ORDER BY, so that
    you are sorting on column 4, which is actually a calculation.

    SELECT column_a,
    (SELECT COUNT(*) FROM blah WHERE x = y) as test_1,
    (SELECT COUNT(*) FROM blah WHERE x = z) as test_2,
    (SELECT COUNT(*) FROM blah WHERE x = y)-(SELECT COUNT(*) FROM blah WHERE x =
    z) as diff
    FROM your_table
    WHERE
    a = b
    ORDER BY 4 DESC

    Phil

    paross1 Guest

  8. #7

    Default Re: order by calculated value

    i was thinking along the lines of setting the subqueries as variables to avoid another subquery, but that works just fine.

    thanks! :)


    tragik Guest

  9. #8

    Default Re: order by calculated value

    Test_1 and test_2 are column aliases, not "variables". Most databases that I
    know of usually do not allow you to use the column aliases in the ORDER BY or
    GROUP BY clauses, so you usually need to repeat the same expression as used in
    the select statement, or in the case of ORDER BY, you can use the relative
    "column" position number.

    Phil

    paross1 Guest

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139