sql - BigQuery - 按多列排名
问题描述
如果我写错了或者说错了,请原谅我。
我想要一个按日期和 fullvisitorid 对访问者 order_id 进行排名的排名列,如下所示。这可能吗?如果是这样,代码行会是什么样子?
日期 | 全访问者 ID | order_id | 秩 |
---|---|---|---|
20201127 | 2148490690588403919 | MUK105556400 | 1 |
20201128 | 2148490690588403919 | MUK105556401 | 2 |
20201129 | 2148490690588403919 | MUK105606555 | 3 |
20201127 | 6444290669077908463 | MUK105521707 | 1 |
20201128 | 6444290669077908463 | MUK105633922 | 2 |
20201129 | 6444290669077908463 | MUK105630521 | 3 |
SELECT
DISTINCT date,
fullVisitorId,
order_id,
FROM (
SELECT
date,
fullVisitorId,
hits.TRANSACTION.transactionId AS order_id
FROM
`table_name`,
UNNEST(hits) AS hits
WHERE
date BETWEEN '20201127'
AND '20201130'
AND hits.TRANSACTION.transactionId IS NOT NULL
AND geoNetwork. country = 'United Kingdom'
AND fullVisitorId IN ('2148490690588403919',
'6444290669077908463'))
ORDER BY
2 ASC,
1 ASC
解决方案
请参阅下面的示例
#standardSQL
with `project.dataset.table` as (
select '20201127' date, 2148490690588403919 fullvisitorID,'MUK105556400' order_id union all
select '20201128', 2148490690588403919,'MUK105556401' union all
select '20201129', 2148490690588403919,'MUK105606555' union all
select '20201127', 6444290669077908463,'MUK105521707' union all
select '20201128', 6444290669077908463,'MUK105633922' union all
select '20201129', 6444290669077908463,'MUK105630521'
)
select *,
rank() over(partition by fullvisitorID order by date, order_id) rank
from `project.dataset.table`
带输出
推荐阅读
- php - 我正在尝试使用 PHP 语言设置新规则
- sql - 在特定空间实例之间提取字符串 - SQL Server
- python - 编写查找数字数组中元素索引的函数
- angular - 如何创建自定义响应式表单元素?
- reactjs - 使用 Bulma.css 在 React.js 中每 3 个列元素添加新的列容器
- xamarin.forms - 如何将详细信息页面变成母版页?
- python-3.x - Python - 无法将发布请求发送到登录表单
- common-lisp - Read file by line and wait for keyboard input to read next line
- ninja - 确定哪些目标在忍者中一次全部失败
- php - ifconfig 并在将 Magento 从 2.1.2 更新到 2.2.7 后无法工作