Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Shorthand for group by all columns?

I have a table with 15 columns with customer info and I want to join the sum of their purchases onto the table. I am currently attempting the following join:

SELECT  A.*, SUM(B.SALES_AMT) AS SALES
FROM CUST_INFO A
LEFT JOIN SALES_TABLE B
ON A.Cust_ID = B.Cust_ID
GROUP BY fooa, foob, fooc, etc

Is there a way to group by where I can simply say GROUP BY A.* instead of having to write out all 15 or so columns?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You could move the sum into an apply:

select  A.*, Isnull(B.SALES, 0) SALES
from CUST_INFO A
outer apply (
  select Sum(B.SALES_AMT) SALES
  from SALES_TABLE B
  where B.Cust_ID = A.Cust_ID
)B;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading