我正在尝试查找在date1和date2之间下订单的客户列表,并获取他们的客户名称,联系方式,电子邮件,所有时间的总花费(不仅限于date 1和date 2之间的情况以及所有订单的数量)时间。
我遇到的问题是,它仅对日期1和日期2之间的订单进行求和计数。我希望这两列都没有date1和date2。
declare @date1 as datetime, @date2 as dateTime
set @date1 = '12/1/2018'
set @date2 = '1/1/2019'
select
cl.BusinessName,
MAX(fn.FirstName) as Contact,
MAX(em.Email) as Email,
SUM(OrderTotal + Gratuity + Tax) as Total,
count(Distinct EventDateTime) as numOrders
from Orders ord
Join Clients cl on ord.ClientID = cl.RID
Join Staffs fn on ord.StaffID = fn.RID
Join Staffs em on ord.StaffID = em.RID
where NOT OrderStatus = 6 and NOT cl.BusinessName = 'Wildcard' and isdraft = 0 and entydate between @date1 and @Date2
Group by cl.BusinessName
答案 0 :(得分:0)
您需要在subquery
中查找总数和计数值,并将其添加到具有CliendId链接的脚本中。据我了解,您需要这样的东西:
DECLARE @date1 AS DATETIME
, @date2 AS DATETIME
SET @date1 = '12/1/2018'
SET @date2 = '1/1/2019'
SELECT
cl.BusinessName
,MAX(fn.FirstName) as Contact
,MAX(em.Email) as Email
--SUM(OrderTotal + Gratuity + Tax) as Total,
--COUNT(Distinct EventDateTime) as numOrders
,OT.numOrders
,OT.Total
FROM Orders ord
JOIN Clients cl on ord.ClientID = cl.RID
JOIN Staffs fn on ord.StaffID = fn.RID
JOIN Staffs em on ord.StaffID = em.RID
JOIN (SELECT CliendId,
SUM(OrderTotal + Gratuity + Tax) as Total,
COUNT(Distinct EventDateTime) as numOrders
FROM Orders
WHERE NOT OrderStatus=6
GROUP BY CliendId) OT ON Ord.CliendId = OT.CliendId
WHERE NOT OrderStatus = 6
AND NOT cl.BusinessName = 'Wildcard'
AND isdraft = 0
AND entydate between @date1 and @Date2
GROUP BY cl.BusinessName
,OT.numOrders
,OT.Total