大查询-重复的记录分成单独的列

时间:2019-08-03 13:25:30

标签: sql google-bigquery

我已经尝试了很久了。我有一个表,其中包含用户详细信息以及重复记录中的管理员详细信息。我需要的是查询特定用户的记录以及向其报告的人员。 数据例如:

[
  {
    "name": "abc",
    "designation": "team member",
    "designationLevel": "3",
    "reportsTo": [
      {
        "name": "bcd",
        "designation": "team lead",
        "designationLevel": "2"
      },
      {
        "name": "qwe",
        "designation": "manager",
        "designationLevel": "1"
      }
    ]
  },
  {
    "name": "bcd",
    "designation": "team lead",
    "designationLevel": "2",
    "reportsTo": [
      {
        "name": "qwe",
        "designation": "manager",
        "designationLevel": "1"
      }
    ]
  },
  {
    "name": "xyz",
    "designation": "team lead",
    "designationLevel": "2",
    "reportsTo": [
      {
        "name": "qwe",
        "designation": "manager",
        "designationLevel": "1"
      }
    ]
  },
  {
    "name": "pqr",
    "designation": "team member",
    "designationLevel": "3",
    "reportsTo": [
      {
        "name": "qwe",
        "designation": "manager",
        "designationLevel": "1"
      },
      {
        "name": "xyz",
        "designation": "team lead",
        "designationLevel": "2"
      }
    ]
  }
]

我一直在尝试使用查询进行查询,方法是使用左连接和带有designationLevel和ReportsTo.name的where子句

查询获取向经理“ qwe”报告的ppl

SELECT manager.data as manager,teamlead.data as teamlead, teammember.data as teammember
from
(
SELECT "manager" as manager, "teamlead" as teamlead, "teammember" as teammember
) AS data
left join
(
SELECT "manager" as tag, 
concat(IFNULL(id,''),'~',IFNULL(name,''),'~',IFNULL(designation,'')) as data
FROM [some_table]
where reportsTo.name = "qwe" and designationLabel=='manager' 
group by tag, data
) as manager on manager.tag=data.manager
left join
(
SELECT "teamlead" as tag, 
concat(IFNULL(id,''),'~',IFNULL(name,''),'~',IFNULL(designation,'')) as data
FROM [some_table]
where reportsTo.name = "qwe" and designationLabel=='teamlead' 
group by tag,data
) as teamlead on teamlead.tag=data.teamlead
left join
(
SELECT "teammember" as tag, 
concat(IFNULL(id,''),'~',IFNULL(name,''),'~',IFNULL(designation,'')) as data
FROM [some_table]
where reportsTo.name = "qwe" and designationLabel=='teammember' 
group by tag,data
) as teammember on teammember.tag=data.teammember

这给了我所有用户的交叉连接。理想情况下,根据我应该获得的数据

编辑输出:

manager | teamlead | teammember
qwe     | bcd      | abc 
qwe     | xyz      | pqr

但是我觉得会有一个最佳的方式来做到这一点?请提出建议。

1 个答案:

答案 0 :(得分:0)

以下是用于BigQuery标准SQL

#standardSQL
WITH temp AS (
  SELECT t.name, t.designation FROM `project.dataset.table` t,
  UNNEST(reportsTo) r WHERE r.name = 'qwe'
)
SELECT * EXCEPT(line) 
FROM (
  SELECT name AS manager, ROW_NUMBER() OVER() line FROM temp WHERE designation = 'manager'
) 
FULL OUTER JOIN (
  SELECT name AS teamlead, ROW_NUMBER() OVER() line FROM temp WHERE designation = 'teamlead'
) USING(line) 
FULL OUTER JOIN (
  SELECT name AS teammember, ROW_NUMBER() OVER() line FROM temp WHERE designation = 'teammember'
) USING(line)    

如果要应用于您的问题的样本数据-结果为

Row manager teamlead    teammember   
1   null    bcd         abc  
2   null    xyz         null