If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Select Query

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-24-11, 15:17
abdul_zu abdul_zu is offline
Registered User
 
Join Date: Dec 2002
Location: Nepal
Posts: 32
Select Query

I have one table i need to just select for one month data till the sum of cost filed 5000.

select sum(cost) as R3c, min(id_r), max(id_r) from pro where
dt >= '2010-11-01 00:00:00' and dt <= '2010-11-31 23:59:59'
and sum(cost) <= 5000;

It give error Invalid Group By function.

Please smuggest how i can achieve this.

thank you
Reply With Quote
  #2 (permalink)  
Old 01-24-11, 15:50
it-iss.com it-iss.com is offline
Registered User
 
Join Date: Sep 2009
Location: San Sebastian, Spain
Posts: 623
Have a look at SELECT .. GROUP BY and HAVING. For example

SELECT deptid, SUM(salary)
FROM depts
GROUP BY deptid
HAVING SUM(salary) > 1000;

After re-reading your post I am not sure whether you are looking to SUM(cost) where cost <= 5000?
__________________
Ronan Cashell
Senior Oracle/MySQL DBA
http://www.it-iss.com
Reply With Quote
  #3 (permalink)  
Old 01-24-11, 16:31
abdul_zu abdul_zu is offline
Registered User
 
Join Date: Dec 2002
Location: Nepal
Posts: 32
I am trying very simple query. I need to keep sum(cost) <= 5000 for a month data in one table and delete rest records. Can you please give a simple sql for this.
Reply With Quote
  #4 (permalink)  
Old 01-25-11, 02:58
it-iss.com it-iss.com is offline
Registered User
 
Join Date: Sep 2009
Location: San Sebastian, Spain
Posts: 623
I am not following what you are looking for? Can you provide a small example of what you are looking to select? I think what you are looking for is a runtime aggregator so that once the costs exceed 5000 you stop. Is that what you are looking for?
__________________
Ronan Cashell
Senior Oracle/MySQL DBA
http://www.it-iss.com
Reply With Quote
  #5 (permalink)  
Old 01-27-11, 11:13
abdul_zu abdul_zu is offline
Registered User
 
Join Date: Dec 2002
Location: Nepal
Posts: 32
Hi,
I have one reseller he need to adjust the sum of cost 5000 only. now his account have more than 30,000 he ask us to delete the records so the sum should come only 5000.

select sum(cost) as TotalMonth_cost from tr_table where p_dt >= '2010-09-01 00:00:00' and p_dt <= '2010-09-31 23:59:59'

if i run this the total TotalMonth_cost will be 30,000, Now i need to delete some rows to adjust TotalMonth_cost only 5000.

This is what i am looking.

BR,
Reply With Quote
  #6 (permalink)  
Old 01-27-11, 12:14
it-iss.com it-iss.com is offline
Registered User
 
Join Date: Sep 2009
Location: San Sebastian, Spain
Posts: 623
Try something like this:

mysql> set @total := 0;
Query OK, 0 rows affected (0.00 sec)

mysql> select id, salary, @total:=salary+@total from info;
+----+--------+-----------------------+
| id | salary | @total:=salary+@total |
+----+--------+-----------------------+
| 1 | 1 | 1 |
| 2 | 3 | 4 |
| 3 | 3 | 7 |
| 4 | 8 | 15 |
+----+--------+-----------------------+
4 rows in set (0.00 sec)

mysql> select id, salary, total from (select id, salary, @total:=salary+@total as total from info) a where a.total < 10;
+----+--------+-------+
| id | salary | total |
+----+--------+-------+
| 1 | 1 | 1 |
| 2 | 3 | 4 |
| 3 | 3 | 7 |
+----+--------+-------+
3 rows in set (0.01 sec)
__________________
Ronan Cashell
Senior Oracle/MySQL DBA
http://www.it-iss.com
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 On
Pingbacks are On
Refbacks are On