我试图按月计算上个月没有的唯一身份用户。因此,如果用户有1月份的记录,然后是2月份的另一份记录,那么我只计算该用户的1月份。
user_id time
a1 1/2/17
a1 2/10/17
a2 2/18/17
a4 2/5/17
a5 3/25/17
我的结果应该是这样的
Month User Count
January 1
February 2
March 1
答案 0 :(得分:1)
我对BigQuery并不熟悉,但这是我如何使用TSQL解决问题。我想你可以在BigQuery中使用类似的逻辑。
1)。首先按user_id排序数据,然后按时间排序。在TSQL中,您可以使用以下内容完成此操作并将其存储在公用表表达式中,您将在此后的步骤中进行查询。
;WITH cte AS
(
select ROW_NUMBER() OVER (PARTITION BY [user_id] ORDER BY [time]) AS rn,*
from dbo.employees
)
2)。下一个查询仅针对rn = 1(特定用户的第一次出现)和按月分组的行。
select DATENAME(month, [time]) AS [Month], count(*) AS user_count
from cte
where rn = 1
group by DATENAME(month, [time])
这假设2017年是您唯一需要处理的年份。如果您处理的时间超过一年,您可能希望步骤#2看起来像这样:
select year([time]) as [year], DATENAME(month, [time]) AS [month],
count(*) AS user_count
from cte
where rn = 1
group by year([time]), DATENAME(month, [time])
答案 1 :(得分:0)
首先按用户ID和月份汇总。然后使用lag()
查看用户是否在上个月出现:
with du as (
select date_trunc(time, month) as yyyymm, user_id
from t
group by date_trunc(time, month)
)
select yyyymm, count(*)
from (select du.*,
lag(yyyymm) over (partition by user_id order by yyyymm) as prev_yyyymm
from du
) du
where prev_yyyymm is not null or
prev_yyyymm < date_add(yyyymm, interval 1 month)
group by yyyymm;
注意:这使用date
函数,但timestamp
存在类似的函数。
答案 2 :(得分:0)
我理解问题的方法是 - 仅在相同用户在上个月展示时,将用户排除在给定月份内。但是如果同一个用户在给出之前的几个月内出现,而不是之前的用户 - 用户应该被计算在内。
如果这是正确的 - 请尝试以下BigQuery Standard SQL
#standardSQL
SELECT Year, Month, COUNT(DISTINCT user_id) AS User_Count
FROM (
SELECT *,
DATE_DIFF(time, LAG(time) OVER(PARTITION BY user_id ORDER BY time), MONTH) AS flag
FROM (
SELECT
user_id,
DATE_TRUNC(PARSE_DATE('%x', time), MONTH) AS time,
EXTRACT(YEAR FROM PARSE_DATE('%x', time)) AS Year,
FORMAT_DATE('%B', PARSE_DATE('%x', time)) AS Month
FROM yourTable
GROUP BY 1, 2, 3, 4
)
)
WHERE IFNULL(flag, 0) <> 1
GROUP BY Year, Month, time
ORDER BY time
您可以使用以下示例使用您的问题
中的虚拟数据来测试/播放上面的内容#standardSQL
WITH yourTable AS (
SELECT 'a1' AS user_id, '1/2/17' AS time UNION ALL
SELECT 'a1', '2/10/17' UNION ALL
SELECT 'a2', '2/18/17' UNION ALL
SELECT 'a4', '2/5/17' UNION ALL
SELECT 'a5', '3/25/17'
)
SELECT Year, Month, COUNT(DISTINCT user_id) AS User_Count
FROM (
SELECT *,
DATE_DIFF(time, LAG(time) OVER(PARTITION BY user_id ORDER BY time), MONTH) AS flag
FROM (
SELECT
user_id,
DATE_TRUNC(PARSE_DATE('%x', time), MONTH) AS time,
EXTRACT(YEAR FROM PARSE_DATE('%x', time)) AS Year,
FORMAT_DATE('%B', PARSE_DATE('%x', time)) AS Month
FROM yourTable
GROUP BY 1, 2, 3, 4
)
)
WHERE IFNULL(flag, 0) <> 1
GROUP BY Year, Month, time
ORDER BY time
输出
Year Month User_Count
2017 January 1
2017 February 2
2017 March 1
答案 3 :(得分:0)
尝试此查询:
{
"items": [
{
"id": "4",
"order_id": "000000145",
"creditmemo": {
"items": [
{
"sku": "SWETA0582",
"product_id": "2640",
"item_id": "211",
"qty": "1.0000",
"reason": "Product didn’t meet your expectation."
},
{
"sku": "SWETA0583",
"product_id": "2642",
"item_id": "212",
"qty": "2.0000",
"reason": "Product didn’t meet your expectation."
}
],
"do_offline": 1,
"comment_text": "",
"shipping_amount": "49.0000",
"adjustment_positive": "",
"adjustment_negative": "49.0000"
},
"additional_remark": "",
"type": "1",
"status": "3",
"account": {
"ifsc": "PYTM0123456",
"account_no": "918981961927",
"name": "Shruti Dhandhania"
},
"refund_status": "0",
"totals_info": "",
"refund_id": "0",
"created_at": "2018-10-25 08:58:37",
"update_at": "2018-10-25 08:58:37",
"tracking_number": "",
"method": "cashondelivery",
"comment": null
},
{
"id": "5",
"order_id": "000000146",
"creditmemo": {
"items": [
{
"sku": "SWETA0584",
"product_id": "2644",
"item_id": "215",
"qty": "3.0000",
"reason": "Product didn’t meet your expectation."
},
{
"sku": "SWETA0585",
"product_id": "2646",
"item_id": "216",
"qty": "2.0000",
"reason": "Product didn’t meet your expectation."
}
],
"do_offline": 1,
"comment_text": "",
"shipping_amount": "49.0000",
"adjustment_positive": "",
"adjustment_negative": "49.0000"
},
"additional_remark": "",
"type": "1",
"status": "3",
"account": {
"ifsc": "PYTM0123456",
"account_no": "918981961927",
"name": "Shruti Dhandhania"
},
"refund_status": "0",
"totals_info": "",
"refund_id": "0",
"created_at": "2018-10-25 08:58:37",
"update_at": "2018-10-25 08:58:37",
"tracking_number": "",
"method": "cashondelivery",
"comment": null
}
]
}