如何在MSSQL中获取月度数据?

时间:2016-03-09 17:23:12

标签: sql-server

我有一个查询,我根据自己的喜好手动更改日期。如何动态提取当前月份数据?

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'   

1 个答案:

答案 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))