有人能告诉我为什么当我添加order_by()
后,输出的查询会从INNER JOIN
变成LEFT OUTER JOIN
吗?
是否有任何方法可以保留INNER JOIN
?
data = models.RetailSalesFact.objects.values('customer_key__customer_state',
'date_key__calendar_month_name')
data = data.filter(date_key__calendar_year=year)
data = data.annotate(sales=Sum('sales_quantity'))
data = data.order_by('date_key__calendar_month_name')
Before:
SELECT Customer_Dimension.Customer_State, Date_Dimension.Calendar_Month_Name,
SUM(Retail_Sales_Fact.Sales_Quantity) AS sales
FROM Retail_Sales_Fact
INNER JOIN Customer_Dimension
ON (Retail_Sales_Fact.Customer_Key = Customer_Dimension.Customer_Key)
INNER JOIN Date_Dimension
ON (Retail_Sales_Fact.Date_Key = Date_Dimension.Date_Key)
WHERE Date_Dimension.Calendar_Year = ?
GROUP BY Customer_Dimension.Customer_State,
Date_Dimension.Calendar_Month_Name
ORDER BY Date_Dimension.Calendar_Month_Name ASC
之后:
SELECT Customer_Dimension.Customer_State, Date_Dimension.Calendar_Month_Name,
SUM(Retail_Sales_Fact.Sales_Quantity) AS sales
FROM Retail_Sales_Fact
INNER JOIN Customer_Dimension
ON (Retail_Sales_Fact.Customer_Key = Customer_Dimension.Customer_Key)
LEFT OUTER JOIN Date_Dimension
ON (Retail_Sales_Fact.Date_Key = Date_Dimension.Date_Key)
WHERE Date_Dimension.Calendar_Year = ?
GROUP BY Customer_Dimension.Customer_State,
Date_Dimension.Calendar_Month_Name
ORDER BY Date_Dimension.Calendar_Month_Name ASC