所以,我已经有了这个具有重要数据的表,我正在尝试选择顶级并将其插入名为“TopLevel”的列中
下载数据的样子 顶级组织称为顶级组织,所有较低级别的组织最终都将“顶级”视为最高级别。
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 | -| - | -|
我找不到足够接近的示例,这有助于我了解如何使用字符串进行条件选择/插入。
如果父Org = Top-Level,则应选择Org并将其插入列顶级。在数据库中,如果某些内容没有更多父组织,则Top-Level将显示为父组织。无论在哪里找到顶层,都会选择一级(因此,Food Corp,chuck e.cheese)并将其插入到Toplevel列中。希望这不会太混乱。
这是select语句的半英文版本:
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 ......一个有效的TopLevel专栏将帮助我做一个有意义的分组,因为我不能简单地按父ID进行分组,而不会从较低级别的orgs(他们拥有)中分离顶级组织。
寻找教导一个人捕鱼的反应,包括解释'如何选择org.displayname插入栏''toplevel'在这样的声明中工作?我看一下这些条件陈述,并且无法理解这些条件。请注意,我不知道是否应该在top select子句中加上'select toplevel'。我期待任何回复
答案 0 :(得分:2)
CASE的SQL语法与您的SQL语法略有不同,但您很接近:
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
因此,完整的陈述将是
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
答案 1 :(得分:0)
Frank PI的回复大部分都有效,但我得到一个错误,说mssql_query()(php)没有识别列名ParentOrg_Name和其他列名。我发现这有效,并且别名在case语句中不起作用。所以完整的陈述是这样的:
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
为了练习学习,还有另一种可能的情况,我可以使用CASE,因为orgType返回一个数字,它被映射到一个字符串值。所以这里是两个CASE的陈述(可能和有效)。
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
希望这会帮助别人。