question

Mark Smith avatar image
Mark Smith asked

Sum a calculated column

I have a query where I calculate ((sum(field a) / 1000) * field b) as total. I now need to calculate total of newly created total column. Is this possible?

I am a little new at this so I am posting the actual query as I am not sure how to incorporate the above into the query, although I am tyring currently. Anyhow here's the query;

select wip_fg.order_no, sum(wip_fg.trx_qty) as trx_qty, ((sum(wip_fg.trx_qty) / 1000) * orders.ord_price) as ord_$

from wip_fg inner join orders on wip_fg.order_no=orders.order_no inner join spec_file on wip_fg.item_no=spec_file.item_no

where wip_fg.trx_datetime>='2010-1-20 07:00' and wip_fg.trx_datetime<='2010-1-20 15:00'

and spec_file.prod_cd in ('chip', 'chip1', 'chip2') and orders.plt_no=1

group by wip_fg.order_no, orders.ord_price

I am not sure where to place the above. Your help is greatly appreciated!

sql-server-2005query
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Scot Hauder avatar image
Scot Hauder answered
            
;WITH cte AS (            
SELECT (SUM(a)/1000)*b [c]            
FROM a            
GROUP BY a,b            
)            
SELECT SUM (c)            
FROM cte            
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Mark Smith avatar image
Mark Smith answered

I am a little new at this so I am posting the actual query as I am not sure how to incorporate the above into the query, although I am tyring currently. Anyhow here's the query;

select wip_fg.order_no, sum(wip_fg.trx_qty) as trx_qty, ((sum(wip_fg.trx_qty) / 1000) * orders.ord_price) as ord_$

from wip_fg inner join orders on wip_fg.order_no=orders.order_no inner join spec_file on wip_fg.item_no=spec_file.item_no

where wip_fg.trx_datetime>='2010-1-20 07:00' and wip_fg.trx_datetime<='2010-1-20 15:00'

and spec_file.prod_cd in ('chip', 'chip1', 'chip2') and orders.plt_no=1

group by wip_fg.order_no, orders.ord_price

I am not sure where to place the above. Your help is greatly appreciated!

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.