我想从学生收藏中获取唯一记录[studentName] 根据加入日期
示例:学生[TableName]
studentName joiningDate
Rosy 2018-06-25
Meera 2018-07-20
Rosy 2018-06-20
我已经在下面的查询中尝试过此方法,但是没有用。
通过加入日期选择与学生订单不同的(学生姓名)
我想要这样的结果,
studentName joiningDate
Rosy 2018-06-25
Meera 2018-07-20
答案 0 :(得分:2)
以下内容适用于BigQuery标准SQL,适用于该表中任意数量的其他字段,无需更改查询
#standardSQL
SELECT AS VALUE ARRAY_AGG(t ORDER BY joiningDate DESC LIMIT 1)[OFFSET(0)]
FROM `project.dataset.student` t
GROUP BY studentName
您可以使用问题中的虚拟数据进行上述测试和操作:
#standardSQL
WITH `project.dataset.student` AS (
SELECT 'Rosy'studentName, '2018-06-25' joiningDate UNION ALL
SELECT 'Meera', '2018-07-20' UNION ALL
SELECT 'Rosy', '2018-06-20'
)
SELECT AS VALUE ARRAY_AGG(t ORDER BY joiningDate LIMIT 1)[OFFSET(0)]
FROM `project.dataset.student` t
GROUP BY studentName
有结果
Row studentName joiningDate
1 Rosy 2018-06-25
2 Meera 2018-07-20
答案 1 :(得分:0)
应改为尝试GROUP BY
,如下所示:
SELECT studentName, MIN(joiningDate)
FROM student
GROUP BY studentName
答案 2 :(得分:0)
您可以将Group By
与Max()
聚合功能一起使用(以获取最近的加入日期)。请尝试以下查询:
SELECT studentName,
MAX(joiningDate) AS joiningDate
FROM student
GROUP BY studentName
ORDER BY joiningDate ASC