Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8711233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:49:28+00:00 2026-06-13T04:49:28+00:00

I am trying to modify a user query in order to return an extra

  • 0

I am trying to modify a user query in order to return an extra column, INV1.WhsCode from the following SQL query:

SELECT  T0.CardCode,
        T2.CardName,
        T0.CodeBars,
        T0.ItemCode,
        T0.ItemName,
        T3.Price AS [POS Price],
        T1.AvgPrice,
        T1.OnHand,
        T1.MinStock,
        T1.MaxStock,
        T0.NumInBuy AS Packsize,
        T0.LstSalDate,
        (
            SELECT SUM(Quantity) AS Expr1
            FROM dbo.INV1
            INNER JOIN OINV
                    ON INV1.DocEntry = OINV.DocEntry
            WHERE INV1.ItemCode = T0.ItemCode
                    AND INV1.WhsCode = [%2]
                    AND Month(OINV.DocDate) = month(GetDate())
        ) AS [Current Period],
        (
            SELECT SUM(Quantity) AS Expr1
            FROM dbo.INV1
            INNER JOIN OINV
                    ON INV1.DocEntry = OINV.DocEntry
            WHERE INV1.ItemCode = T0.ItemCode
                    AND INV1.WhsCode = [%2]
                    AND Month(OINV.DocDate) = month(GetDate()) - 1
        ) AS [Previous Period],
        (
            SELECT SUM(Quantity) AS Expr1
            FROM dbo.INV1
            INNER JOIN OINV
                    ON INV1.DocEntry = OINV.DocEntry
            WHERE INV1.ItemCode = T0.ItemCode
                    AND INV1.WhsCode = [%2]
                    AND Month(OINV.DocDate) = month(GetDate()) - 2
        ) AS [60-90],
        (
            SELECT TOP 1 OPDN.DocDate AS Expr1
            FROM dbo.PDN1
            INNER JOIN OPDN
                    ON PDN1.DocEntry = OPDN.DocEntry
            WHERE PDN1.ItemCode = T0.ItemCode
            ORDER BY OPDN.DocDate DESC
        ) AS LastGRNDate
FROM    OITM T0
        INNER JOIN OITW T1
            ON T0.ItemCode = T1.ItemCode
        INNER JOIN OCRD T2
            ON T0.CardCode = T2.CardCode
        INNER JOIN ITM1 T3
            ON T0.ItemCode = T3.ItemCode
        INNER JOIN OWHS T4
            ON T1.WhsCode = T4.WhsCode
        INNER JOIN OITB T5
            ON T0.ItmsGrpCod = T5.ItmsGrpCod
WHERE   T3.PriceList = '3'
        AND T4.WhsName = [%0]
        AND T5.ItmsGrpNam = [%1]

How do I achieve this? (MS SQL Server 2008)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T04:49:29+00:00Added an answer on June 13, 2026 at 4:49 am

    There are a number of things wrong/inefficient about your query which I know you didn’t ask about, but I am going to answer anyway because it will help answer the question you did ask.

    You need to avoid correlated subqueries where possible, there are times that they are unavoidable and the best solution, however I so often see them in a place where a JOIN would do the same job and the optimiser will deal with join so much better. For instance you have:

    SELECT  (
                SELECT TOP 1 OPDN.DocDate AS Expr1
                FROM dbo.PDN1
                INNER JOIN OPDN
                        ON PDN1.DocEntry = OPDN.DocEntry
                WHERE PDN1.ItemCode = T0.ItemCode
                ORDER BY OPDN.DocDate DESC
            ) AS LastGRNDate
    FROM    OITM T0
    

    This evaluates the subquery for each row, whereas if you re-wrote as so:

    SELECT  LastGRN.LastGRNDate
    FROM    OITM TO
            LEFT JOIN
            (   SELECT  PDN1.ItemCode, [LastGRNDate] = MAX(OPDN.DocDate)
                FROM    dbo.PDN1
                        INNER JOIN OPDN
                            ON PDN1.DocEntry = OPDN.DocEntry
                GROUP BY PDN1.ItemCode
            ) LastGRN
                ON LastGRN.ItemCode = T0.ItemCode
    

    you would get the same result, but evaluated in a much more efficent manner.

    The next fault is your method of using MONTH(GETDATE()) – 1 to get 2 months ago. In January this will evauluate to 0 and get no matches. The best way to do this is to convert each date to the first of each month using something akin to his:

    SELECT  [FirstOfThisMonth] = DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0),
            [FirstOfLastMonth] = DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE() - 1), 0)
    

    The same principle of joins rather than correlated subqueries can also be applied to your quantity columns, and this gives access to the WhsCode columns, it is not necessary, but I have used a common table expression to clean up the query (using the date logic from above)

    WITH Quantities AS
    (   SELECT  [DocMonth] = DATEADD(MONTH, DATEDIFF(MONTH, 0, IONV.DocDate),
                Inv1.WhsCode,
                ItemCode,
                [Quantity] = SUM(Quantity)
        FROM    dbo.Inv1
                INNER JOIN OINV
                    ON Inv1.DocEntry = OINV.DocEntry
        GROUP BY DATEADD(MONTH, DATEDIFF(MONTH, 0, IONV.DocDate), WhsCode, itemCode
    )
    SELECT  T0.ItemCode,
            [Current Period] = COALESCE(Cur.Quantity, 0),
            [Previous Period] = COALESCE(prev.Quantity, 0),
            [60-90] = COALESCE(prev2.Quantity, 0)
    FROM    OITM T0
            LEFT JOIN Quantities cur
                ON cur.ItemCode = T0.ItemCode
                AND cur.DocDate = DATEADD(MONTH, DATEDIFF(MONTH, 0, CURRENT_TIMESTAMP), 0)
                AND Cur.WhsCode = [%2]
            LEFT JOIN Quantities prev
                ON prev.ItemCode = T0.ItemCode
                AND prev.DocDate = DATEADD(MONTH, DATEDIFF(MONTH, 0, CURRENT_TIMESTAMP) - 1, 0)
                AND prev.WhsCode = [%2]
            LEFT JOIN Quantities prev2
                ON prev2.ItemCode = T0.ItemCode
                AND prev2.DocDate = DATEADD(MONTH, DATEDIFF(MONTH, 0, CURRENT_TIMESTAMP) - 2, 0)
                AND prev2.WhsCode = [%2]
    

    Combining all this into your final query gives:

    SELECT  T0.CardCode,
            T2.CardName,
            T0.CodeBars,
            T0.ItemCode,
            T0.ItemName,
            T3.Price,
            T1.AvgPrice,
            T1.OnHand,
            T1.MinStock,
            T1.MaxStock,
            T0.NumInBuy AS Packsize,
            T0.LstSalDate
            [Current Period] = COALESCE(Cur.Quantity, 0),
            [Previous Period] = COALESCE(prev.Quantity, 0),
            [60-90] = COALESCE(prev2.Quantity, 0)
            LastGRN.LastGRNDate
    FROM    OITM T0
            INNER JOIN OITW T1
                ON T0.ItemCode = T1.ItemCode
            INNER JOIN OCRD T2
                ON T0.CardCode = T2.CardCode
            INNER JOIN ITM1 T3
                ON T0.ItemCode = T3.ItemCode
            INNER JOIN OWHS T4
                ON T1.WhsCode = T4.WhsCode
            INNER JOIN OITB T5
                ON T0.ItmsGrpCod = T5.ItmsGrpCod
            LEFT JOIN Quantities cur
                ON cur.ItemCode = T0.ItemCode
                AND cur.DocDate = DATEADD(MONTH, DATEDIFF(MONTH, 0, CURRENT_TIMESTAMP), 0)
                AND Cur.WhsCode = [%2]
            LEFT JOIN Quantities prev
                ON prev.ItemCode = T0.ItemCode
                AND prev.DocDate = DATEADD(MONTH, DATEDIFF(MONTH, 0, CURRENT_TIMESTAMP) - 1, 0)
                AND prev.WhsCode = [%2]
            LEFT JOIN Quantities prev2
                ON prev2.ItemCode = T0.ItemCode
                AND prev2.DocDate = DATEADD(MONTH, DATEDIFF(MONTH, 0, CURRENT_TIMESTAMP) - 2, 0)
                AND prev2.WhsCode = [%2]
            LEFT JOIN
            (   SELECT  PDN1.ItemCode, [LastGRNDate] = MAX(OPDN.DocDate)
                FROM    dbo.PDN1
                        INNER JOIN OPDN
                            ON PDN1.DocEntry = OPDN.DocEntry
                GROUP BY PDN1.ItemCode
            ) LastGRN
                ON LastGRN.ItemCode = T0.ItemCode
    WHERE   T3.PriceList = '3'
    AND     T4.WhsName = [%0]
    AND     T5.ItmsGrpNam = [%1]
    

    This is all untested so there may be some typos/slight syntax errors, but the same principals should still apply.

    If you are still set on the idea of correlated subqueries, you could use APPLY to allow you to access multiple columns from it. e.g.

    SELECT  T0.Code, cur.WhsCode, cur.Expr1 AS [Current Period]
    FROM    OITM T0
            OUTER APPLY
            (   SELECT  INV1.WhsCode, SUM(Quantity) AS Expr1
                FROM    dbo.INV1
                        INNER JOIN OINV
                            ON INV1.DocEntry = OINV.DocEntry
                WHERE   INV1.ItemCode = T0.ItemCode
                AND     INV1.WhsCode = [%2]
                AND     MONTH(OINV.DocDate) = MONTH(GETDATE())
            ) cur
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an sql query. I want to allow user to modify only relational
I'm trying to modify the way FOSUSerBundle validates the username/email when the user registers,
I am trying to using the JS to take user input and modify certain
I'm trying to modifty the Flex Tree control to allow a user to select
I am trying to modify an Integer field on existing table from nullable to
I am trying to modify the code below to check the input from a
I'm trying to create a query that pulls information about sellers from my database,
I'm trying to modify an existing shell script to accept user input and handle
I am trying to modify the System.Drawing.Printing.PrinterSettings object that I get from the System.Windows.Forms.PrintDialog
I'm trying to modify some code that returns results from the database. Currently the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.