如何从SQL今天开始下一个最早的日期?

时间:2018-05-24 16:17:43

标签: sql

我有今天过去的日期。我只需要从今天开始的下一个最早的日期。

下面的代码采用最短的日期,我正在寻找解决方案,以便从今天开始下一个最早的日期?

SELECT DISTINCT
    StyleItem.STYLE,
    StyleItem.COLOR,
    StyleItem.SIZE,
    StyleItem.UPC,
    INVENTORY.ITEMNO,
    SUM(QOHQTY) AS QOHQTY,
    dates.Dates,
    ProdOrderDetail.PRODLINEQTY
FROM 
    (SELECT
         ProdOrderDetail.ITEMNO, 
         MIN(DUEDATE) AS Dates
     FROM
         ProdOrderDetail
     GROUP BY
         ProdOrderDetail.ITEMNO) AS dates
RIGHT OUTER JOIN
    Inventory ON Inventory.ITEMNO = dates.ITEMNO
LEFT OUTER JOIN
    ProdOrderDetail ON ProdOrderDetail.DUEDATE = dates.dates 
                    AND ProdOrderDetail.ITEMNO = dates.ITEMNO
INNER JOIN
    StyleItem ON styleitem.itemno = Inventory.ITEMNO
WHERE
    StyleItem.DIVISION = 'HH' 
    AND StyleItem.MERCHGROUPA = 'NATL' 
    AND StyleItem.MERCHGROUPB <> 'MARKETING'
GROUP BY
    Inventory.ITEMNO,
    dates.dates,
    ProdOrderDetail.PRODLINEQTY,
    StyleItem.STYLE, StyleItem.COLOR, StyleItem.SIZE, StyleItem.UPC
ORDER BY
    styleitem.style, styleitem.color

1 个答案:

答案 0 :(得分:0)

基于您的RDBMS,它可能是其中之一:

SQL Server

SELECT TOP (1) AvailDate
FROM YourTable
WHERE AvailDate > GETDATE()
ORDER BY AvailDate;

SELECT MIN(AvailDate) AS AvailDate
FROM YourTable
WHERE AvailDate > GETDATE();

的MySQL

SELECT AvailDate
FROM YourTable
WHERE AvailDate > NOW()
ORDER BY AvailDate
LIMIT 1;

SELECT MIN(AvailDate) AS AvailDate
FROM YourTable
WHERE AvailDate > NOW();

在这两种情况下,您都必须过滤大于当前日期的行,然后:

  1. 按该日期排序并采用第一个元素
  2. 使用MIN()查找大于当前日期的最低日期