SQL:压缩重复的COUNTIF:

时间:2018-10-24 08:21:52

标签: sql google-bigquery

在此(BigQuery)查询中:

pywintypes.com_error: (-2147352567, 'Une exception s\u2019est produite.', (0, None, 'La base de données est déjà ouverte.', None, -1, -2146820421), None)

我有很多样板def drop_user(user): global dbpath with open("login_hash.txt", 'r+') as fp: data = fp.readlines() for lines in data: if user not in lines: fp.write('') fp.truncate() try: print("Access running...") access = win32com.client.Dispatch('Access.Application') access.OpenCurrentDatabase(dbpath) acDb = access.CurrentDb() access.Visible = False access.CurrentProject.Connection.Execute("DROP USER " + user + ";") kill_access() del access messagebox.showinfo("Success", user + " a bien été ajouté.") except (pywintypes.com_error): pattern = r'"(.*?)"' m = re.search(pattern, str(sys.exc_info()[1])) messagebox.showinfo("Erreur", m.group().strip('"').replace('\xa0', '')) kill_access() del access 语句。 (如果我想查看14天或28天,情况会更糟。)是否可以更紧凑地编写此查询?

2 个答案:

答案 0 :(得分:2)

受Mikhail Berlyant的回答here的启发,我生成了一个查询,该查询产生了您需要的查询。当然是标准SQL。

with device_event_count as (select * from
(select '2015-08-01' as user_first_touch_date, 1 as active_days) union all
(select '2015-08-01' as user_first_touch_date, 2 as active_days) union all
(select '2015-08-01' as user_first_touch_date, 3 as active_days) union all
(select '2015-08-02' as user_first_touch_date, 1 as active_days) union all
(select '2015-08-02' as user_first_touch_date, 2 as active_days) union all
(select '2015-08-03' as user_first_touch_date, 1 as active_days) union all
(select '2015-08-03' as user_first_touch_date, 2 as active_days) union all
(select '2015-08-01' as user_first_touch_date, 5 as active_days) union all
(select '2015-08-01' as user_first_touch_date, 7 as active_days) union all
(select '2015-08-01' as user_first_touch_date, 3 as active_days) union all
(select '2015-08-02' as user_first_touch_date, 5 as active_days) union all
(select '2015-08-02' as user_first_touch_date, 6 as active_days) union all
(select '2015-08-03' as user_first_touch_date, 7 as active_days) union all
(select '2015-08-03' as user_first_touch_date, 3 as active_days) union all
(select '2015-08-01' as user_first_touch_date, 1 as active_days) union all
(select '2015-08-01' as user_first_touch_date, 2 as active_days) union all
(select '2015-08-01' as user_first_touch_date, 3 as active_days) union all
(select '2015-08-02' as user_first_touch_date, 1 as active_days) union all
(select '2015-08-02' as user_first_touch_date, 2 as active_days) union all
(select '2015-08-03' as user_first_touch_date, 1 as active_days) union all
(select '2015-08-03' as user_first_touch_date, 21 as active_days) union all
(select '2015-08-01' as user_first_touch_date, 53 as active_days) union all
(select '2015-08-01' as user_first_touch_date, 72 as active_days) union all
(select '2015-08-01' as user_first_touch_date, 3 as active_days) union all
(select '2015-08-02' as user_first_touch_date, 11 as active_days) union all
(select '2015-08-02' as user_first_touch_date, 62 as active_days) union all
(select '2015-08-03' as user_first_touch_date, 73 as active_days) union all
(select '2015-08-03' as user_first_touch_date, 34 as active_days) union all
(select '2015-08-03' as user_first_touch_date, 4 as active_days)
)

select concat('选择user_first_touch_date,共COUNT(*)个,',    string_agg(concat('COUNTIF(active_days> =',safe_cast(active_days as string),')as active_',safe_cast(active_days as string),'d'),','),     (来自device_event_count组,按user_first_touch_date排序,按user_first_touch_date排序)) 来自(从device_event_count组的active_days中按active_days的顺序选择active_days)

这是它的返回内容:

select user_first_touch_date, COUNT(*) as total, COUNTIF(active_days >=1) as active_1d,COUNTIF(active_days >=2) as active_2d,COUNTIF(active_days >=3) as active_3d,COUNTIF(active_days >=4) as active_4d,COUNTIF(active_days >=5) as active_5d,COUNTIF(active_days >=6) as active_6d,COUNTIF(active_days >=7) as active_7d,COUNTIF(active_days >=11) as active_11d,COUNTIF(active_days >=21) as active_21d,COUNTIF(active_days >=34) as active_34d,COUNTIF(active_days >=53) as active_53d,COUNTIF(active_days >=62) as active_62d,COUNTIF(active_days >=72) as active_72d,COUNTIF(active_days >=73) as active_73 from `device_event_count` group by user_first_touch_date order by user_first_touch_date

结果 enter image description here

答案 1 :(得分:1)

我看到的唯一解决方法是“自动”创建查询-在SO上有很多这样的示例-至少我知道我提供了这样的答案-您可以进行快速搜索。大概是:
选项1-半手动-具有实用程序查询,该查询生成所需查询的文本,其中包含根据需要的“多余”行,然后将其输出并作为查询运行;
 选项2-使用您选择的客户端,一次完成所有操作