我查询有困难,我有大量的订单,我从中提取月份和年份,需要在每个月和每年内汇总所有价格。我能提出解决方案的唯一方法是迭代并进行大量昂贵的比较,这似乎打败了关系数据库的目的。价格是在我的查询中计算的,因为折扣通常应用于小计。我已将所有必要的表连接起来并进行匹配,但由于价格计算,我无法使用内置的SUM()函数。提前谢谢!
编辑:这是代码,我今天表现得非常分散。这绝对是我的,谢谢你和我在一起:`SELECT dbo.SalesOrder.SalesOrderNo
,dbo.SalesOrder.CustomerID
,(dbo.InvoiceDetails.ExtendedPrice * (1 - dbo.Invoice.DiscountPct / 100)) AS [Price]
,dbo.Customers.NAME AS [Customer Name]
,dbo.Customers.SalesRepID
,dbo.Employees.FirstName + dbo.Employees.LastName AS [SalesRepName]
,dbo.Estimate.DateCreated AS [Date Quoted]
,dbo.SalesOrder.OrderDate AS [Date Ordered]
,dbo.Invoice.BillingDate AS [Date Invoiced]
,MONTH(dbo.Estimate.DateCreated) AS [Month Quoted]
,MONTH(dbo.SalesOrder.OrderDate) AS [Month Ordered]
,MONTH(dbo.Invoice.BillingDate) AS [Month Invoiced]
,YEAR(dbo.Estimate.DateCreated) AS [Year Quoted]
,YEAR(dbo.SalesOrder.OrderDate) AS [Year Ordered]
,YEAR(dbo.Invoice.BillingDate) AS [Year Invoiced]
FROM SalesOrder
INNER JOIN Invoice ON Invoice.SalesOrderID = SalesOrder.SalesOrderID
INNER JOIN InvoiceDetails ON InvoiceDetails.InvoiceID = Invoice.InvoiceID
INNER JOIN Customers ON Customers.CustomerID = SalesOrder.CustomerID
INNER JOIN SalesReps ON Customers.SalesRepID = SalesReps.SalesRepID
INNER JOIN Employees ON Employees.EmployeeID = SalesReps.EmployeeID
INNER JOIN Estimate ON Estimate.EstimateID = SalesOrder.EstimateID
WHERE dbo.Invoice.Approved = '1'
AND (YEAR(dbo.Invoice.BillingDate) >= YEAR(GETDATE()) - 1)
AND (YEAR(dbo.Estimate.DateCreated) >= YEAR(GETDATE()) - 1)
AND (YEAR(dbo.SalesOrder.OrderDate) >= YEAR(GETDATE()) - 1)`
答案 0 :(得分:0)
没有任何其他信息,你有什么理由可以做这样的事情吗?
SELECT YEAR(ORDER_DATE) AS [Year], MONTH(ORDER_DATE) AS [Month], SUM(AMOUNT) AS [Total]
FROM Orders
GROUP BY YEAR(ORDER_DATE), MONTH(ORDER_DATE)
ORDER BY YEAR(ORDER_DATE), MONTH(ORDER_DATE)
假设表名是"订单"它有列" ORDER_DATE"作为日期时间," AMOUNT"作为数值。
根据显示价格计算的更新查询,一个简单的方法就是从现有查询中创建一个CTE(公用表表达式),然后选择SELECT / GROUP BY ......这样的东西:
WITH sales AS (
SELECT dbo.SalesOrder.SalesOrderNo
,dbo.SalesOrder.CustomerID
,(dbo.InvoiceDetails.ExtendedPrice * (1 - dbo.Invoice.DiscountPct / 100)) AS [Price]
,dbo.Customers.NAME AS [Customer Name]
,dbo.Customers.SalesRepID
,dbo.Employees.FirstName + dbo.Employees.LastName AS [SalesRepName]
,dbo.Estimate.DateCreated AS [Date Quoted]
,dbo.SalesOrder.OrderDate AS [Date Ordered]
,dbo.Invoice.BillingDate AS [Date Invoiced]
,MONTH(dbo.Estimate.DateCreated) AS [Month Quoted]
,MONTH(dbo.SalesOrder.OrderDate) AS [Month Ordered]
,MONTH(dbo.Invoice.BillingDate) AS [Month Invoiced]
,YEAR(dbo.Estimate.DateCreated) AS [Year Quoted]
,YEAR(dbo.SalesOrder.OrderDate) AS [Year Ordered]
,YEAR(dbo.Invoice.BillingDate) AS [Year Invoiced]
FROM SalesOrder
INNER JOIN Invoice ON Invoice.SalesOrderID = SalesOrder.SalesOrderID
INNER JOIN InvoiceDetails ON InvoiceDetails.InvoiceID = Invoice.InvoiceID
INNER JOIN Customers ON Customers.CustomerID = SalesOrder.CustomerID
INNER JOIN SalesReps ON Customers.SalesRepID = SalesReps.SalesRepID
INNER JOIN Employees ON Employees.EmployeeID = SalesReps.EmployeeID
INNER JOIN Estimate ON Estimate.EstimateID = SalesOrder.EstimateID
WHERE dbo.Invoice.Approved = '1'
AND (YEAR(dbo.Invoice.BillingDate) >= YEAR(GETDATE()) - 1)
AND (YEAR(dbo.Estimate.DateCreated) >= YEAR(GETDATE()) - 1)
AND (YEAR(dbo.SalesOrder.OrderDate) >= YEAR(GETDATE()) - 1)
)
SELECT [Month Ordered], [Year Ordered], SUM([Price]) AS TotalOrderedPrice
FROM Sales
GROUP BY [Month Ordered], [Year Ordered]
ORDER BY [Month Ordered], [Year Ordered]
这会创建一个名为" sales"使用原始查询verbatum,然后使用底部SELECT中的CTE对该查询的结果进行分组和排序(包括求计算价格)。这是一种简单的语法糖'这使得表达这种东西比你不得不重新设计查询以计算出值或子选择或者你有什么,这是一个很有用的技巧。