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
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Sum of all rows

Sum of all rows

Scheduled Pinned Locked Moved Database
databasehelp
5 Posts 2 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.
  • C Offline
    C Offline
    CodingLover
    wrote on last edited by
    #1

    Hi all, Please have a look at the following query.

    select avg(family_income) as income
    from family_income
    where income_type = 1 and sample_id = 10 group by family_income_name_id

    family_income table has income details with different types. Through group by I can find the average of different types. Now I want to add all the averages. sum(...) doesn't work for me. Any comments really appreciate. thanks

    I appreciate your help all the time... CodingLover :)

    W 1 Reply Last reply
    0
    • C CodingLover

      Hi all, Please have a look at the following query.

      select avg(family_income) as income
      from family_income
      where income_type = 1 and sample_id = 10 group by family_income_name_id

      family_income table has income details with different types. Through group by I can find the average of different types. Now I want to add all the averages. sum(...) doesn't work for me. Any comments really appreciate. thanks

      I appreciate your help all the time... CodingLover :)

      W Offline
      W Offline
      Wendelius
      wrote on last edited by
      #2

      if you want to add all the averages without grouping can you simply use an inline view:

      select sum(iv.income)
      from (
      select avg(family_income) as income
      from family_income
      where income_type = 1
      and sample_id = 10
      group by family_income_name_id) iv

      The need to optimize rises from a bad design.My articles[^]

      C 1 Reply Last reply
      0
      • W Wendelius

        if you want to add all the averages without grouping can you simply use an inline view:

        select sum(iv.income)
        from (
        select avg(family_income) as income
        from family_income
        where income_type = 1
        and sample_id = 10
        group by family_income_name_id) iv

        The need to optimize rises from a bad design.My articles[^]

        C Offline
        C Offline
        CodingLover
        wrote on last edited by
        #3

        Thanks a lot. It works fine as I expected. I'm not clever with views in SQL. Could you have any articles written by your related to views?

        I appreciate your help all the time... CodingLover :)

        W 1 Reply Last reply
        0
        • C CodingLover

          Thanks a lot. It works fine as I expected. I'm not clever with views in SQL. Could you have any articles written by your related to views?

          I appreciate your help all the time... CodingLover :)

          W Offline
          W Offline
          Wendelius
          wrote on last edited by
          #4

          CodingLover wrote:

          Thanks a lot. It works fine as I expected

          No problem.

          CodingLover wrote:

          Could you have any articles written by your related to views?

          Good idea, perhaps someday I'll contribute an article touching this subject.

          The need to optimize rises from a bad design.My articles[^]

          C 1 Reply Last reply
          0
          • W Wendelius

            CodingLover wrote:

            Thanks a lot. It works fine as I expected

            No problem.

            CodingLover wrote:

            Could you have any articles written by your related to views?

            Good idea, perhaps someday I'll contribute an article touching this subject.

            The need to optimize rises from a bad design.My articles[^]

            C Offline
            C Offline
            CodingLover
            wrote on last edited by
            #5

            Mika Wendelius wrote:

            Good idea, perhaps someday I'll contribute an article touching this subject.

            Yeah. If I learn the stuff correctly, I'll give a try too. :)

            I appreciate your help all the time... CodingLover :)

            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