【问题标题】:Importing data using temp tables in Power BI在 Power BI 中使用临时表导入数据
【发布时间】:2021-08-09 09:27:55
【问题描述】:

我想使用以下 SQL 查询将数据导入 Power BI。它涉及创建临时表并将该表用作主要数据源。如何在 Power BI 中执行此操作?从数据库加载数据时,我尝试在编辑器中使用此查询,但我不断收到这样的错误

我基本上使用了这个数据集https://www.kaggle.com/kyanyoga/sample-sales-data并将其加载到一个postgressql数据库中。

-- 1. Create temp table to house temporary results
DROP TABLE IF EXISTS product_quantity;
CREATE TEMP TABLE product_quantity
 (product_line varchar, this_month_quantity integer, last_month_quantity integer)

 --2. Quantity ordered for each Product line for current month is inserted into temporary table.
INSERT INTO product_quantity (product_line, this_month_quantity, last_month_quantity)
SELECT "productline", SUM("quantityordered"), 0
FROM test_schema.sales_data_sample
where "month_id" = 3 and "year_id" = 2003
GROUP BY "productline";

--3. Quantity ordered for each Product line for last month is inserted into temporary table.
INSERT INTO product_quantity (product_line, this_month_quantity, last_month_quantity)
SELECT "productline", 0, SUM("quantityordered")
FROM test_schema.sales_data_sample
where "month_id" = 2 and "year_id" = 2003
GROUP BY "productline";

--4. Retrieve required results.
select
    "product_line",
    sum("this_month_quantity") as "this_month_quantity",
    sum("last_month_quantity") as "last_month_quantity"
FROM product_quantity
group by "product_line"

【问题讨论】:

  • 表格真的必须是 TEMP 吗? (如果这是问题)。此外,您可以尝试根据消息禁用折叠
  • 最好。不可能吗?你知道我为什么会收到这个错误吗?
  • 我的猜测是查询太复杂而无法应用“折叠”。看看这篇关于禁用折叠的帖子,看看它是否有帮助:community.powerbi.com/t5/Desktop/Query-Folding-disable/td-p/…
  • 我实际上试过它没有成功:(
  • 你是说你设置了[EnableFolding=false],但你仍然得到同样的错误?接下来我建议你使用普通表而不是临时表,看看是否有帮助。

标签: sql postgresql powerbi


【解决方案1】:

这个查询是否运行没有错误?

我已将您的查询转换为一个大的内联查询。

select
    ST."product_line",
    sum(ST."this_month_quantity") as "this_month_quantity",
    sum(ST."last_month_quantity") as "last_month_quantity"
FROM
(
    SELECT "productline", 
    SUM("quantityordered") as this_month_quantity, 
    0 as last_month_quantity
    FROM test_schema.sales_data_sample
    where "month_id" = 3 and "year_id" = 2003
    GROUP BY "productline"
    UNION ALL
    SELECT "productline", 
    0, 
    SUM("quantityordered")
    FROM test_schema.sales_data_sample
    where "month_id" = 2 and "year_id" = 2003
    GROUP BY "productline"
) as ST
group by ST."product_line"

(请注意,我只是猜测了转换 - 我没有要测试的 postgresql)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-27
    • 2022-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多