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. Need help on this query

Need help on this query

Scheduled Pinned Locked Moved Database
helpdatabasedata-structuresquestion
2 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.
  • F Offline
    F Offline
    Filipe Peixinho
    wrote on last edited by
    #1

    Hello all. I'm almost getting my head to the wall :confused: because I can't get to the solution for a query I need to do. Is anyone here wanting to help me ? Let me explain the problem: I have a table named T1 with the following structure: ID | Subject | ParentID This table represents info in a form of tree and when I'm at the top branch the ID and ParentID fields have the same value. If I have the following data ID Subject ParentID 1 Sub1 1 2 Sub2 2 3 Sub1_1 1 4 Sub1_2 1 5 Sub1_1_1 3 I want to make a query with the following result: ID|Subject|NumberOfChildRecords e.g.: 1 | Sub1 | 2 2 | Sub2 | 0 3 | Sub1_1 | 1 4 | Sub1_2 | 0 5 | Sub1_1_1 | 0 Is this simple to do ? Thanks in advance for the attention you are having reading this.

    J 1 Reply Last reply
    0
    • F Filipe Peixinho

      Hello all. I'm almost getting my head to the wall :confused: because I can't get to the solution for a query I need to do. Is anyone here wanting to help me ? Let me explain the problem: I have a table named T1 with the following structure: ID | Subject | ParentID This table represents info in a form of tree and when I'm at the top branch the ID and ParentID fields have the same value. If I have the following data ID Subject ParentID 1 Sub1 1 2 Sub2 2 3 Sub1_1 1 4 Sub1_2 1 5 Sub1_1_1 3 I want to make a query with the following result: ID|Subject|NumberOfChildRecords e.g.: 1 | Sub1 | 2 2 | Sub2 | 0 3 | Sub1_1 | 1 4 | Sub1_2 | 0 5 | Sub1_1_1 | 0 Is this simple to do ? Thanks in advance for the attention you are having reading this.

      J Offline
      J Offline
      jasncab
      wrote on last edited by
      #2

      I dont think it is simple... but you could certainly try something like: Here is how you would do it using NW: select a.employeeid, count(b.employeeid) from employees a, employees b where a.employeeid = b.reportsto group by a.employeeid What we have is a self referencing table. What we need to do is essentially add the table twice in the from clause with two seperate aliases and then correlate the two in the where. I should be using ANSI standard joins here, but it was quick. so for you we need something like select a.id, count(b.id) from T1 a, T1 b where a.id = b.parentid group by a.id; Let me know if this works. http://www.jasncab.com/huberblog :: Jason Huber

      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