Performing math operation on temporary column in SQL

后端 未结 2 1526
忘掉有多难
忘掉有多难 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条回答
  •  再見小時候
    2021-01-27 16:27

    Maybe you meant something like this:

    SELECT
      t.t_id,
      t.occurrences,
      t.value / tm.occurrences AS newValue
    FROM table t
      INNER JOIN (
        SELECT
          tm_id,
          COUNT(*) AS occurrences
        FROM table_map
        GROUP BY tm_id
        HAVING COUNT(*) > 1
      ) tm ON t.t_id = tm.tm_id
    

提交回复
热议问题