Suppose I have two tables PO
and PO_Line
and I want to list all fields from PO
plus the quantity of rows from PO_Line
that link back to each row in PO
I would write a query something like -
SELECT
PO.propA,
PO.propB,
PO.propC,
PO.propD,
PO.propE,
...
PO.propY,
COUNT(PO_Line.propA) LINES
FROM
PO
LEFT JOIN
PO_Lines
ON
PO.ID = PO_Lines.PO_ID
Obviously this would give an error someting along the lines of -
Column 'PO.propA' is invaalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
So to get the query to run I will add a GROUP BY
clause to the end of the query and copy and paste my select lines, like so -
SELECT
PO.propA,
PO.propB,
PO.propC,
PO.propD,
PO.propE,
...
PO.propY,
COUNT(PO_Line.propA) LINES
FROM
PO
LEFT JOIN
PO_Lines
ON
PO.ID = PO_Lines.PO_ID
GROUP BY
PO.propA,
PO.propB,
PO.propC,
PO.propD,
PO.propE,
...
PO.propY
Which works perfectly however it all feels a little unwieldy, especially if I've named my columns i.e. -
SELECT
PO.propA AS 'FIRST PROPERTY',
PO.propB AS 'SECOND PROPERTY',
PO.propC AS 'THIRD PROPERTY',
PO.propD AS 'ANOTHER PROPERTY',
PO.propE AS 'YOU GET THE IDEA',
...
PO.propY
and I have to copy/paste the entries from the select clause and then delete the column names.
So my question is - Is there a shorthand method to say group by all non-aggregated entries found in the select clause?