使用时间字段选择包含今天日期的第一次出现的记录

时间:2013-04-26 09:09:54

标签: sql sql-server sql-server-2008

我的表格中包含date字段和每个记录的time(7)字段。将记录插入数据库后,它将具有插入日期的日期并记录插入的时间。

现在我想从Table1 INNER JOIN Table2中选择今天第一次出现的日期的所有记录,因为记录可能是重复的。我已尝试使用time(7)字段选择今天record1 = 'ABC'的最短时间,但这不会返回任何结果,我确信查询应该返回1个结果,因为它会以不同的方式记录两次时间,但今天的日期。如何选择今天首次出现的产品?

示例:

SELECT ProductName, Description, Quantity,
FROM Products
INNER JOIN Transactions
    ON Products.ProductID = Transactions.ProductID
WHERE Transactions.ProductID = 'A6612'
    AND CONVERT(VARCHAR(20), `Transactions.Date_Tracked, 103) = CONVERT(VARCHAR(10), getdate(), 103)
    AND Time_Tracked = (
        SELECT min(Time_Tracked)
        FROM Transactions
        )
    AND Country = 'United States'

3 个答案:

答案 0 :(得分:2)

使用EXISTS运算符选项。此外,您可以避免使用CAST函数而不是CONVERT到VARCHAR数据类型的有效条件(或谓词)

SELECT p.ProductName, p.Description, p.Quantity
FROM Products p INNER JOIN Transactions t ON p.ProductID = t.ProductID
WHERE t.ProductID = 'A6612' 
  AND CAST(t.Date_Tracked AS date) = CAST(GETDATE() AS date)
  AND EXISTS (                      
              SELECT 1
              FROM Transactions t2
              WHERE t.ProductID = t2.ProductID
                AND CAST(t2.Date_Tracked AS date) = CAST(GETDATE() AS date)
              HAVING MIN(t2.Time_Tracked) = t.Time_Tracked                      
              ) 
  AND Country = 'United States'

如果请求返回一条记录,只需使用带有TOP子句的ORDER BY子句。

SELECT TOP 1 p.ProductName, p.Description, p.Quantity
FROM Products p INNER JOIN Transactions t ON p.ProductID = t.ProductID
WHERE t.ProductID = 'A6612' 
  AND CAST(t.Date_Tracked AS date) = CAST(GETDATE() AS date)
  AND p.Country = 'United States'
ORDER BY t.Time_Tracked

最后,如果Date_Tracked的类型为datetime,那么:

SELECT TOP 1 p.ProductName, p.Description, p.Quantity
FROM Products p INNER JOIN Transactions t ON p.ProductID = t.ProductID
WHERE t.ProductID = 'A6612' 
  AND CAST(t.Date_Tracked AS date) = CAST(GETDATE() AS date)
  AND p.Country = 'United States'
ORDER BY CAST(t.Date_Tracked AS time)

答案 1 :(得分:0)

你的查询应该是这样的,

SELECT ProductName, Description, Quantity,
FROM Products
INNER JOIN Transactions
    ON Products.ProductID = Transactions.ProductID
WHERE Transactions.ProductID = 'A6612'
    AND CONVERT(VARCHAR(20), Transactions.Date_Tracked, 103) = CONVERT(VARCHAR(10), getdate(), 103)
    AND Time_Tracked = (
        SELECT min(Time_Tracked)
        FROM Transactions
        WHERE CONVERT(VARCHAR(20), Transactions.Date_Tracked, 103) = CONVERT(VARCHAR(10), getdate(), 103)
        )
    AND Country = 'United States'

答案 2 :(得分:0)

您尝试过使用rowid吗?

SELECT ProductName, Description, Quantity,
FROM Products
INNER JOIN Transactions
    ON Products.ProductID = Transactions.ProductID
WHERE Transactions.ProductID = 'A6612'
    AND CONVERT(VARCHAR(20), `Transactions.Date_Tracked, 103) = CONVERT(VARCHAR(10), getdate(), 103)
    AND Transactions.rowid = (SELECT min(rowid)
        FROM Transactions group by productid,date) --This will give you the minimum rowid 
    AND Country = 'United States'                    --grouped by productid and date.