Quantcast
Channel: SCN: Message List
Viewing all articles
Browse latest Browse all 11212

Re: Help required in query problem

$
0
0

Hi Steve,

 

 

YOu can try this query:

 

SELECT

    CASE WHEN RowNum IN (1) THEN docnum ELSE '' END AS Docnum,

   CASE

    WHEN RowNum <> 1 THEN 'SubTotal'

    ELSE ItemCode

  END AS ProductName

    

     ,qty3

FROM

(

    SELECT distinct

        ITEMCODE,

        CASE

            WHEN GROUPING(docnum) = 1 and grouping(itemcode)=1 THEN 'GrandTotal'

            --WHEN GROUPING(itemcode) in (1,2) THEN 'Subtotal'

            ELSE CONVERT(VARCHAR(12), docnum, 113)

        END AS docnum,

        sum(isnull(x.qty,0)) AS qty3,

        ROW_NUMBER() OVER (PARTITION BY isnull(docnum,'') ORDER BY itemcode) AS RowNum

    FROM

        (

        select distinct a.itemcode [itemcode], a.docnum [docnum], a.CmpltQty [qty]

       

         from owor a inner join wor1 b on b.docentry = a.docentry

where a.Status <> 'c'

) x

  where    x.docnum between '161' and '162'

    GROUP BY

        itemcode,

        docnum,

        qty

       

    WITH ROLLUP

) AS T

where t.docnum <>'' and t.qty3 <> '0'

ORDER BY

    CASE WHEN itemcode IS NULL THEN 0 ELSE 1 END DESC, -- Grand Total last.

   

    T.docnum,

    T.itemcode

    ASc

 

Good luck

 

JM


Viewing all articles
Browse latest Browse all 11212

Trending Articles