Solved

sum columns in query

Posted on 2010-11-10
9
648 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
  • 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
 

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

This article will describe one method to parse a delimited string into a table of data.   Why would I do that you ask?  Let's say that you need to pass multiple parameters into a stored procedure to search for.  For our sake, we'll say that we wa…
INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now