Wrox Programmer Forums Subtotal
 | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersâ€™ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
July 15th, 2008, 07:15 AM
 Authorized User Join Date: Apr 2007 Location: Silvassa, Dadra & Nagar Haveli, India. Posts: 41 Thanks: 0 Thanked 0 Times in 0 Posts
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
July 15th, 2008, 02:16 PM
 Friend of Wrox Join Date: Jun 2008 Location: Snohomish, WA, USA Posts: 1,649 Thanks: 3 Thanked 141 Times in 140 Posts

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```

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is Off HTML code is OffTrackbacks are Off Pingbacks are On Refbacks are Off Forum Rules

 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

All times are GMT -4. The time now is 08:21 PM.