对于问题的错误说明感到抱歉,但问题很难用几句话来解释,同时考虑到我不是SQL的专家。
我有一个包含三个字段的数据集:
ID SECTOR GRADE
1 aaa Junior
2 aaa Senior
3 aaa Director1
4 bbb Junior
5 bbb Senior
6 bbb Director2
ID指的是不同的人,等级,显然是组织内的等级。
我试图通过添加另一个描述每个ID的director的列来获得以下输出。对于扇区'aaa'我们有'Director1',对于扇区'bbb'我们有'Director2'。因此我想:
ID SECTOR GRADE DIRECTOR
1 aaa Junior Director1
2 aaa Senior Director1
3 aaa Director1 director1
4 bbb Junior Director2
5 bbb Senior Director2
6 bbb Director2 Director2
我只能考虑光标,但我确信有一种简单的方法可以这样做。
结构是:
SELECT R.ID, D.SECTOR, D.Grade
FROM Resource
LEFT JOIN D.Dimension on D.ID=R.ID
答案 0 :(得分:0)
您可以使用以下内容构建临时董事表:
SELECT D.SECTOR AS SECTOR, D.Grade AS DIRECTOR
FROM D.Dimension
WHERE D.Grade LIKE 'Director%'
然后您可以使用“部门到导演”表格JOIN
原始查询:
SELECT R.ID, D.SECTOR, D.Grade, Dir.DIRECTOR
FROM Resource
LEFT JOIN D.Dimension on D.ID=R.ID
JOIN (
SELECT D.SECTOR AS SECTOR, D.Grade AS DIRECTOR
FROM D.Dimension
WHERE D.Grade LIKE 'Director%'
) AS Dir ON Dir.SECTOR = D.SECTOR