【发布时间】:2020-11-24 21:18:50
【问题描述】:
我有以下查询,它为我提供了前一周的数据,如下所示。它返回包含以下列的数据:type、amount 和 total 使用内部子查询中使用的 week_number 列为前一周。
select type,
case
WHEN (type = 'PROC1' AND code = 'UIT') THEN 450
WHEN (type = 'PROC1' AND code = 'KJH') THEN 900
WHEN (type = 'PROC2' AND code = 'LOP') THEN 8840
WHEN (type = 'PROC2' AND code = 'AWE') THEN 1490
WHEN (type = 'PROC3' AND code = 'MNH') THEN 1600
WHEN (type = 'PROC3' AND code = 'LKP') THEN 1900
END as amount,
total
from xyz.orders pa
join
(select clientid as clientid, max(version) as version
from xyz.orders where consumerid IN (select distinct entity_id from abc.items
where week_number = extract(week from current_date) - 1
and item_type like '%Ionize - Data%' )
and createdfor ='BLOCK'
and holder='RELAY_FUTURES'
group by clientid) pb on
pa.clientid = pb.clientid and pa.version = pb.version;
以下是我目前使用上述查询返回的上周的输出:
type amount total
---------------------------
PROC1 450 1768
PROC1 900 123
PROC1 450 456
PROC2 8840 99897
PROC2 1490 2223
PROC2 8840 9876
PROC3 1900 23456
PROC3 1600 12498
PROC3 1600 28756
在我上面的查询中,我有如下所示的内部子查询,它返回前一周的数据,然后在外部查询中使用它的输出。
select distinct entity_id from abc.items
where week_number = extract(week from current_date) - 1
and item_type like '%Ionize - Data%'
现在我正在尝试找出一种方法来获取过去 6 周(不包括当前周)的数据,并且还按每周分组,所以我认为我们需要修改上面的内部查询,以便它可以给我过去 6 周的数据,然后在外部查询中按每周分组。基本上我想在过去 6 周内为每个type 获得amount、total,如下所示。不知何故,我还需要在最终输出中添加 week_number 列。
预期输出
week_number type amount total
--------------------------------------------
46 PROC1 450 1768
46 PROC1 900 123
46 PROC1 450 456
46 PROC2 8840 99897
46 PROC2 1490 2223
46 PROC2 8840 9876
46 PROC3 1900 23456
46 PROC3 1600 12498
46 PROC3 1600 28756
45 PROC1 450 1768
45 PROC1 900 123
45 PROC1 450 456
45 PROC2 8840 99897
45 PROC2 1490 2223
45 PROC2 8840 9876
45 PROC3 1900 23456
45 PROC3 1600 12498
45 PROC3 1600 28756
44 PROC1 450 1768
44 PROC1 900 123
44 PROC1 450 456
44 PROC2 8840 99897
44 PROC2 1490 2223
44 PROC2 8840 9876
44 PROC3 1900 23456
44 PROC3 1600 12498
44 PROC3 1600 28756
43 PROC1 450 1768
43 PROC1 900 123
43 PROC1 450 456
43 PROC2 8840 99897
43 PROC2 1490 2223
43 PROC2 8840 9876
43 PROC3 1900 23456
43 PROC3 1600 12498
43 PROC3 1600 28756
42 PROC1 450 1768
42 PROC1 900 123
42 PROC1 450 456
42 PROC2 8840 99897
42 PROC2 1490 2223
42 PROC2 8840 9876
42 PROC3 1900 23456
42 PROC3 1600 12498
42 PROC3 1600 28756
41 PROC1 450 1768
41 PROC1 900 123
41 PROC1 450 456
41 PROC2 8840 99897
41 PROC2 1490 2223
41 PROC2 8840 9876
41 PROC3 1900 23456
41 PROC3 1600 12498
41 PROC3 1600 28756
所以我通过修改内部子查询尝试使用以下查询,但它给了我invalid operation:subquery has too many columns 的错误。知道我在这里做错了什么吗?
select type,
case
WHEN (type = 'PROC1' AND code = 'UIT') THEN 450
WHEN (type = 'PROC1' AND code = 'KJH') THEN 900
WHEN (type = 'PROC2' AND code = 'LOP') THEN 8840
WHEN (type = 'PROC2' AND code = 'AWE') THEN 1490
WHEN (type = 'PROC3' AND code = 'MNH') THEN 1600
WHEN (type = 'PROC3' AND code = 'LKP') THEN 1900
END as amount,
total
from xyz.orders pa
join
(select clientid as clientid, max(version) as version
from xyz.orders where consumerid IN (select week_number, entity_id from abc.items
where week_number >= extract(week from current_date) - 6
and week_number <= extract(week from current_date) - 1
and item_type like '%Ionize - Data%'
order by week_number desc )
and createdfor ='BLOCK'
and holder='RELAY_FUTURES'
group by clientid) pb on
pa.clientid = pb.clientid and pa.version = pb.version;
我将我的内部查询修改为:
select week_number, entity_id from abc.items
where week_number >= extract(week from current_date) - 6
and week_number <= extract(week from current_date) - 1
and item_type like '%Ionize - Data%'
order by week_number desc
我可能完全错误地实现了所需的输出,因此我们将不胜感激。
【问题讨论】:
-
不幸的是不是这样,我通过更改内部子查询走了不同的路线,但我在这里收到错误。
-
在子查询中删除 order by。您不能在子查询中进行排序。
-
我尝试删除
order by但仍然出现同样的错误 -invalid operation:subquery has too many columns@SaiAbhiramInapala -
在您的原始查询中,当您使用 week_number >= extract(week from current_date) 时会发生什么 - 6. 它有效吗?
标签: sql amazon-web-services amazon-redshift