我使用以下查询来提取一年的未来订单。有没有什么方法可以在今天日期之前和之后提取数据?
SELECT ord_ln.prdnum AS 'Product', ord.cusnam, product.prddsc AS 'Description',
ord_ln.stdnetwgt AS 'Case_Weight', ord_ln.prdlincod AS 'Line_Code',
ord_ln.ordqty AS 'Order_Qty', ord.plnshpdat AS 'Plan_Ship_Date'
FROM velocity.dbo.ord ord,
velocity.dbo.ord_ln ord_ln,
velocity.dbo.product product
WHERE ord.ordtky = ord_ln.ordtky
AND product.prdtky = ord_ln.prdtky
AND ((ord.plnshpdat Between DATEADD(dd,0,GETDATE()) And DATEADD(dd,365,GETDATE())))
ORDER BY ord_ln.prdlincod
提前致谢。
答案 0 :(得分:0)
在查询中使用之前声明2个变量并计算年数。(以防万一你与表现有关;)
Declare @NextYearDate Date= DATEADD(YY,1,getdate())
Declare @PrevYearDate Date= DATEADD(YY,-1,getdate())
SELECT ord_ln.prdnum AS 'Product', ord.cusnam, product.prddsc AS 'Description',
ord_ln.stdnetwgt AS 'Case_Weight', ord_ln.prdlincod AS 'Line_Code',
ord_ln.ordqty AS 'Order_Qty', ord.plnshpdat AS 'Plan_Ship_Date'
FROM velocity.dbo.ord ord,
velocity.dbo.ord_ln ord_ln,
velocity.dbo.product product
WHERE ord.ordtky = ord_ln.ordtky
AND product.prdtky = ord_ln.prdtky
AND (ord.plnshpdat Between @PrevYearDate and @NextYearDate)
ORDER BY ord_ln.prdlincod
答案 1 :(得分:0)
这就是我编写查询的方式:
SELECT ol.prdnum as Product, o.cusnam, p.prddsc as Description,
ol.stdnetwgt as Case_Weight, ol.prdlincod as Line_Code,
ol.ordqty as Order_Qty, o.plnshpdat as Plan_Ship_Date
FROM velocity.dbo.ord o JOIN
velocity.dbo.ord_ln ol
ON o.ordtky = ol.ordtky JOIN
velocity.dbo.product p
ON p.prdtky = ol.prdtky
WHERE o.plnshpdat Between DATEADD(year, -1, GETDATE()) And DATEADD(year, 1, GETDATE())
ORDER BY ol.prdlincod;
的变化:
JOIN
语法。 从不在FROM
子句中使用逗号。