gpt4 book ai didi

sql - oracle SQL 从这个 date_id 选择到这个 date_id

转载 作者:搜寻专家 更新时间:2023-10-30 22:59:54 24 4
gpt4 key购买 nike

我必须显示 2011-2014 年表格中的数据。该表具有另一个名为 time 的表的时间 ID,该表存储实际年份。我有声明从这个日期 ID 到日期 ID 选择数据。

SELECT sum(no_of_placements)
FROM fact_accounts
WHERE
(SELECT account_dim.account_id FROM account_dim WHERE account_dim.account_name = 'DCompany1')
= fact_accounts.fk2
AND (
(SELECT time_dim.time_id FROM time_dim WHERE time_dim.year = 2011)
= fact_accounts.fk1_time_id
OR (SELECT time_dim.time_id FROM time_dim WHERE time_dim.year = 2012)
= fact_accounts.fk1_time_id
OR (SELECT time_dim.time_id FROM time_dim WHERE time_dim.year = 2013)
= fact_accounts.fk1_time_id
OR (SELECT time_dim.time_id FROM time_dim WHERE time_dim.year = 2014)
= fact_accounts.fk1_time_id
)
GROUP BY fact_accounts.no_of_placements

如何在不设置条件以匹配每年的 time_id 的情况下实现相同的目标?如果年份范围更大,这似乎并不有效。

最佳答案

SELECT sum(no_of_placements)
FROM fact_accounts, account_dim, time_dim
WHERE account_dim.account_id = fact_accounts.fk2
AND account_dim.account_name = 'DCompany1'
AND fact_accounts.FK1_TIME_ID = time_dim.time_id
AND time_dim.year BETWEEN 2011 AND 2014

GROUP BY fact_accounts.no_of_placements

SELECT sum(no_of_placements)
FROM fact_accounts,
WHERE
(select account_dim.account_id FROM account_dim WHERE account_dim.account_name = 'DCompany1') = fact_accounts.fk2

AND fact_accounts.FK1_TIME_ID IN
(select time_dim.time_id from time_dim WHERE
time_dim.year BETWEEN 2011 AND 2014)

GROUP BY fact_accounts.no_of_placements

但为什么你们都按 no_of_placements 求和和分组?这对我来说意义不大。

关于sql - oracle SQL 从这个 date_id 选择到这个 date_id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35217265/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com