Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Conditional Sum in SQL Query

Conditional Sum in SQL Query

Scheduled Pinned Locked Moved Database
databasesql-serversysadmin
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tufail Ahmad
    wrote on last edited by
    #1

    I use SQL Server 2005. Case is Valsue in Totall field are: -2, 5,4,-1 Select sum(Total) as sTotal from Table1. it gives 6. i want to sum positive and negative values seperatly that is Select sum(Total) as PositiveSum, Sum(total) as NegetiveSum as sTotal from Table1.

    P M N 3 Replies Last reply
    0
    • T Tufail Ahmad

      I use SQL Server 2005. Case is Valsue in Totall field are: -2, 5,4,-1 Select sum(Total) as sTotal from Table1. it gives 6. i want to sum positive and negative values seperatly that is Select sum(Total) as PositiveSum, Sum(total) as NegetiveSum as sTotal from Table1.

      P Offline
      P Offline
      Pranay Rana
      wrote on last edited by
      #2

      use case when statement like sum(case when colvalue<0 then colvalue end) as [Total negative], sum(case when colvalue>=0 then colvalue end) as [Total positive],

      1 Reply Last reply
      0
      • T Tufail Ahmad

        I use SQL Server 2005. Case is Valsue in Totall field are: -2, 5,4,-1 Select sum(Total) as sTotal from Table1. it gives 6. i want to sum positive and negative values seperatly that is Select sum(Total) as PositiveSum, Sum(total) as NegetiveSum as sTotal from Table1.

        M Offline
        M Offline
        Mycroft Holmes
        wrote on last edited by
        #3

        Use a case statement

        Sum(Case when ValueField > 0 then ValueField else 0 end) SumPositive,
        Sum(Case when ValueField < 0 then ValueField else 0 end)

        Never underestimate the power of human stupidity RAH

        1 Reply Last reply
        0
        • T Tufail Ahmad

          I use SQL Server 2005. Case is Valsue in Totall field are: -2, 5,4,-1 Select sum(Total) as sTotal from Table1. it gives 6. i want to sum positive and negative values seperatly that is Select sum(Total) as PositiveSum, Sum(total) as NegetiveSum as sTotal from Table1.

          N Offline
          N Offline
          Niladri_Biswas
          wrote on last edited by
          #4

          Even you can try this declare @t table(field int) insert into @t select -2 union all select 5 union all select 4 union all select -1

          select top 1
          PositiveSum = (select SUM(field) from @t where field >0)
          ,NegetiveSum =(select SUM(field) from @t where field <0)
          from @t

          Output:

          PositiveSum NegetiveSum
          9 -3

          :)

          Niladri Biswas

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups