简体   繁体   English

最大化列的DB2连接查询

[英]DB2 Join Query that Maximizes Column

I am OK at basic SQL, but my understanding fails when it comes to more complex searches. 我对基本的SQL没问题,但是对于更复杂的搜索,我的理解失败了。

Right now, I am trying to return records for Location 16. 现在,我正在尝试返回位置16的记录。

Most records are empty, so I cannot debug my database application with those records. 大多数记录为空,因此我无法使用这些记录调试数据库应用程序。

SELECT
  I.PART_NUM, I.ID, L.SHELF, L.IN_STOCK
FROM
  INVENTORY I
  JOIN LOCATIONS L ON I.ID=L.INV_ID
WHERE
  L.ID=16 AND PART_NUM IN 
  (
    SELECT
      TOP 10 N.PART_NUM
    FROM
      INVENTORY N 
      JOIN LOCATIONS T ON N.ID=T.INV_ID
    WHERE
      T.ID=16
    ORDER BY 
      IN_STOCK
  )
ORDER BY 
      IN_STOCK, I.PART_NUM

Developers are not given direct access to the database, but rather make calls using an in-house developed application that permits basic SQL calls. 开发人员没有直接访问数据库的权限,而是使用允许基本SQL调用的内部开发应用程序进行调用。

When I try running the code below, I get the following error: 当我尝试运行下面的代码时,出现以下错误:

ERROR [42601][IBM][DB2/AIX64] SQL0104N An unexpected token "10" was found following "DOR IN ( SELECT TOP". Expected tokens may include: "CONCAT". 错误[42601] [IBM] [DB2 / AIX64] SQL0104N在“ DOR IN(SELECT TOP)”之后发现意外的标记“ 10”。预期的标记可能包括:“ CONCAT”。

I wasn't sure if this was a limitation of our in-house developed application or something in DB2. 我不确定这是我们内部开发的应用程序的限制还是DB2中的某些限制。

I went to the SQL Tutorial here: 我在这里进入了SQL教程:

http://www.w3schools.com/sql/trysql.asp?filename=trysql_select_all http://www.w3schools.com/sql/trysql.asp?filename=trysql_select_all

There, I used the tables they supply to create a similar query: 在那里,我使用了它们提供的表来创建类似的查询:

SELECT 
    OrderID, ProductID, Quantity, LastName, FirstName
FROM 
    OrderDetails I 
    JOIN Orders L ON I.OrderID=L.OrderID
WHERE 
    EmployeeID=5 AND OrderDetailID IN 
    (
        SELECT 
            TOP 10 N.OrderDetailID 
        FROM 
            OrderDetails N 
            JOIN Orders T ON N.OrderID=T.OrderID
        WHERE 
            EmployeeID=5
        ORDER BY 
            Quantity
    )
ORDER BY 
    Quantity

It gives me a similar error: 它给了我一个类似的错误:

Error 1: could not prepare statement (1 near "10": syntax error) 错误1:无法准备语句(“ 10”附近的1:语法错误)

What do I need to do to modify my SQL query to return the Vendors with the most quantity in stock? 我该怎么做才能修改我的SQL查询以退回库存数量最多的供应商?

I can order the LOCATIONS table by the IN_STOCK quantity, but then I cannot filter to get INVENTORY records that are valid (lots of test data in the database). 我可以按IN_STOCK数量对LOCATIONS表进行排序 ,但是随后我无法进行过滤以获取有效的INVENTORY记录(数据库中的大量测试数据)。

The clause Select TOP is not valid on DB2. Select TOP子句在DB2上无效。 Intead use 互联网使用

SELECT * 
FROM myTable
ORDER BY id
FETCH FIRST 10 ROWS ONLY

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM