我一直在尝试这项工作。我希望显示最新的数据,因为我希望在去年同一天的同一天显示
Jul 7 2016
Jul 6 2016
Jul 5 2016
Jul 4 2016
Jul 3 2016
Jul 2 2016
Jul 1 2016
Jul 7 2015
Jul 6 2015
Jul 5 2015
Jul 4 2015
Jul 3 2015
Jul 2 2015
Jul 1 2015
这是我的代码:现在显示所有2015年的记录,我希望它像上面那样显示最新。
SELECT
OrderStatus, Sum_SellPrice, Sum_SellerMargin, Sum_BuyPrice,
OrderPeriodMonthName, OrderDate
FROM
Sum_OrderCharges
WHERE
(OrderStatus IN ('Completed', 'Invoiced', 'Open'))
AND (OrderPeriodYear IN ('2016','2015'))
AND (MONTH(OrderDate) = MONTH(GETDATE()))
ORDER BY
OrderDate
答案 0 :(得分:1)
SELECT OrderStatus, Sum_SellPrice, Sum_SellerMargin, Sum_BuyPrice, OrderPeriodMonthName, OrderDate
FROM Sum_OrderCharges
WHERE (OrderStatus IN ('Completed', 'Invoiced', 'Open')) AND (OrderPeriodYear IN ('2016','2015')) AND (MONTH(OrderDate) = MONTH(GETDATE()))
AND DAY(OrderDate) <= DAY(GETDATE())
ORDER BY OrderDate
note DAY()
将返回介于1和1之间的整数。 31不是实际日期,因此您可以使用它来比较并进一步将结果限制为实际发生的日期。
答案 1 :(得分:0)
我相信你会将以下内容附加到where子句:
AND (OrderDate <= GETDATE())
完全是:
SELECT OrderStatus, Sum_SellPrice, Sum_SellerMargin, Sum_BuyPrice, OrderPeriodMonthName, OrderDate
FROM Sum_OrderCharges
WHERE (OrderStatus IN ('Completed', 'Invoiced', 'Open')) AND (OrderPeriodYear IN ('2016','2015')) AND (MONTH(OrderDate) = MONTH(GETDATE()))
ORDER BY OrderDate
不需要:我会将其格式化为以下内容,以便您可以更好地了解where子句中的所有内容。 (更容易看到所有开括号和右括号)
SELECT OrderStatus
,Sum_SellPrice
,Sum_SellerMargin
,Sum_BuyPrice
,OrderPeriodMonthName
,OrderDate
FROM Sum_OrderCharges
WHERE (OrderStatus IN ('Completed', 'Invoiced', 'Open'))
AND (OrderPeriodYear IN ('2016','2015'))
AND (MONTH(OrderDate) = MONTH(GETDATE()))
AND (OrderDate <= GETDATE())
ORDER BY OrderDate