Evgeniy Kleban Evgeniy Kleban - 3 months ago 9
SQL Question

output specific columns in SQL

I want to output only 2 rows - title and sum. Title comes from orbiting_group_types table, while SUM is calculating during sql query. Here is my query that output all orbiting_group_types joined sum column:

SELECT
*
FROM
orbiting_group_types ogt
LEFT JOIN (
SELECT
SUM (val),
orbiting_group_type_id
FROM
report_orbiting_vals subrov
WHERE
subrov.orbiting_group_type_id = 4
GROUP BY
orbiting_group_type_id
) rov ON ogt. ID = rov.orbiting_group_type_id


I want to output only title and sum columns. How should i modify my query for that?

Answer

Try this :

SELECT
    ogt.title,
    rov.summ 
FROM
orbiting_group_types ogt
LEFT JOIN (
  SELECT
    SUM (val) AS summ,
    orbiting_group_type_id
  FROM
    report_orbiting_vals subrov
  WHERE
    subrov.orbiting_group_type_id = 4
 GROUP BY
    orbiting_group_type_id
) rov ON ogt. ID = rov.orbiting_group_type_id