SQL Query to sum accross the row

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cris1978
    New Member
    • Sep 2007
    • 7

    #1

    SQL Query to sum accross the row

    Hi,

    Can someone help to write a query that will sum the information across the row, not down the column.

    nome_item Sunday Monday Tuesday Wednesday Thursday Friday Total
    item1 12 5 5 9 4 23 58
    item2 2 6 2 2 45 3 60
    item3 3 7 3 3 4 3 23
    item4 4 8 4 4 4 5 29
  • azimmer
    Recognized Expert New Member
    • Jul 2007
    • 200

    #2
    Originally posted by cris1978
    Hi,

    Can someone help to write a query that will sum the information across the row, not down the column.

    nome_item Sunday Monday Tuesday Wednesday Thursday Friday Total
    item1 12 5 5 9 4 23 58
    item2 2 6 2 2 45 3 60
    item3 3 7 3 3 4 3 23
    item4 4 8 4 4 4 5 29
    You just add up the columns like this:
    Code:
    SELECT nome_item, Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, 
    [B]Sunday+Monday+Tuesday+Wednesday+Thursday+Friday as Total[/B]
    FROM mytable

    Comment

    • cris1978
      New Member
      • Sep 2007
      • 7

      #3
      Originally posted by azimmer
      You just add up the columns like this:
      Code:
      SELECT nome_item, Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, 
      [B]Sunday+Monday+Tuesday+Wednesday+Thursday+Friday as Total[/B]
      FROM mytable
      ------------------------------------------------------------------------------------------------------------

      I run the query, but it's the results of the Total column is '0'.

      Do you have any idea?

      Comment

      • azimmer
        Recognized Expert New Member
        • Jul 2007
        • 200

        #4
        Originally posted by cris1978
        ------------------------------------------------------------------------------------------------------------

        I run the query, but it's the results of the Total column is '0'.

        Do you have any idea?
        It works fine (I tried) so long as your days columns are of numeric type (i.e. int). If they are string (char, varchar -- why?), you need to cast them like this:
        Code:
        SELECT nome_item, Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, 
        cast(Sunday as int)+...+cast(Friday as int) as Total
        FROM mytable

        Comment

        • cris1978
          New Member
          • Sep 2007
          • 7

          #5
          Originally posted by azimmer
          It works fine (I tried) so long as your days columns are of numeric type (i.e. int). If they are string (char, varchar -- why?), you need to cast them like this:
          Code:
          SELECT nome_item, Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, 
          cast(Sunday as int)+...+cast(Friday as int) as Total
          FROM mytable
          _______________ _______________ _______________ _____________

          Thanks!!!

          This works very fine!!

          Comment

          Working...