May 13, 2017 at 5:14 am
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
May 13, 2017 at 5:35 am
sajjadkaswani 43055 - Saturday, May 13, 2017 5:14 AMSQL Server 2008My Table:
InstructionID
CompanyCode
ProductType (F/I/P/C/…)
AmountOutput :
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
Group by CompamyCode and ProductType
😎
May 13, 2017 at 5:44 am
Eirikur Eiriksson - Saturday, May 13, 2017 5:35 AMsajjadkaswani 43055 - Saturday, May 13, 2017 5:14 AMSQL Server 2008My Table:
InstructionID
CompanyCode
ProductType (F/I/P/C/…)
AmountOutput :
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
Group by CompamyCode and ProductType
😎
I wish to have output like this.
Company Name | Count of F | Sum of F | Count of I | Sum of I | Count of P | Sum of P
Thanks
May 13, 2017 at 5:49 am
sajjadkaswani 43055 - Saturday, May 13, 2017 5:44 AMEirikur Eiriksson - Saturday, May 13, 2017 5:35 AMsajjadkaswani 43055 - Saturday, May 13, 2017 5:14 AMSQL Server 2008My Table:
InstructionID
CompanyCode
ProductType (F/I/P/C/…)
AmountOutput :
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
Group by CompamyCode and ProductType
😎I wish to have output like this.
Company Name | Count of F | Sum of F | Count of I | Sum of I | Count of P | Sum of P
Thanks
I got it
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 MyTableWHERE ...GROUP BY CompanyCodeORDER BY CompanyCode
Thanks
May 15, 2017 at 1:24 am
sajjadkaswani 43055 - Saturday, May 13, 2017 5:49 AMsajjadkaswani 43055 - Saturday, May 13, 2017 5:44 AMEirikur Eiriksson - Saturday, May 13, 2017 5:35 AMsajjadkaswani 43055 - Saturday, May 13, 2017 5:14 AMSQL Server 2008My Table:
InstructionID
CompanyCode
ProductType (F/I/P/C/…)
AmountOutput :
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
Group by CompamyCode and ProductType
😎I wish to have output like this.
Company Name | Count of F | Sum of F | Count of I | Sum of I | Count of P | Sum of P
Thanks
I got it
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 MyTableWHERE ...GROUP BY CompanyCodeORDER BY CompanyCode
Thanks
Great. Glad you found the solution yourself.
Here's an article on the method that you have used which might be useful.
http://www.sqlservercentral.com/articles/T-SQL/63681/
How to post data/code on a forum to get the best help - Jeff Moden
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply