Skip to Content

Items sold by preferred vendor for a date range

I currently have the below query which shows me items sold by preferred vendor for a date range. I am trying to add the Sale Price for each item, but am not having any luck. Is this something you can help with?

SELECT T2.DocNum, T2.CardName, T0.[ItemCode] AS 'Item No.', T0.[SuppCatNum] AS 'Mfr Catalog No.', T0.[ItemName] AS 'Item Description', T0.[CardCode] AS 'Preferred Vendor', T0.[AvgPrice] AS 'Item Cost', SUM(T1.[Quantity]) AS 'Amount Quantity' FROM [dbo].[OITM] T0 INNER JOIN [dbo].[INV1] T1 ON T1.[ItemCode] = T0.[ItemCode] AND T1.[ItemCode] = T0.[ItemCode] LEFT JOIN OINV T2 ON T2.DocEntry=t1.DocEntry WHERE T0.[CardCode] = (N'[%1]' ) AND T1.[DocDate] >= (CONVERT(DATETIME, '[%2]', 112) ) AND T1.[DocDate] <= (CONVERT(DATETIME, '[%3]', 112) ) AND T1.[Quantity] <> (0 ) GROUP BY T2.DocNum, T2.CardName, T0.[ItemCode], T0.[SuppCatNum], T0.[ItemName], T0.[CardCode], T0.[AvgPrice] ORDER BY T0.[ItemCode],T0.[SuppCatNum]

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

2 Answers

  • Posted on Oct 14, 2019 at 11:22 PM

    Hi Kedar

    Thank you so much for responding, the sale price i am trying to add is an additional column.

    I'd like to see how much we sold it for

    Thank you

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Oct 15, 2019 at 03:44 AM
    Hi Andrea,
    You can use Windows function within SQL statement. Please refer below sample and you can modify as per your actual data set and key columns.
    
    Sample Data - for your reference
    ITEMCODE	DOCNUM	SUPPCATNUM	AVGPRICE	QUANTITY
    ABC		123	X		10		100
    ABC		345	X		20		200
    DEF		678	Y		50		500
    
    SELECT ItemCode, DocNum, SUM(AvgPrice) OVER (PARTITION BY ItemCode) AS SALE_PRICE
    , SUM(Quantity) OVER (PARTITION BY ItemCode, DocNum) AS QUANTITY
    FROM
    (
    SELECT 'ABC' AS ItemCode,
    '123' AS DocNum,
    'X' AS SuppCatNum,
    10 AS AvgPrice,
    100 AS Quantity
    FROM DUMMY
    UNION ALl
    SELECT 'ABC' AS ItemCode,
    '345' AS DocNum,
    'X' AS SuppCatNum,
    20 AS AvgPrice,
    200 AS Quantity
    FROM DUMMY
    UNION ALL
    SELECT 'DEF' AS ItemCode,
    '678' AS DocNum,
    'Y' AS SuppCatNum,
    50 AS AvgPrice,
    500 AS Quantity
    FROM DUMMY
    )
    ORDER BY ItemCode, DocNum
    ;
    
    It will give output as below:
    ITEMCODE	DOCNUM	SALE_PRICE	QUANTITY
    ABC		123	30		100
    ABC		345	30		200
    DEF		678	50		500		
    
    Regards
    Kedar
    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.