Sql Query

  • 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

  • sajjadkaswani 43055 - Saturday, May 13, 2017 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

    Group by CompamyCode and ProductType
    😎

  • Eirikur Eiriksson - Saturday, May 13, 2017 5:35 AM

    sajjadkaswani 43055 - Saturday, May 13, 2017 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

    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

  • sajjadkaswani 43055 - Saturday, May 13, 2017 5:44 AM

    Eirikur Eiriksson - Saturday, May 13, 2017 5:35 AM

    sajjadkaswani 43055 - Saturday, May 13, 2017 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

    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

  • sajjadkaswani 43055 - Saturday, May 13, 2017 5:49 AM

    sajjadkaswani 43055 - Saturday, May 13, 2017 5:44 AM

    Eirikur Eiriksson - Saturday, May 13, 2017 5:35 AM

    sajjadkaswani 43055 - Saturday, May 13, 2017 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

    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/


    Kingston Dhasian

    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