DB2 – How to Select All with Other Parameters

db2

What is the equivalent query for

SELECT *,SUM(total_request_count) AS total_calls  FROM API_REQUEST_SUMMARY

in DB2. It says * is invalid in db2 data explorer. After running this I got below error

An unexpected token "*" was found following "SELECT ". Expected tokens may include: "NULL".. SQLCODE=-104, SQLSTATE=42601, DRIVER=3.68.61

Any Idea to resolve this is much appreciated.

Best Answer

any not aggregated column must be included in GROUP BY

SELECT COL1, COL2 ,SUM(total_request_count) AS total_calls  FROM API_REQUEST_SUMMARY GROUP BY COL1, COL2

alternative if many columns for GROUP BY

SELECT T1.*, T2.total_calls FROM API_REQUEST_SUMMARY T1 LEFT JOIN
(SELECT GROUP_COLUMN, SUM(total_request_count) AS total_calls  FROM API_REQUEST_SUMMARY GROUP BY GROUP_COLUMN) T2 ON T1.GROUP_COLUMN = T2.GROUP_COLUMN

it is my work solution, may be other present as well