Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Database > SQL Language
Password Reminder
Register
| FAQ | Members List | 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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old April 19th, 2004, 02:30 AM
Registered User
 
Join Date: Feb 2004
Location: , , .
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default Sum up columns with different criteria

Hi!

Could anybody help me, how to sum up a column two ways in one query. I mean:

Code Date(M.D) Weight
EUT 05.15 0,5
COM 05.16 8
COR 05.21 2
SAR 05.22 1

SAR 06.03 0,5
COM 06.03 6
EUT 06.16 6,5
COR 06.16 1

Now I would like to make a query, in which I can sum ip weights for the whle period and for just the month June.

Code Sum(whole period) Sum(just in June)
EUT 7 0,5
COM 14 6
COR 3 6,5
SAR 1,5 1

Is this at all possible?

Thanks in advance:

pontitt

Reply With Quote
  #2 (permalink)  
Old April 19th, 2004, 03:05 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , United Kingdom.
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

Does this method help http://p2p.wrox.com/topic.asp?TOPIC_ID=12476
Reply With Quote
  #3 (permalink)  
Old April 19th, 2004, 08:46 AM
Registered User
 
Join Date: Feb 2004
Location: , , .
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi pgtip,

I tried the following code in MSQuery:

SELECT `<Table name>`.<Code>, Sum(`<Table name>`.<Weight>) AS 'Whole sum', SUM(CASE WHEN `<Table name>`.<Date(M.D)> >={ts '2004-03-01 00:00:00'} THEN `<Table name>`.<Weight> ELSE 0 END) AS 'Sum just in March'
FROM `\\<pathname>\<filename>`.`<Table name>` `<Table name>`
WHERE (`<Table name>`.<Date(M.D)> >={ts '2004-01-01 00:00:00'})
GROUP BY `<Table name>`.<Code>

But it won't work.

What can be the problem?

pontitt

Reply With Quote
  #4 (permalink)  
Old April 26th, 2004, 01:43 AM
Registered User
 
Join Date: Feb 2004
Location: , , .
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Can this problem be handled in any way?

Reply With Quote
  #5 (permalink)  
Old April 29th, 2004, 06:25 AM
Authorized User
 
Join Date: Apr 2004
Location: , , Ireland.
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Try this it should work

SELECT Code,
          Date(M.D),
          SUM(Weight),
          CASE WHEN Date(M.D) BETWEEN '2004-06-01' AND '2004-06-31'
               THEN SUM(Weight)
               ELSE 0
          END
FROM TableName
GROUP BY Code, Date(M.D)




Reply With Quote
  #6 (permalink)  
Old May 11th, 2004, 02:31 AM
Registered User
 
Join Date: Feb 2004
Location: , , .
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I put an Excel example relating to this problem at the following Excel topic:

http://www.ozgrid.com/forum/viewthre...age=1#pid50913

called <<Summing2.xls>>

Could somebody help, how to make it work?
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Sum two columns and get the result to text chriseebh Classic ASP Databases 1 April 19th, 2008 12:58 PM
sum group values based on cell criteria darkdestroyer Excel VBA 1 January 9th, 2008 09:13 PM
multiplying columns in a rows then getting sum Condor76 XSLT 9 May 10th, 2007 06:01 PM
Lookup max with criteria in multiple columns cc16 Excel VBA 2 September 6th, 2006 05:48 AM
Help: Running Sum (or Cumulative Sum) timdasa VB Databases Basics 1 August 22nd, 2006 03:12 PM



All times are GMT -4. The time now is 05:59 PM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.