SQL 使用跨表分区
SQL using partition across tables
我有以下两个 table:
CREATE TABLE `T1` (
`user`,
`str_to_match`,
`x` ,
`y`
);
和
CREATE TABLE `T2` (
`user`,
`ID`,
`str_to_match`,
`a`,
`b`
);
以下值被插入到两个 table 中:
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'
我已经为 cols 用户和 str_to_match 在 table T1 上使用了 over partition,并且能够进行聚合,但是无法与 table 一起加入T2 以获得完整的所需输出。 只有在同一用户的 table T1 和 T2 中 str_to_match col 匹配时才必须执行聚合。
这是我当前的查询,适用于 table 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
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
这对你有用吗? (这是 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.
来完成
不完全清楚,但我认为使用常规聚合的 LEFT JOIN 是可行的方法(这里没有适用的 window 函数)
#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
此解决方案将为您提供上述预期的确切结果:
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
我有以下两个 table:
CREATE TABLE `T1` (
`user`,
`str_to_match`,
`x` ,
`y`
);
和
CREATE TABLE `T2` (
`user`,
`ID`,
`str_to_match`,
`a`,
`b`
);
以下值被插入到两个 table 中:
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'
我已经为 cols 用户和 str_to_match 在 table T1 上使用了 over partition,并且能够进行聚合,但是无法与 table 一起加入T2 以获得完整的所需输出。 只有在同一用户的 table T1 和 T2 中 str_to_match col 匹配时才必须执行聚合。
这是我当前的查询,适用于 table 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
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
这对你有用吗? (这是 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.
来完成不完全清楚,但我认为使用常规聚合的 LEFT JOIN 是可行的方法(这里没有适用的 window 函数)
#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
此解决方案将为您提供上述预期的确切结果:
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