1

我有一个表,每个记录都有一个date字段和一个time(7)字段。将记录插入数据库后,它将记录插入日的日期和插入时间。

现在我想从中选择第一次出现Table1 INNER JOIN Table2今天日期的所有记录,因为这些记录可以是重复的。我尝试使用该time(7)字段来选择今天日期的最短时间 where 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'
4

3 回答 3

2

将选项与 EXISTS 运算符一起使用。此外,您可以避免使用 CAST 函数而不是 CONVERT into 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)
于 2013-04-26T09:20:14.653 回答
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.  
于 2013-04-26T09:35:41.920 回答
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'
于 2013-04-26T09:20:09.330 回答