Performing math operation on temporary column in SQL

后端 未结 2 1531
忘掉有多难
忘掉有多难 2021-01-27 15:42

What I\'m trying to do is have it where I get the occurances on an id in the table, and then based upon the amount of occurances, divide the value of another column by that valu

2条回答
  •  旧时难觅i
    2021-01-27 16:04

    Just throw it in a sub-query and do the math after that:

    SELECT t_id, occurances, value/occurances AS newValue
    from (
        SELECT t.t_id, count(DISTINCT tm.tm_id) AS occurances, t.value
        FROM table t
        INNER JOIN table_map tm ON t.t_id = tm.tm_id
        GROUP BY t.t_id
        HAVING occurances > 1) t
    

提交回复
热议问题