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. adding Group by result to each other

adding Group by result to each other

Scheduled Pinned Locked Moved Database
sql-servercomtutorial
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.
  • E Offline
    E Offline
    Erdinc27
    wrote on last edited by
    #1

    i have a table which was created in MsSql 2005 Express.. http://img441.imageshack.us/f/numaralar.jpg/[^] i have numbers in 6 columns..i can group every column and count which number was selected how many times in columns... but for example number 10 exist in column1 and column2 and column3 also..how i can add that count results that i found for every column seperately to each other to find the total of the number 10 selected ...

    vemedya.com

    G 1 Reply Last reply
    0
    • E Erdinc27

      i have a table which was created in MsSql 2005 Express.. http://img441.imageshack.us/f/numaralar.jpg/[^] i have numbers in 6 columns..i can group every column and count which number was selected how many times in columns... but for example number 10 exist in column1 and column2 and column3 also..how i can add that count results that i found for every column seperately to each other to find the total of the number 10 selected ...

      vemedya.com

      G Offline
      G Offline
      Goutam Patra
      wrote on last edited by
      #2

      Are you looking for something like this

      SELECT

      CASE WHEN NUM1 = 10 THEN 1 ELSE 0 END +
      CASE WHEN NUM2 = 10 THEN 1 ELSE 0 END +
      CASE WHEN NUM3 = 10 THEN 1 ELSE 0 END +
      CASE WHEN NUM4 = 10 THEN 1 ELSE 0 END +
      CASE WHEN NUM5 = 10 THEN 1 ELSE 0 END +
      CASE WHEN NUM6 = 10 THEN 1 ELSE 0 END
      AS TOTAL

      FROM MYTABLE

      E 1 Reply Last reply
      0
      • G Goutam Patra

        Are you looking for something like this

        SELECT

        CASE WHEN NUM1 = 10 THEN 1 ELSE 0 END +
        CASE WHEN NUM2 = 10 THEN 1 ELSE 0 END +
        CASE WHEN NUM3 = 10 THEN 1 ELSE 0 END +
        CASE WHEN NUM4 = 10 THEN 1 ELSE 0 END +
        CASE WHEN NUM5 = 10 THEN 1 ELSE 0 END +
        CASE WHEN NUM6 = 10 THEN 1 ELSE 0 END
        AS TOTAL

        FROM MYTABLE

        E Offline
        E Offline
        Erdinc27
        wrote on last edited by
        #3

        thanks for reply friend..when i write that code

        SELECT num1,count(*) FROM NumaraBilgileri
        GROUP BY num1

        i got that http://img827.imageshack.us/f/num1column.png/[^] as u see i have 22 numbers there..it is just for column1 and i have 5 more columns.. i mean that after that Select executed i have number 10 here 4 times..and in column2 i have 5 times number 10 so total makes 9..and so on.. i want to get the all numbers total in that table from 1 to 49..how i can get it

        vemedya.com

        G 1 Reply Last reply
        0
        • E Erdinc27

          thanks for reply friend..when i write that code

          SELECT num1,count(*) FROM NumaraBilgileri
          GROUP BY num1

          i got that http://img827.imageshack.us/f/num1column.png/[^] as u see i have 22 numbers there..it is just for column1 and i have 5 more columns.. i mean that after that Select executed i have number 10 here 4 times..and in column2 i have 5 times number 10 so total makes 9..and so on.. i want to get the all numbers total in that table from 1 to 49..how i can get it

          vemedya.com

          G Offline
          G Offline
          Goutam Patra
          wrote on last edited by
          #4

          Then try using UNION Query like

          SELECT A.NUM, SUM(A.TOTAL) AS TOTAL FROM (
          SELECT num1 AS NUM,count(*) AS TOTAL FROM NumaraBilgileri
          GROUP BY num1
          UNION ALL
          SELECT num2 AS NUM,count(*) AS TOTAL FROM NumaraBilgileri
          GROUP BY num2
          ) AS A
          GROUP BY A.NUM

          You need to create union for all fields.

          E 1 Reply Last reply
          0
          • G Goutam Patra

            Then try using UNION Query like

            SELECT A.NUM, SUM(A.TOTAL) AS TOTAL FROM (
            SELECT num1 AS NUM,count(*) AS TOTAL FROM NumaraBilgileri
            GROUP BY num1
            UNION ALL
            SELECT num2 AS NUM,count(*) AS TOTAL FROM NumaraBilgileri
            GROUP BY num2
            ) AS A
            GROUP BY A.NUM

            You need to create union for all fields.

            E Offline
            E Offline
            Erdinc27
            wrote on last edited by
            #5

            thanks for the reply man..that was the what exactly i mean now it works for all columns

            vemedya.com

            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