Wrox Programmer Forums
|
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 15th, 2008, 07:15 AM
Authorized User
 
Join Date: Apr 2007
Posts: 41
Thanks: 0
Thanked 0 Times in 0 Posts
Default Subtotal

How to calculate the subtotal of a column RMVALUE with respect to PWO_NO,
if the output is coming like this :

PWO_NO RQTY RMVALUE
200,000,001 23 587
200,000,001 42 10,396
200,000,001 324 24,653
200,000,001 599 72
200,000,001 1,054 119,563
200,000,002 23 587
200,000,002 41 10,149
200,000,002 324 24,653
200,000,002 599 72
200,000,002 1,054 119,563
200,000,003 60 14,852
200,000,003 300 23,063


SELECT DISTINCT
PRWD_WO_NO PWO_NO,
SUM(PRI_QTY_BU/IU_MAX_LOOSE_1)RQTY,
SUM((PRI_QTY_BU/IU_MAX_LOOSE_1)*CGCV_COST_1)RMVALUE

FROM
OT_PR_ITEM,
OS_COST_GROUP_CURR_VAL,
OM_ITEM,
OM_ITEM_UOM,
OT_PR_WO_DETAIL
WHERE PRI_ITEM_CODE = CGCV_ITEM_CODE
AND PRI_ITEM_CODE = ITEM_CODE
AND PRI_ITEM_CODE = IU_ITEM_CODE
AND PRWD_SYS_ID = PRI_PRWD_SYS_ID
AND CGCV_COST_GROUP_CODE = 'CGSIL001'
GROUP BY PRI_PRWD_SYS_ID,PRWD_WO_NO,PRI_ITEM_CODE,ITEM_NAME ,CGCV_COST_1,PRI_QTY,PRI_QTY_LS,PRI_QTY_BU,
         IU_MAX_LOOSE_1
ORDER BY PRWD_WO_NO


Now

Yogesh
__________________
Yogesh
 
Old July 15th, 2008, 02:16 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Making it too hard.

First of all, NEVER combine DISTINCT with GROUP BY. Which means don't combine DISTINCT with aggregate functions, such as SUM( ).

And then when you GROUP BY, you only group by the columns (a) in the SELECT and (b) *NOT* in aggregate functions such as SUM().

So:

Code:
SELECT PRWD_WO_NO as PWO_NO,
       SUM(PRI_QTY_BU/IU_MAX_LOOSE_1) as RQTY,
       SUM((PRI_QTY_BU/IU_MAX_LOOSE_1)*CGCV_COST_1) as RMVALUE
FROM 
OT_PR_ITEM,
OS_COST_GROUP_CURR_VAL,
OM_ITEM,
OM_ITEM_UOM,
OT_PR_WO_DETAIL
WHERE  PRI_ITEM_CODE    = CGCV_ITEM_CODE
AND    PRI_ITEM_CODE   = ITEM_CODE
AND    PRI_ITEM_CODE   = IU_ITEM_CODE
AND    PRWD_SYS_ID    =  PRI_PRWD_SYS_ID
AND    CGCV_COST_GROUP_CODE = 'CGSIL001' 
GROUP BY PRWD_WO_NO 
ORDER BY PRWD_WO_NO





Similar Threads
Thread Thread Starter Forum Replies Last Post
Subtotal fields jsims Access 1 November 10th, 2008 11:34 AM
Multilevel subtotal of columns yogeshyl SQL Language 1 July 15th, 2008 02:12 PM
QUERY SUBTOTAL NEWAGE Access 2 November 8th, 2006 11:59 AM
percent of subtotal mtlpp Excel VBA 0 May 14th, 2005 10:09 AM
SubTotal Field geoffeg BOOK: Professional SQL Server Reporting Services ISBN: 0-7645-6878-7 1 September 23rd, 2004 06:20 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.