为了使用PowerBI分析特定主题(我无法使用其他程序),我提出了一个观点。在我的情况下,数据库根据以下ERD: The ERD
我创建了以下视图:
CREATE VIEW sales_based_foodstyless AS
(
SELECT food_style,
total_quantities=Sum(quantity),
average_dish_price=Avg(d.price),
total _revenue=sum(quantity*price),
orders_amount=count(DISTINCT o.orderid),
total_vendors_operating=count( v.vendorid)
FROM vendors AS v
JOIN food_style AS fs
ON fs.vendorid=v.vendorid
JOIN dishes AS d
ON v.vendorid=d.vendorid
JOIN dish_quantities AS dq
ON d.vendorid=dq.vendorid
JOIN orders AS o
ON dq.orderid=o.orderid
GROUP BY food_style
)
我希望在PowerBI中按日期过滤数据,该数据将在视图的select语句中作为联接表的Order_Date找到。但是我无法做到这一点。 请帮助
答案 0 :(得分:0)
您可以添加一个过滤器作为示例
CREATE VIEW sales_based_foodstyless AS
(
SELECT
food_style,
total_quantities=Sum(quantity),
average_dish_price=Avg(d.price),
total _revenue=sum(quantity*price),
orders_amount=count(DISTINCT o.orderid),
total_vendors_operating=count( v.vendorid)
FROM vendors AS v
JOIN food_style AS fs ON fs.vendorid=v.vendorid
JOIN dishes AS d ON v.vendorid=d.vendorid
JOIN dish_quantities AS dq ON d.vendorid=dq.vendorid
JOIN orders AS o ON dq.orderid=o.orderid
WHERE YEAR(order_date) = 2018
GROUP BY food_style
)
但是,如果要动态执行此操作,则必须使用存储过程或参数化视图。
答案 1 :(得分:0)
您可以使用表值函数执行此操作:
CREATE FUNCTION fSelectSalesBasedFoodstyles (
@StartDate DATE
, @EndDate DATE
)
RETURNS TABLE
AS
RETURN
SELECT food_style,
total_quantities=Sum(quantity),
average_dish_price=Avg(d.price),
total _revenue=sum(quantity*price),
orders_amount=count(DISTINCT o.orderid),
total_vendors_operating=count( v.vendorid) ,
order_date
FROM vendors AS v
JOIN food_style AS fs
ON fs.vendorid=v.vendorid
JOIN dishes AS d
ON v.vendorid=d.vendorid
JOIN dish_quantities AS dq
ON d.vendorid=dq.vendorid
JOIN orders AS o
ON dq.orderid=o.orderid
-- CAST only necessary if OrderDate is a DATETIME:
WHERE CAST(o.order_date AS DATE) >= @StartDate
AND CAST(o.order_date AS DATE) <= @EndDate
GROUP BY food_style
GO