如何通过SQL连接查询构建嵌套结构结果

时间:2019-02-13 16:33:50

标签: google-bigquery

我在BigQuery中有Employee和Employee Detail表。在使用SQL查询连接这两个表时,我希望结果集(如嵌套结构,EmployeeName和EmployeeDetails)位于单行中。

请提供一些使用查询来构建此嵌套结构的想法。下面的查询需要使用嵌套结构结果进行修改。

select EmployeeName, EmployeeDetail1, EmployeeDetail2 from Table1 t1, Table2 t2 where t1.Id = t2.Id
GROUP BY EmployeeName, EmployeeDetail1, EmployeeDetail2

2 个答案:

答案 0 :(得分:2)

我想那是

<script>
    $(document).ready(() => {
        function trainer_changed() {
            let trainer = $('#trainer');
            jQuery.ajax({
                url: '{{ path('trainer_select_ajax') }}',
                type: 'GET',
                data: {
                    //this is the id of the selected option, not the search term
                    search: trainer.val() 
                },
                success: function (html) {
                    let trainer = $('#trainer');
                    let newchoices = $(html).find('#trainer');
                    trainer.replaceWith(
                        newchoices  // works
                    );
                }
            });
        }
        // when a new option is selected, not what I want
        $('#trainer').change(trainer_changed);
        $('#trainer').on('input', trainer_changed);  // no effect
    });
</script>

答案 1 :(得分:1)

以下是用于BigQuery标准SQL

   
#standardSQL
SELECT EmployeeName, ARRAY_AGG(STRUCT(EmployeeDetail1, EmployeeDetail2)) EmployeeDetails
FROM `project.dataset.Employee` t1
LEFT JOIN `project.dataset.Details` t2 
USING(Id)
GROUP BY EmployeeName

Yo可以使用[完全]虚设数据来测试,玩游戏,如下例所示

#standardSQL
WITH `project.dataset.Employee` AS (
  SELECT 1 AS id, 1 AS EmployeeName UNION ALL
  SELECT 2, 2 UNION ALL
  SELECT 3, 3 
), `project.dataset.Details` AS (
  SELECT 1 AS id, 11 AS EmployeeDetail1, 12 EmployeeDetail2 UNION ALL
  SELECT 1, 21, 22 UNION ALL
  SELECT 3, 211, 221 UNION ALL
  SELECT 3, 212, 222 UNION ALL
  SELECT 3, 31, NULL 
)
SELECT EmployeeName, ARRAY_AGG(STRUCT(EmployeeDetail1, EmployeeDetail2)) EmployeeDetails
FROM `project.dataset.Employee` t1
LEFT JOIN `project.dataset.Details` t2 
USING(Id)
GROUP BY EmployeeName   

有结果

enter image description here