SQL使用表

时间:2017-01-31 10:58:48

标签: sql google-bigquery window-functions

我有以下两个表:

CREATE TABLE `T1` (
    `user`,
    `str_to_match`,
    `x` ,
    `y` 
);

CREATE TABLE `T2` (
    `user`,
    `ID`,
    `str_to_match`,
    `a`,
    `b` 
);

将以下值插入两个表中:

INSERT INTO T1 VALUES ('U1','123', 23, 'YVAL');
INSERT INTO T1 VALUES ('U2','123', 21, 'YVAL1');
INSERT INTO T1 VALUES ('U2','121', 27, 'YVAL2');
INSERT INTO T1 VALUES ('U1','123', 28, 'YVAL3');
INSERT INTO T1 VALUES ('U1','456', 30, 'YVAL4');

INSERT INTO T2 VALUES ('U1', 1, '123', 'AVAL', 'BVAL');
INSERT INTO T2 VALUES ('U1', 2, '123', 'AVAL1', 'BVAL1');
INSERT INTO T2 VALUES ('U2', 3, '123', 'AVAL2', 'BVAL2');
INSERT INTO T2 VALUES ('U2', 4, '121', 'AVAL3', 'BVAL3');

我正在尝试以下输出

T1.user, T1.str_to_match, SUM(T1.x), COUNT(T1.x), T2.ID, T2.a, T2.b
U1, '123', 51, 2, 1, 'AVAL', 'BVAL'
U1, '123', 51, 2, 2, 'AVAL1', 'BVAL1'
U2, '123', 21, 1, 3, 'AVAL2', 'BVAL2'
U2, '121', 27, 1, 4, 'AVAL3', 'BVAL3'

我已经在表T1上使用了分区用于cols user和str_to_match并且能够获得聚合,但是无法将其与表T2连接以获得完整的所需输出。必须执行聚合仅当表T1和T2中的str_to_match col与同一用户匹配时才会匹配。

这是我当前的查询,它适用于表T1

SELECT SUM(x) OVER (PARTITION BY user, str_to_match), 
COUNT(x)  OVER (PARTITION BY user, str_to_match), 
str_to_match, 
user from T1 

4 个答案:

答案 0 :(得分:1)

Below query will give you the expected output

select distinct t.str_to_match,t.[user],total,cnt
,t2.id,t2.a,t2.b 
from (SELECT SUM(x) OVER (PARTITION BY [user], str_to_match) total, 
COUNT(x)  OVER (PARTITION BY [user], str_to_match) cnt, 
str_to_match, 
[user] from @T1 ) as t
join @T2 t2 on t.str_to_match = t2.str_to_match and t.[user] = t2.[user]
order by t.[user],id

答案 1 :(得分:1)

这对你有用吗? (这是BigQuery中的标准SQL)

with T1 as(
select 'U1' as user, '123' as str_to_match, 23 as x, 'YVAL' as y union all
select 'U2' as user, '123' as str_to_match, 21 as x, 'YVAL1' as y union all
select 'U2' as user, '121' as str_to_match, 27 as x, 'YVAL2' as y union all
select 'U1' as user, '123' as str_to_match, 28 as x, 'YVAL3' as y union all
select 'U1' as user, '456' as str_to_match, 30 as x, 'YVAL4' as y
),
T2 as(
select 'U1' as user, 1 as ID, '123' as str_to_match, 'AVAL' as a, 'BVAL' as b union all
select 'U1' as user, 2 as ID, '123' as str_to_match, 'AVAL1' as a, 'BVAL1' as b union all
select 'U2' as user, 3 as ID, '123' as str_to_match, 'AVAL2' as a, 'BVAL2' as b union all
select 'U2' as user, 4 as ID, '121' as str_to_match, 'AVAL3' as a, 'BVAL3' as b
)


select 
T1.user user, T1.str_to_match str_to_match, sum(T1.x) sum_x, count(T1.x) count_x, T2.ID ID, T2.a a, T2.b b
from T1
join T2
on T1.user = T2.user and T1.str_to_match = T2.str_to_match
group by 
user, str_to_match, ID, a, b
order by user, id

我不确定我是否正确理解了您的问题,但是对于我在您想要的输出中可以看到的内容,您希望首先区分用户ID值,然后运行求和和计数操作,这可以通过首次加入来完成 T1 T2

答案 2 :(得分:1)

不完全清楚,但我认为LEFT JOIN与常规聚合是可行的方法(此处不适用窗口函数)

#standardSQL
WITH T1 AS (
  SELECT 'U1' AS user, '123' AS str_to_match, 23 AS x,  'YVAL' AS y UNION ALL
  SELECT 'U2' AS user, '123' AS str_to_match, 21 AS x, 'YVAL1' AS y UNION ALL
  SELECT 'U2' AS user, '121' AS str_to_match, 27 AS x, 'YVAL2' AS y UNION ALL
  SELECT 'U1' AS user, '123' AS str_to_match, 28 AS x, 'YVAL3' AS y UNION ALL
  SELECT 'U1' AS user, '456' AS str_to_match, 30 AS x, 'YVAL4' AS y
),
T2 AS (
  SELECT 'U1' AS user, 1 AS ID, '123' AS str_to_match,  'AVAL' AS a,  'BVAL' AS b UNION ALL
  SELECT 'U1' AS user, 2 AS ID, '123' AS str_to_match, 'AVAL1' AS a, 'BVAL1' AS b UNION ALL
  SELECT 'U2' AS user, 3 AS ID, '123' AS str_to_match, 'AVAL2' AS a, 'BVAL2' AS b UNION ALL
  SELECT 'U2' AS user, 4 AS ID, '121' AS str_to_match, 'AVAL3' AS a, 'BVAL3' AS b
)
SELECT 
  T2.user AS user, 
  T2.str_to_match AS str_to_match, 
  T2.ID AS ID, 
  T2.a AS a, 
  T2.b AS b,
  SUM(T1.x) AS sum_x, 
  COUNT(T1.x) AS count_x
FROM T2 LEFT JOIN T1 
ON T1.user = T2.user AND T1.str_to_match = T2.str_to_match
GROUP BY user, str_to_match, ID, a, b

答案 3 :(得分:1)

此解决方案将为您提供上述期望的确切结果:

select distinct abc.[User], abc.str_to_match, abc.sumval,
abc.countval,T2.ID, T2.a, T2.b
from
(select T1.[user],T1.str_to_match, SUM(T1.x) as sumval, COUNT(T1.x) as countval from T1 
group by T1.[user],T1.str_to_match) abc
inner join t2 on t2.[user] = abc.[user] and t2.str_to_match = abc.str_to_match;

请在下面找到查询和输出的附加屏幕截图链接:

query

Output