none
Optimized query to sum some fields using CASE. RRS feed

  • Pergunta

  • Hello,

    I am a begginer and have the following doubt:

    I have a tables with the columns "client", "month", "credit1", "credit2", "credit3". The column month goes from 1 to 3 and I did the queries below:


    SELECT sum(credit1) AS total1
    FROM account
    WHERE month=1
    GROUP BY month;

    SELECT sum(credit2) AS total2
    FROM account
    WHERE month=2
    GROUP BY month;

    SELECT sum(credit3) AS total3
    FROM account
    WHERE month=3
    GROUP BY month;

    The situation is: If my table has thousands of clients and 60 months, how can I optimize the query using CASE ?

    I would appreciate if someone could help me.

    Thanks


    sábado, 2 de fevereiro de 2013 15:36

Todas as Respostas

  • Helo, give us a example of how you intent use case in this solution ?

    []'s | Rodrigo Ribeiro Gomes | MCTS/MCITP Dev/DBA

    segunda-feira, 1 de julho de 2013 18:07
  • You need separeted result sets? If not, you just need the below query

    SELECT month,sum(credit1) AS total1
    FROM account
    GROUP BY month;

    Doing this SQL Server will group by each month.



    Att,
    Marcos Freccia [MTA|MCTS|MCITP|MCT SQL Server 2008]
    Blog|Twitter
    Assine também os feeds clicando aqui

    segunda-feira, 1 de julho de 2013 22:37