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. Total Count

Total Count

Scheduled Pinned Locked Moved Database
3 Posts 3 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.
  • N Offline
    N Offline
    NET India
    wrote on last edited by
    #1

    Hello Friends, I've a table as shown below ID RefID 1 0 2 1 3 2 4 1 5 1 and i want to display it's output as shown below but i'm not getting the way please suggest a way ID RefID Total 1 0 3 2 1 1 3 2 0 4 1 0 5 1 0

    M N 2 Replies Last reply
    0
    • N NET India

      Hello Friends, I've a table as shown below ID RefID 1 0 2 1 3 2 4 1 5 1 and i want to display it's output as shown below but i'm not getting the way please suggest a way ID RefID Total 1 0 3 2 1 1 3 2 0 4 1 0 5 1 0

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

      This is one solution if the table is small, it requires a seperate sub select for each record

      Select A.ID, A.RefID, (Select count(*) from Table where RefID = ID) Total
      From Table

      For a more efficient method on a large table I would use a LEFT join to itself on TableA.ID = TableB.RefID then use Isnull, a case statement and count to get the same result from a large table.

      Never underestimate the power of human stupidity RAH

      1 Reply Last reply
      0
      • N NET India

        Hello Friends, I've a table as shown below ID RefID 1 0 2 1 3 2 4 1 5 1 and i want to display it's output as shown below but i'm not getting the way please suggest a way ID RefID Total 1 0 3 2 1 1 3 2 0 4 1 0 5 1 0

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

        Try this

        declare @tbl table(id int identity, refid int)
        insert into @tbl
        select 0 union all select 1 union all
        select 2 union all select 1 union all
        select 1

        select t.id,t.refid,case when x.cnt is null then 0 else x.cnt end as total
        from @tbl t
        left join
        (
        select refid,count(refid) as cnt
        from @tbl
        where refid <> 0
        group by refid
        having (count(refid)>0)) x
        on t.id = x.refid

        Output : id refid total

        1 0 3
        2 1 1
        3 2 0
        4 1 0
        5 1 0

        :)

        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