Solved

sum columns in query

Posted on 2010-11-10
9
653 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
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 

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

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

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

There are some very powerful Data Management Views (DMV's) introduced with SQL 2005. The two in particular that we are going to discuss are sys.dm_db_index_usage_stats and sys.dm_db_index_operational_stats.   Recently, I was involved in a discu…
by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

688 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