SQLTeam.com | Weblogs | Forums

Sql Query


#1

SQL Server 2008

My Table:

InstructionID
CompanyCode
ProductType (F/I/P/C/…)
Amount

Output :

I wish to have a query to return output like

CompanyCode, Count of F, Sum of F, Count of I, Sum of I, Count of P, Sum of P , Count of C, Sum of C

Kindly assist me

Thanks


#2
SELECT CompanyCode
       , [CountOfF] = SUM(CASE WHEN ProductType = 'F' THEN 1 ELSE 0 END)
       , [SumOfF] = SUM(CASE WHEN ProductType = 'F' THEN Amount ELSE 0 END)
       ...
FROM MyTable
WHERE ...
GROUP BY CompanyCode
ORDER BY CompanyCode

#3

Thank you alot