1

I have the following SQL script which returns duplciate values in PIVOT. How do I combine those duplicate records to one row.

Please check the below image for the results set.

SELECT *
FROM   (SELECT X.stockcode,
               X.description,
               X.pack,
               X.location,
               X.lname,
               X.qty,
               Y.stockcode AS StockCode2,
               y.periodname,
               Y.months,
               Y.saleqty
        FROM   (SELECT dbo.stock_items.stockcode,
                       dbo.stock_items.description,
                       dbo.stock_items.pack,
                       dbo.stock_loc_info.location,
                       dbo.stock_locations.lname,
                       dbo.stock_loc_info.qty
                FROM   dbo.stock_locations
                       INNER JOIN dbo.stock_loc_info
                               ON dbo.stock_locations.locno = dbo.stock_loc_info.location
                       LEFT OUTER JOIN dbo.stock_items
                                    ON dbo.stock_loc_info.stockcode = dbo.stock_items.stockcode
                WHERE  ( dbo.stock_items.status = 's' )) AS X
               LEFT OUTER JOIN (SELECT dbo.dr_invlines.stockcode,
                                       ( 12 + Datepart(month, Getdate()) - Datepart(month, dbo.dr_trans.transdate) ) % 12 + 1 AS Months,
                                       Sum(dbo.dr_invlines.quantity)                                                          AS SaleQty,
                                       dbo.period_status.periodname
                                FROM   dbo.dr_trans
                                       INNER JOIN dbo.period_status
                                               ON dbo.dr_trans.period_seqno = dbo.period_status.seqno
                                       LEFT OUTER JOIN dbo.stock_items AS STOCK_ITEMS_1
                                                       RIGHT OUTER JOIN dbo.dr_invlines
                                                                     ON STOCK_ITEMS_1.stockcode = dbo.dr_invlines.stockcode
                                                    ON dbo.dr_trans.seqno = dbo.dr_invlines.hdr_seqno
                                WHERE  ( STOCK_ITEMS_1.status = 'S' )
                                       AND ( dbo.dr_trans.transtype IN ( 1, 2 ) )
                                       AND ( dbo.dr_trans.transdate >= Dateadd(m, -6, Getdate()) )
                                GROUP  BY dbo.dr_invlines.stockcode,
                                          Datepart(month, dbo.dr_trans.transdate),
                                          dbo.period_status.periodname) AS Y
                            ON X.stockcode = Y.stockcode) z
       PIVOT (Sum(saleqty) FOR [months] IN ([1],[2],[3],[4],[5],[6])) AS pivoted  

enter image description here

3
  • 1
    There is a column PERIODNAME are different value. what's your expect result? Commented Apr 13, 2019 at 2:05
  • "Feed" the pivot only the required columns. As D-Shih alludes to, remove PERIODNAME from your query. Commented Apr 13, 2019 at 2:16
  • I will remove the period name column then Commented Apr 13, 2019 at 2:20

1 Answer 1

1

EDIT: I missed the root-cause of your issue being the inclusion of the periodname column causing the percieved duplication. I am leaving this in place as general solution showing CTE usage, because it could still be useful if you then want to do extra filtering/transformation of your pivot results

One way is to take the results of the pivot query and run it through a SELECT DISTINCT query.

An example of wrapping your pivot query as a CTE and using it to feed a SELECT DISTINCT below (please note: untested, but parses as valid in my SSMS)

WITH PivotResults_CTE (
    stockcode,
    description,
    pack,
    location,
    lname,
    qty,
    StockCode2,
    periodname,
    months,
    saleqty
)
AS (
    SELECT *
    FROM (
        SELECT X.stockcode
            ,X.description
            ,X.pack
            ,X.location
            ,X.lname
            ,X.qty
            ,Y.stockcode AS StockCode2
            ,y.periodname
            ,Y.months
            ,Y.saleqty
        FROM (
            SELECT dbo.stock_items.stockcode
                ,dbo.stock_items.description
                ,dbo.stock_items.pack
                ,dbo.stock_loc_info.location
                ,dbo.stock_locations.lname
                ,dbo.stock_loc_info.qty
            FROM dbo.stock_locations
            INNER JOIN dbo.stock_loc_info ON dbo.stock_locations.locno = dbo.stock_loc_info.location
            LEFT OUTER JOIN dbo.stock_items ON dbo.stock_loc_info.stockcode = dbo.stock_items.stockcode
            WHERE (dbo.stock_items.STATUS = 's')
            ) AS X
        LEFT OUTER JOIN (
            SELECT dbo.dr_invlines.stockcode
                ,(12 + Datepart(month, Getdate()) - Datepart(month, dbo.dr_trans.transdate)) % 12 + 1 AS Months
                ,Sum(dbo.dr_invlines.quantity) AS SaleQty
                ,dbo.period_status.periodname
            FROM dbo.dr_trans
            INNER JOIN dbo.period_status ON dbo.dr_trans.period_seqno = dbo.period_status.seqno
            LEFT OUTER JOIN dbo.stock_items AS STOCK_ITEMS_1
            RIGHT OUTER JOIN dbo.dr_invlines ON STOCK_ITEMS_1.stockcode = dbo.dr_invlines.stockcode ON dbo.dr_trans.seqno = dbo.dr_invlines.hdr_seqno WHERE (STOCK_ITEMS_1.STATUS = 'S')
                AND (
                    dbo.dr_trans.transtype IN (
                        1
                        ,2
                        )
                    )
                AND (dbo.dr_trans.transdate >= Dateadd(m, - 6, Getdate()))
            GROUP BY dbo.dr_invlines.stockcode
                ,Datepart(month, dbo.dr_trans.transdate)
                ,dbo.period_status.periodname
            ) AS Y ON X.stockcode = Y.stockcode
        ) z
    PIVOT(Sum(saleqty) FOR [months] IN (
                [1]
                ,[2]
                ,[3]
                ,[4]
                ,[5]
                ,[6]
                )) AS pivoted
)
SELECT DISTINCT *
FROM
    PivotResults_CTE
; 

Also note, your sql included in the above may look slightly different to your original but that is only because i ran it through a reformatter to ensure i understood the structure of it.

In other words, the basic CTE wrapper for your pivot query is:

WITH PivotResults_CTE (
    Field1,
    Field2,
    ...
)
AS (
    YOUR_PIVOT_QUERY_HERE
)
SELECT DISTINCT *
FROM
    PivotResults_CTE
;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.