Question

So, I've got this table with hierchical data, and I'm trying to select the top-level and insert it into a column called 'TopLevel'

Heres what the data would look like The top org is called Top-Level, and all lower levels have 'Top-Level' as the very top level, eventually.

Organization | Parent Org | GParent Org | GGParent Org | Toplevel
Top-Level | - | - | -| - |
Food Corp | Top-Level | - | -| - |
Taco Bell | Food Corp | Top-Level |  -| - |
Taco Bell Express | TacoBell | Food Corp | Top-Level | - |
KFC | Food Corp | Top-Level | -| - |
Chuck e. Cheese | Top-Level | -| - | -| 

I can't find a close enough example that helps me understand how to do conditional selects/inserts with a string.

on the condition that Parent Org = Top-Level, Org should be selected and inserted into column top-level. In the database, if something has no more parent organizations, Top-Level appears as the parent. Wherever top-level is found, one level down(so, Food Corp, chuck e. cheese) would be selected and inserted into the column Toplevel. Hope that isn't too confusing.

Here's the semi-english version of the select statement:

select Org.displayName, Parent.displayName as ParentOrg_Name, GParent.displayName as gParentOrg_Name,GGParent.displayName as ggParentOrg_Name, Org.uid, Org.accountType, Org.orgType,Parent.orgType, GParent.orgType

CASE
          If Org.ParentOrg_Name == Top-Level, 
             then toplevelorg = Org.Displayname
          elseif gParentOrg_Name == Top-Level, 
              then toplevelorg = ParentOrg_Name
         elseif ggparentOrg_Name == Top-Level,
             then toplevelorg = gParentOrg_Name
           else toplevelorg = ggparentOrgName

     from dbo.Organization Org
     LEFT OUTER JOIN dbo.Organization Parent ON Org.parentOrganization_id = Parent.id
     LEFT OUTER JOIN dbo.Organization GParent ON Parent.parentOrganization_id = GParent.id
     LEFT OUTER JOIN dbo.Organization GGParent ON GParent.parentOrganization_id = GGParent.id

MySQL, PHP... A working TopLevel column will help me do a meaningful group-by, as I cannot simply group by parent ID without seperating the top level orgs from the lower level orgs(that they own).

Looking for teach a man to fish response, including an explaination of 'how does the select org.displayname insert into column 'toplevel' work in a statement like this? I look at these conditional statements and have trouble understanding the THEN parts'. Note that I don't know if I should put 'select toplevel' in the top select clause. I look forward to any response

Was it helpful?

Solution

The SQL syntax for CASE is lightly different from yours, but you were close:

CASE
    WHEN Org.ParentOrg_Name = 'Top-Level' 
         then Org.Displayname
    WHEN gParentOrg_Name = 'Top-Level' 
         then ParentOrg_Name
    WHEN ggparentOrg_Name = 'Top-Level'
         then  gParentOrg_Name
    ELSE ggparentOrgName
END AS toplevelorg

Thus, the full statement would be

select Org.displayName,
       Parent.displayName as ParentOrg_Name,
       GParent.displayName as gParentOrg_Name,
       GGParent.displayName as ggParentOrg_Name,
       Org.uid,
       Org.accountType,
       Org.orgType,Parent.orgType,
       GParent.orgType,
       CASE
           WHEN Org.ParentOrg_Name = 'Top-Level' 
                then Org.Displayname
           WHEN gParentOrg_Name = 'Top-Level' 
                then ParentOrg_Name
           WHEN ggparentOrg_Name = 'Top-Level'
                then  gParentOrg_Name
           ELSE ggparentOrgName
       END AS toplevelorg

     from dbo.Organization Org
     LEFT OUTER JOIN dbo.Organization Parent ON Org.parentOrganization_id = Parent.id
     LEFT OUTER JOIN dbo.Organization GParent ON Parent.parentOrganization_id = GParent.id
     LEFT OUTER JOIN dbo.Organization GGParent ON GParent.parentOrganization_id = GGParent.id

OTHER TIPS

Frank PI's response mostly worked, but I got an error saying mssql_query() (php) didnt recognise the column name ParentOrg_Name and other colum names. I found that this worked, and that the aliases weren't working within the case statement. So the full statement is this:

select Org.displayName as Organization_Name, Parent.displayName as ParentOrg_Name,
GParent.displayName as gParentOrg_Name,GGParent.displayName as ggParentOrg_Name,
     Org.uid, Org.accountType, Org.orgType,
     CASE WHEN Parent.displayName = 'Top-Level' then Org.Displayname
         when GParent.displayName = 'Top-Level' then Parent.displayName
         when ggParent.displayName = 'Top-Level' then gParent.displayName
         else ggParent.displayName
    END AS toplevelorg
     from HQDevBox.dbo.Organization Org
     LEFT OUTER JOIN HQDevBox.dbo.Organization Parent ON Org.parentOrganization_id = Parent.id
     LEFT OUTER JOIN HQDevBox.dbo.Organization GParent ON Parent.parentOrganization_id = GParent.id
     LEFT OUTER JOIN HQDevBox.dbo.Organization GGParent ON GParent.parentOrganization_id = GGParent.id

To excercise the learning, there was another possible case where I could use CASE, because orgType returns a number, which is mapped to a string value. So here's the statement with both CASE's(is possible and works).

select Org.displayName as Organization_Name, Parent.displayName as ParentOrg_Name,
GParent.displayName as gParentOrg_Name,GGParent.displayName as ggParentOrg_Name,
      CASE WHEN Parent.displayName = 'Top-Level' then Org.Displayname
         when GParent.displayName = 'Top-Level' then Parent.displayName
         when ggParent.displayName = 'Top-Level' then gParent.displayName
         else ggParent.displayName
         END AS toplevelorg,
      Org.uid, Org.accountType,
      CASE WHEN Org.orgType = 0 then 'Agency'
           when Org.orgType = 1 then 'Essential'
           when Org.orgType = 2 then 'Power'
           when Org.orgType = 3 then 'Sub_Agency'
           when Org.orgType = 4 then 'Sub_Organization'
           END AS orgType
     from HQDevBox.dbo.Organization Org
     LEFT OUTER JOIN HQDevBox.dbo.Organization Parent ON Org.parentOrganization_id = Parent.id
     LEFT OUTER JOIN HQDevBox.dbo.Organization GParent ON Parent.parentOrganization_id = GParent.id
     LEFT OUTER JOIN HQDevBox.dbo.Organization GGParent ON GParent.parentOrganization_id = GGParent.id

Hopefully this will help someone else.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top