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?
>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;