sql- 仅显示最近四个月
sql- show only the last four month
我的好参考是
How to filter my results so it shows the last four months of data - sql
到目前为止我有
SELECT ItemCode, SUM(QuantityOrdered) AS Total_Quantity
FROM OrderDetail
GROUP BY ItemCode
ORDER BY SUM(QuantityOrdered) DESC;
显示
ItemCode Total_Quantity
PL 1200
MA 975
153 200
PL 的订单数量为 2011 年 9 月 23 日 200 件 2014 年 2 月 3 日 200 件 2016 年 12 月 6 日 200 件 2017 年 1 月 21 日 200 件
如何将其过滤掉以仅显示今天月份的最后四个月结果?
我可以在查询设计或 SQL 代码上做些什么吗?
Any idea on Syntax error on line WHERE dbo_SO_SalesOrderHeader.OrderDate >= dateadd(month, -4, cast(getdate() AS DATE))
. Thank you so much!
SELECT dbo_SO_SalesOrderDetail.ItemCode, SUM(dbo_SO_SalesOrderDetail.QuantityOrdered) AS Total_Quantity, dbo_SO_SalesOrderHeader.OrderDate
FROM dbo_SO_SalesOrderDetail INNER
JOIN dbo_SO_SalesOrderHeader ON dbo_SO_SalesOrderDetail.SalesOrderNo = dbo_SO_SalesOrderHeader.SalesOrderNo
WHERE dbo_SO_SalesOrderHeader.OrderDate >= dateadd(month, -4, cast(getdate() AS DATE))
GROUP BY dbo_SO_SalesOrderDetail.ItemCode
ORDER BY SUM(dbo_SO_SalesOrderDetail.QuantityOrdered) DESC;
添加 where 子句并将 "DateField" 与当前日期进行比较。因此,如果您的 DateField 名为 OrderDate,请将 "DateField" 替换为 "OrderDate"
SELECT ItemCode, SUM(QuantityOrdered) AS Total_Quantity
FROM OrderDetail
where DateField >= dateadd(month, -4, cast(getdate() as DATE))
GROUP BY ItemCode
ORDER BY SUM(QuantityOrdered) DESC;
我猜您的日期字段实际上在 OrderHeader table 中,如果是这样,您将需要加入 OrderHeader table。下面是一个例子
SELECT ItemCode, SUM(QuantityOrdered) AS Total_Quantity
FROM OrderDetail
JOIN OrderHeader on OrderHeader.Orderid=OrderDetail.OrderID
WHERE OrderHeader.OrderDate >= dateadd(month, -4, cast(getdate() as DATE))
GROUP BY ItemCode
ORDER BY SUM(QuantityOrdered) DESC;
我的好参考是 How to filter my results so it shows the last four months of data - sql
到目前为止我有
SELECT ItemCode, SUM(QuantityOrdered) AS Total_Quantity
FROM OrderDetail
GROUP BY ItemCode
ORDER BY SUM(QuantityOrdered) DESC;
显示
ItemCode Total_Quantity
PL 1200
MA 975
153 200
PL 的订单数量为 2011 年 9 月 23 日 200 件 2014 年 2 月 3 日 200 件 2016 年 12 月 6 日 200 件 2017 年 1 月 21 日 200 件
如何将其过滤掉以仅显示今天月份的最后四个月结果?
我可以在查询设计或 SQL 代码上做些什么吗?
Any idea on Syntax error on line
WHERE dbo_SO_SalesOrderHeader.OrderDate >= dateadd(month, -4, cast(getdate() AS DATE))
. Thank you so much!
SELECT dbo_SO_SalesOrderDetail.ItemCode, SUM(dbo_SO_SalesOrderDetail.QuantityOrdered) AS Total_Quantity, dbo_SO_SalesOrderHeader.OrderDate
FROM dbo_SO_SalesOrderDetail INNER
JOIN dbo_SO_SalesOrderHeader ON dbo_SO_SalesOrderDetail.SalesOrderNo = dbo_SO_SalesOrderHeader.SalesOrderNo
WHERE dbo_SO_SalesOrderHeader.OrderDate >= dateadd(month, -4, cast(getdate() AS DATE))
GROUP BY dbo_SO_SalesOrderDetail.ItemCode
ORDER BY SUM(dbo_SO_SalesOrderDetail.QuantityOrdered) DESC;
添加 where 子句并将 "DateField" 与当前日期进行比较。因此,如果您的 DateField 名为 OrderDate,请将 "DateField" 替换为 "OrderDate"
SELECT ItemCode, SUM(QuantityOrdered) AS Total_Quantity
FROM OrderDetail
where DateField >= dateadd(month, -4, cast(getdate() as DATE))
GROUP BY ItemCode
ORDER BY SUM(QuantityOrdered) DESC;
我猜您的日期字段实际上在 OrderHeader table 中,如果是这样,您将需要加入 OrderHeader table。下面是一个例子
SELECT ItemCode, SUM(QuantityOrdered) AS Total_Quantity
FROM OrderDetail
JOIN OrderHeader on OrderHeader.Orderid=OrderDetail.OrderID
WHERE OrderHeader.OrderDate >= dateadd(month, -4, cast(getdate() as DATE))
GROUP BY ItemCode
ORDER BY SUM(QuantityOrdered) DESC;