我有一个查询,我根据自己的喜好手动更改日期。如何动态提取当前月份数据?
SELECT SUM(op.price * op.qty)
FROM dbo.Orders o
INNER JOIN dbo.OrdersProduct op ON op.orderID = o.orderID
WHERE o.ID IN ( xx, xx, xx, xx )
AND o.orderDate BETWEEN '3/1/2016' AND '3/10/16'
答案 0 :(得分:0)
这将是到目前为止的月份:
SELECT SUM(op.price * op.qty)
FROM dbo.Orders o
INNER JOIN dbo.OrdersProduct op ON op.orderID = o.orderID
WHERE o.ID IN ( xx, xx, xx, xx )
AND o.orderDate BETWEEN DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)
AND
DATEADD(DAY, -1, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) + 1, 0))