» Quick Links
You can make money on these forums
We Share profits with you
Google
Google Adsense
Google Adwords
YPN
Yahoo
MSN Search
Web Directories
Web Hosting
Web Hosting Offers
Hosting News
Suggestions
Link Building
Domain Names
PHP Forums
MySQL Forums
» More Links
OSP News
Reseller Hosting
Shared Hosting
Dedicated Servers
Google Adsense
Search Engine Marketing
Link Development
Affiliate Marketing


» Advertising
Multiple DC PR Check

Free SEO Tools


Go Back   Webmaster Forums > Website Developement / Programming > MySQL Forums

MySQL Forums Share Your Ideas and Tips about MYSQL. Ask questions about MYSQL. Table Design and Much More...

Reply
 
Thread Tools Display Modes
  #1  
Old 01-01-2007, 05:12 AM
durban durban is offline
Junior Member
 
Join Date: Oct 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
durban is on a distinguished road
Default i need some help guys!

Hello guys,
I have a table with 5 fields: aa, a, b, c and d. I would like to get a query where it will list the results in 3 columns i.e.
Column 1: values of a
Column 2: sum of the values of b where c<2006
Column 3: sum of the values of b where c=2006

I can get the results doing 2 queries:
Query 1: SELECT a,c, SUM(b) FROM table1 GROUP BY a HAVING c<'2006'
Query 2: SELECT a,c, SUM(b) FROM table1 GROUP BY a HAVING c='2006'
but I will have to enter the first query then use a for-loop to get the results of the second query. I'm trying to avoid the for-loop style and do it in just one query.

Example:
table1
------------------------------
aa | a | b | c | d |
------------------------------
1 | 1 | 10 | 2005 | 1 |
------------------------------
2 | 1 | 20 | 2005 | 2 |
------------------------------
3 | 1 | 10 | 2006 | 2 |
----------------- -- This message may have been cut off and the rest will only be shown to members. To become a member, click here --
Reply With Quote
Revenue Sharing Ads ( ?):
  #2  
Old 01-01-2007, 08:16 AM
evy evy is offline
Senior Member
 
Join Date: Oct 2006
Posts: 111
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 16
evy is on a distinguished road
Default

Try this:
SELECT a, SUM(IF(c < 2006, b, 0)) AS `SUM_b_lessthan2006`,
SUM(IF(c = 2006, b, 0)) AS `SUM_b_equal2006`
FROM mytable
GROUP BY a -- This message may have been cut off and the rest will only be shown to members. To become a member, click here --
Reply With Quote
Reply


Thread Tools
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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How fat is your cheque this month? khasmoth AdSense 39 09-03-2008 04:56 AM
i need some help, guys! groven MySQL Forums 0 01-13-2007 04:55 AM
need some help, guys! Krishin Web Hosting Industry 1 12-28-2006 06:34 AM


All times are GMT. The time now is 08:59 PM.