sql:显示最新条目

pkmbmrz7  于 2021-08-09  发布在  Java
关注(0)|答案(3)|浏览(374)

我不是一个真正的开发人员,我今年才开始自学vba for excel,两天前才开始使用sql。。。
这就是说:我正试图写一个查询,只显示每个invbticketno基于ittrandate的最新条目,但我似乎无法找到它。

SELECT
  WHSNAME.WHSNNAME AS [Warehouse Name]
 ,VALID_BIN.BIN_TYPE AS T
 ,INVENTORY_BIN.INVBBINNO AS [Bin Number]
 ,INVENTORY_BIN.INVBITEMNO AS [Item Number]
 ,INVENTORY_ITEM_TAB.DESCR AS [Item Description]
 ,PRODUCT_CODE.DESCR AS [Product Code & Description]
 ,CUSTOMER.CSNAME AS [Customer Name]
 ,ORDERS.ORDER_NO AS [Oder No]
 ,INVENTORY_BIN.DATE_OF_INVENTORY AS [Date of Inventory]
 ,INVENTORY_BIN.INVBTICKETNO AS [Ticket No]
 ,INVENTORY_BIN.INVBQTYONHAND AS [Qty on Hand]
 ,ORDERS.QTY_ORDERED AS [Qty Ordered]
 ,INVENTORY_ITEM_TAB.SQUARE_FEET_UNIT AS MSF
 ,INVENTORY_BIN.EXTENDED_CST AS [Valuation?]
 ,INVENTORY_TRAN.ITSOURCE AS Source
 ,INVENTORY_TRAN.ITCOMMENT AS Comment
 ,MAX(INVENTORY_TRAN.ITTRANDATE) AS [Trans Date]
 ,ORDERS.FOR_INVT_FLG AS Flag
 ,SALESAGENT.SANAME AS [Sales Agent]
 ,ORDERS.CUST_PO_NO AS [Customer PO No]
FROM dbo.INVENTORY_BIN INVENTORY_BIN
INNER JOIN dbo.INVENTORY_ITEM_TAB INVENTORY_ITEM_TAB
  ON INVENTORY_BIN.INVBITEMNO = INVENTORY_ITEM_TAB.ITEM_NO
INNER JOIN dbo.WHSNAME WHSNAME
  ON INVENTORY_BIN.INVBWHSNCODE = WHSNAME.WHSNCODE
INNER JOIN dbo.VALID_BIN VALID_BIN
  ON INVENTORY_BIN.INVBBINNO = VALID_BIN.BIN_ID
    AND INVENTORY_BIN.INVBWHSNCODE = VALID_BIN.WHSNCODE
LEFT OUTER JOIN dbo.INVENTORY_TRAN INVENTORY_TRAN
  ON INVENTORY_BIN.INVBTICKETNO = INVENTORY_TRAN.ITTICKETNO
    AND INVENTORY_BIN.DATE_LST_ACTIVITY < INVENTORY_TRAN.ITTRANDATE
LEFT OUTER JOIN dbo.CUSTOMER CUSTOMER
  ON INVENTORY_BIN.CSCODE = CUSTOMER.CSCODE
LEFT OUTER JOIN dbo.ORDERS ORDERS
  ON INVENTORY_BIN.ORDER_NO = ORDERS.ORDER_NO
INNER JOIN dbo.PRODUCT_CODE PRODUCT_CODE
  ON INVENTORY_ITEM_TAB.PROD_CD = PRODUCT_CODE.PROD_CD
LEFT OUTER JOIN dbo.SALESAGENT SALESAGENT
  ON CUSTOMER.SACODE = SALESAGENT.SACODE
WHERE ORDERS.FOR_INVT_FLG = 'R'
GROUP BY INVENTORY_BIN.INVBITEMNO
        ,INVENTORY_BIN.INVBBINNO
        ,INVENTORY_BIN.INVBQTYONHAND
        ,INVENTORY_BIN.DATE_OF_INVENTORY
        ,INVENTORY_BIN.EXTENDED_CST
        ,INVENTORY_ITEM_TAB.DESCR
        ,INVENTORY_ITEM_TAB.SQUARE_FEET_UNIT
        ,WHSNAME.WHSNNAME
        ,VALID_BIN.BIN_TYPE
        ,INVENTORY_TRAN.ITSOURCE
        ,CUSTOMER.CSNAME
        ,ORDERS.ORDER_NO
        ,ORDERS.CUST_PO_NO
        ,ORDERS.QTY_ORDERED
        ,PRODUCT_CODE.DESCR
        ,SALESAGENT.SANAME
        ,ORDERS.FOR_INVT_FLG
        ,INVENTORY_BIN.INVBTICKETNO
        ,INVENTORY_TRAN.ITCOMMENT
ORDER BY [Date of Inventory]
7eumitmz

7eumitmz1#

我发现https://kristiannielsen.livejournal.com/6745.html 并实施了“最快”的解决方案。
只是看起来不是很快。。。返回217行1分12秒。如果您在远程工作,查询是否运行较慢?

SELECT
  WHSNAME.WHSNNAME AS [Warehouse Name]
 ,VALID_BIN.BIN_TYPE AS T
 ,INVENTORY_BIN.INVBBINNO AS [Bin Number]
 ,INVENTORY_BIN.INVBITEMNO AS [Item Number]
 ,INVENTORY_ITEM_TAB.DESCR AS [Item Description]
 ,PRODUCT_CODE.DESCR AS [Product Code & Description]
 ,CUSTOMER.CSNAME AS [Customer Name]
 ,ORDERS.ORDER_NO AS [Order Number]
 ,INVENTORY_BIN.DATE_OF_INVENTORY AS [Date of Inventory]
 ,tra1.ITTICKETNO AS [Ticket Number]
 ,INVENTORY_BIN.INVBQTYONHAND AS [Qty on Hand]
 ,ORDERS.QTY_ORDERED AS [Qty Ordered]
 ,INVENTORY_ITEM_TAB.SQUARE_FEET_UNIT AS MSF
 ,INVENTORY_BIN.EXTENDED_CST AS [Valuation?]
 ,tra1.ITSOURCE AS Source
 ,tra1.ITCOMMENT AS Comment
 ,tra1.ITTRANDATE AS [Trans Date]
 ,ORDERS.FOR_INVT_FLG AS Flag
 ,SALESAGENT.SANAME AS [Sales Agent]
 ,ORDERS.CUST_PO_NO AS [Customer PO Number]
FROM dbo.INVENTORY_TRAN tra1

**INNER JOIN (SELECT

    INVENTORY_TRAN.ITTICKETNO
   ,MAX(INVENTORY_TRAN.ITTRANDATE) AS ITTRANDATE
  FROM dbo.INVENTORY_TRAN
  GROUP BY INVENTORY_TRAN.ITTICKETNO) tra2
  ON tra1.ITTICKETNO = tra2.ITTICKETNO
    AND tra1.ITTRANDATE = tra2.ITTRANDATE**
INNER JOIN dbo.INVENTORY_BIN
  ON tra1.ITTICKETNO = INVENTORY_BIN.INVBTICKETNO
INNER JOIN dbo.INVENTORY_ITEM_TAB
  ON INVENTORY_BIN.INVBITEMNO = INVENTORY_ITEM_TAB.ITEM_NO
INNER JOIN dbo.PRODUCT_CODE
  ON INVENTORY_ITEM_TAB.PROD_CD = PRODUCT_CODE.PROD_CD
INNER JOIN dbo.ORDERS
  ON INVENTORY_BIN.ORDER_NO = ORDERS.ORDER_NO
INNER JOIN dbo.WHSNAME
  ON INVENTORY_BIN.INVBWHSNCODE = WHSNAME.WHSNCODE
INNER JOIN dbo.CUSTOMER
  ON INVENTORY_BIN.CSCODE = CUSTOMER.CSCODE
INNER JOIN dbo.SALESAGENT
  ON CUSTOMER.SACODE = SALESAGENT.SACODE
INNER JOIN dbo.VALID_BIN
  ON INVENTORY_BIN.INVBWHSNCODE = VALID_BIN.WHSNCODE
    AND VALID_BIN.BIN_ID = INVENTORY_BIN.INVBBINNO
WHERE ORDERS.FOR_INVT_FLG = 'R'
ORDER BY [Date of Inventory]
q5lcpyga

q5lcpyga2#

我从头开始重新构建了查询,它似乎可以工作,尽管我还没有添加几乎所有的列。

SELECT
  MAX(INVENTORY_TRAN.ITTRANDATE) AS expr1
 ,INVENTORY_BIN.INVBTICKETNO
 ,ORDERS.ORDER_NO
 ,ORDERS.FOR_INVT_FLG
FROM dbo.INVENTORY_TRAN
INNER JOIN dbo.INVENTORY_BIN
  ON INVENTORY_TRAN.ITTICKETNO = INVENTORY_BIN.INVBTICKETNO
INNER JOIN dbo.ORDERS
  ON INVENTORY_BIN.ORDER_NO = ORDERS.ORDER_NO
WHERE ORDERS.FOR_INVT_FLG = 'r'
GROUP BY INVENTORY_BIN.INVBTICKETNO
        ,ORDERS.ORDER_NO
        ,ORDERS.FOR_INVT_FLG
ORDER BY INVENTORY_BIN.INVBTICKETNO
mf98qq94

mf98qq943#

代替

LEFT OUTER JOIN dbo.INVENTORY_TRAN INVENTORY_TRAN
  ON INVENTORY_BIN.INVBTICKETNO = INVENTORY_TRAN.ITTICKETNO
    AND INVENTORY_BIN.DATE_LST_ACTIVITY < INVENTORY_TRAN.ITTRANDATE

然后在子选择中添加列,该子选择将确定具有最新日期的注解行:

INNER JOIN (SELECT
   INVENTORY_TRAN.ITTICKETNO
   ,INVENTORY_TRAN.ITSOURCE AS [Source]
   ,INVENTORY_TRAN.ITCOMMENT AS [Comment]
   ,MAX(INVENTORY_TRAN.ITTRANDATE) [Trans Date]
FROM [INVENTORY_TRAN]
GROUP BY INVENTORY_TRAN.ITTICKETNO, INVENTORY_TRAN.ITSOURCE, INVENTORY_TRAN.ITCOMMENT
HAVING MAX(INVENTORY_TRAN.ITTRANDATE) = INVENTORY_TRAN.ITTRANDATE
) AS [sub_INVENTORY_TRAN] ON [INVENTORY_BIN].[INVBTICKETNO] = [sub_INVENTORY_TRAN].[ITTICKETNO]

您还需要将所选列替换为通过子选择添加的新列:

,[sub_INVENTORY_TRAN].[Source]
   ,[sub_INVENTORY_TRAN].[Comment]
   ,[sub_INVENTORY_TRAN].[Trans Date]

相关问题