Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

sum columns in query

Posted on 2010-11-10
9
Medium Priority
?
662 Views
Last Modified: 2012-06-27
this query works

but I want sum of subtotal, total(derived), cost(derived)
select dateordered, company, orderid, billfullname, email, oitemid, cost1, cost2, cost3, productid, ptitle, etitle, iitemid, status, sku, quantity, cost,
case when iitemid = minitemid then debit else 0 end total, case when iitemid=minitemid then subtotal else 0 end subtotal from (


select o.dateordered,o.company,o.orderid,RTRIM ( LTRIM( o.billfirstname + ' ' + o.billlastname ) ) billfullname,o.email,o.itemid as oitemid
,p.cost1,p.cost2,p.cost3,p.productid,p.title as ptitle
,e.title as etitle
,s.status
,i.sku,i.quantity,i.itemid as iitemid
,CASE sku
WHEN p.internalsku1 THEN p.cost1*i.quantity
WHEN p.internalsku2 THEN p.cost2*i.quantity
WHEN p.internalsku3 THEN p.cost3*i.quantity
ELSE 0 END as cost
,(select sum(subtotal) from orderitems where orderid=o.orderid)as subtotal
,(select SUM(debit) from payments where orderid=o.orderid and ( (type='Paypal' and 'confirmation'!='' and resultcode!='Denied' and resultcode!='unconfirmed') or (type!='Paypal') ) )as debit
,(select min(itemid) from orderitems where orderid=o.orderid)as minitemid
from orders o
INNER JOIN orderitems i on i.orderid = o.orderid
INNER JOIN products p on i.productid = p.productid
left join ebaydata e on o.orderid = e.orderid
left join sstatuses s on o.statusid=s.statusid
where (o.dateordered between '11/01/2010' and '11/09/2010')
AND o.orderid In (select orderid from orderitems i,Products P Where i.productid = p.productid and P.title LIKE '%%')
and o.statusid in (10,90,110,260)
and ( (company='company1'and o.itemid>0) or (company='company2'and o.itemid>0) or o.itemid=0 )



) q

Open in new window

0
Comment
Question by:rgb192
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 9

Expert Comment

by:wellhole
ID: 34103514
In the outer select, would it not work if you just slap on a sum() for everything you want to sum up, and add a group by for everything else?
0
 

Author Comment

by:rgb192
ID: 34103765
just adding sum(subtotal)

Msg 8120, Level 16, State 1, Line 1
Column 'q.dateordered' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
select dateordered, company, orderid, billfullname, email, oitemid, cost1, cost2, cost3, productid, ptitle, etitle, iitemid, status, sku, quantity, cost,
case when iitemid = minitemid then debit else 0 end total, case when iitemid=minitemid then subtotal else 0 end subtotal
, sum(subtotal) 
from (


select o.dateordered,o.company,o.orderid,RTRIM ( LTRIM( o.billfirstname + ' ' + o.billlastname ) ) billfullname,o.email,o.itemid as oitemid
,p.cost1,p.cost2,p.cost3,p.productid,p.title as ptitle
,e.title as etitle
,s.status
,i.sku,i.quantity,i.itemid as iitemid
,CASE sku
WHEN p.internalsku1 THEN p.cost1*i.quantity
WHEN p.internalsku2 THEN p.cost2*i.quantity
WHEN p.internalsku3 THEN p.cost3*i.quantity
ELSE 0 END as cost
,(select sum(subtotal) from orderitems where orderid=o.orderid)as subtotal
,(select SUM(debit) from payments where orderid=o.orderid and ( (type='Paypal' and 'confirmation'!='' and resultcode!='Denied' and resultcode!='unconfirmed') or (type!='Paypal') ) )as debit
,(select min(itemid) from orderitems where orderid=o.orderid)as minitemid
from orders o
INNER JOIN orderitems i on i.orderid = o.orderid
INNER JOIN products p on i.productid = p.productid
left join ebaydata e on o.orderid = e.orderid
left join sstatuses s on o.statusid=s.statusid
where (o.dateordered between '11/01/2010' and '11/09/2010')
AND o.orderid In (select orderid from orderitems i,Products P Where i.productid = p.productid and P.title LIKE '%%')
and o.statusid in (10,90,110,260)
and ( (company='company1'and o.itemid>0) or (company='company2'and o.itemid>0) or o.itemid=0 )



) q

Open in new window

0
 
LVL 9

Expert Comment

by:wellhole
ID: 34103864
You only did sum. Where is the group by clause at the bottom? You have to put everything else thats not aggregated in the group by clause.

GROUP BY DATEORDERED, COMPANY, ORDERID, etc.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:rgb192
ID: 34104247
old errors:
invalid column 'total'
error by 'group by'

Msg 8120, Level 16, State 1, Line 3
Column 'q.minitemid' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Msg 8120, Level 16, State 1, Line 3
Column 'q.debit' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.


so I commented subtotal,total,q


current error:

Msg 156, Level 15, State 1, Line 34
Incorrect syntax near the keyword 'group'.
select dateordered, company, orderid, billfullname, email, oitemid, cost1, cost2, cost3, productid, ptitle, etitle, iitemid, status, sku, quantity, cost,
case when iitemid = minitemid then debit else 0 end total, case when iitemid=minitemid then subtotal else 0 end subtotal
--, sum(subtotal) 
from (


select o.dateordered,o.company,o.orderid,RTRIM ( LTRIM( o.billfirstname + ' ' + o.billlastname ) ) billfullname,o.email,o.itemid as oitemid
,p.cost1,p.cost2,p.cost3,p.productid,p.title as ptitle
,e.title as etitle
,s.status
,i.sku,i.quantity,i.itemid as iitemid
,CASE sku
WHEN p.internalsku1 THEN p.cost1*i.quantity
WHEN p.internalsku2 THEN p.cost2*i.quantity
WHEN p.internalsku3 THEN p.cost3*i.quantity
ELSE 0 END as cost
,(select sum(subtotal) from orderitems where orderid=o.orderid)as subtotal
,(select SUM(debit) from payments where orderid=o.orderid and ( (type='Paypal' and 'confirmation'!='' and resultcode!='Denied' and resultcode!='unconfirmed') or (type!='Paypal') ) )as debit
,(select min(itemid) from orderitems where orderid=o.orderid)as minitemid
from orders o
INNER JOIN orderitems i on i.orderid = o.orderid
INNER JOIN products p on i.productid = p.productid
left join ebaydata e on o.orderid = e.orderid
left join sstatuses s on o.statusid=s.statusid
where (o.dateordered between '11/01/2010' and '11/09/2010')
AND o.orderid In (select orderid from orderitems i,Products P Where i.productid = p.productid and P.title LIKE '%%')
and o.statusid in (10,90,110,260)
and ( (company='company1'and o.itemid>0) or (company='company2'and o.itemid>0) or o.itemid=0 )



) 
--q
group by dateordered, company, orderid, billfullname, email, oitemid, cost1, cost2, cost3, productid, ptitle, etitle, iitemid, status, sku, quantity, cost
--,total
, subtotal

Open in new window

0
 
LVL 9

Expert Comment

by:wellhole
ID: 34104486
As the error explains, you can't use something in the select list if its not either aggregated, such as with sum(field), or its not put into the group by clause.

so, put minitemid and debit in the group by clause.
0
 

Accepted Solution

by:
rgb192 earned 0 total points
ID: 34110157
Msg 156, Level 15, State 1, Line 34
Incorrect syntax near the keyword 'group'.
select dateordered, company, orderid, billfullname, email, oitemid, cost1, cost2, cost3, productid, ptitle, etitle, iitemid, status, sku, quantity, cost,
case when iitemid = minitemid then debit else 0 end total, case when iitemid=minitemid then subtotal else 0 end subtotal
--, sum(subtotal) 
from (


select o.dateordered,o.company,o.orderid,RTRIM ( LTRIM( o.billfirstname + ' ' + o.billlastname ) ) billfullname,o.email,o.itemid as oitemid
,p.cost1,p.cost2,p.cost3,p.productid,p.title as ptitle
,e.title as etitle
,s.status
,i.sku,i.quantity,i.itemid as iitemid
,CASE sku
WHEN p.internalsku1 THEN p.cost1*i.quantity
WHEN p.internalsku2 THEN p.cost2*i.quantity
WHEN p.internalsku3 THEN p.cost3*i.quantity
ELSE 0 END as cost
,(select sum(subtotal) from orderitems where orderid=o.orderid)as subtotal
,(select SUM(debit) from payments where orderid=o.orderid and ( (type='Paypal' and 'confirmation'!='' and resultcode!='Denied' and resultcode!='unconfirmed') or (type!='Paypal') ) )as debit
,(select min(itemid) from orderitems where orderid=o.orderid)as minitemid
from orders o
INNER JOIN orderitems i on i.orderid = o.orderid
INNER JOIN products p on i.productid = p.productid
left join ebaydata e on o.orderid = e.orderid
left join sstatuses s on o.statusid=s.statusid
where (o.dateordered between '11/01/2010' and '11/09/2010')
AND o.orderid In (select orderid from orderitems i,Products P Where i.productid = p.productid and P.title LIKE '%%')
and o.statusid in (10,90,110,260)
and ( (company='company1'and o.itemid>0) or (company='company2'and o.itemid>0) or o.itemid=0 )



) 
--q
group by dateordered, company, orderid, billfullname, email, oitemid, cost1, cost2, cost3, productid, ptitle, etitle, iitemid, status, sku, quantity, cost,minitemid,debit
--,total
, subtotal

Open in new window

0
 
LVL 9

Expert Comment

by:wellhole
ID: 34111567
you're going to need to leave the q in there. thats the alias for your temporary table.
0
 

Author Comment

by:rgb192
ID: 34220200
I changed the query to work.  And I think
34104486
and
34103864


led to correct answer

0
 

Author Closing Comment

by:rgb192
ID: 34246465
thanks
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you having speed problem in loading SQL Server Management Studio, try to uncheck these options in your internet browser (IE -> Internet Options / Advanced / Security):    . Check for publisher's certificate revocation    . Check for server ce…
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question