我不是一个真正的开发人员,我今年才开始自学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]
3条答案
按热度按时间7eumitmz1#
我发现https://kristiannielsen.livejournal.com/6745.html 并实施了“最快”的解决方案。
只是看起来不是很快。。。返回217行1分12秒。如果您在远程工作,查询是否运行较慢?
q5lcpyga2#
我从头开始重新构建了查询,它似乎可以工作,尽管我还没有添加几乎所有的列。
mf98qq943#
代替
然后在子选择中添加列,该子选择将确定具有最新日期的注解行:
您还需要将所选列替换为通过子选择添加的新列: