我正在尝试创建一个查询,该查询将显示每笔销售的详细信息以及另一列,该列将显示订单创建当天发生的每日总销售额(创建日期)
我有两个查询可以提供所需的信息,但我无法合并它们。我相信LEFT JOIN SELECT语句是实现这一目标所必需的。
### Query that displays order details ###
SELECT DISTINCT
Tracking.ID,
Tracking.CreationDate,
Tracking.CurrentAssignedDriverID,
Tracking.TotalCost,
Tracking.CustomerID,
Tracking.dFrom,
Tracking.dTo,
(SELECT Company FROM Customers WHERE Customers.ID = CustomerID)
FROM Tracking
### Query that yields daily totals ###
SELECT
CAST(convert(char(10), Tracking.CreationDate, 101) AS smalldatetime),
Tracking.TotalCost
FROM
Tracking
GROUP BY
CAST(convert(char(10), Tracking.CreationDate, 101) AS smalldatetime),
Tracking.TotalCost
答案 0 :(得分:1)
您可以在一个查询中获取所有信息,如下所示:
SELECT Tracking.ID,
Tracking.CreationDate,
Tracking.CurrentAssignedDriverID,
Tracking.TotalCost,
Tracking.CustomerID,
Tracking.dFrom,
Tracking.dTo,
Customers.Company,
SUM(Tracking.TotalCost) OVER(PARTITION BY CAST(convert(char(10), Tracking.CreationDate, 101) AS smalldatetime))
AS TotalSales
FROM Tracking INNER JOIN Customers
ON Customers.ID = Tracking.CustomerID