Shantanu's Blog

Database Consultant

April 30, 2008

 

MySQL Case Study - 170

Trying to keep this simple,
I have many pickers and I am looking to find:
How many apples, how many oranges picked today and total of all fruit so far for each picker..

[picker] [apples] [oranges [date]
a 0 50 20080102
b 0 75 20080102
c 0 30 20080102
a 20 0 20080103
b 50 0 20080103
c 10 0 20080103
a 0 95 20080104
b 100 0 20080104
c 0 85 20080104

The results I am trying to get are something like what follows:
[picker] [apples] [oranges] [date] [total fruit]
a 0 95 20080104 165
b 100 0 20080104 225
c 0 85 20080104 125

I can can easily get the most current rows for each picker
select picker, apples, oranges from work where date='20080104',
I can easily get the total fruit for each picker
select picker, (sum(apples) + sum(oranges)) from work group by picker,
I just cannot seem to cram it all in one sql query.

I have looked at 'WITH ROLLUP' but it does not seem to do what I need to do.

http://forums.mysql.com/read.php?10,207297,207297#msg-207297

Labels:


 

MySQL Case Study - 169

Order By problem

Here is a sample set of data (ignore the dashes - - - - - - between columns):

topic_id - - - message_time

1 - - - - - - 2008-04-12 16:27:56
1 - - - - - - 2008-04-12 16:27:56
1 - - - - - - 2008-04-12 16:27:56
2 - - - - - - 2008-04-13 16:27:56
1 - - - - - - 2008-04-26 17:27:56
2 - - - - - - 2008-04-26 18:27:56
2 - - - - - - 2008-04-26 19:27:56
2 - - - - - - 2008-04-26 20:27:56

What I would like have happen is for the topics that have the most entries within the last seven days to be returned first. If there is a tie, it should go to the one with the most recent entry.

Below is the query I started with, but it doesn't seem to even be getting the first part of what I want. Any help would be greatly appreciated.

SELECT DISTINCT(topic_id) AS topic_id
FROM messages
ORDER BY COUNT(message_time) >= NOW()
LIMIT 0,25;

http://forums.mysql.com/read.php?10,207167,207167#msg-207167

Labels:


April 18, 2008

 

MySQL Case Study - 168

select rows with max value?
Given a table like this:

CREATE TABLE IF NOT EXISTS `people` (
`name` varchar(10) NOT NULL,
`age` smallint(5) unsigned NOT NULL,
PRIMARY KEY (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

INSERT INTO `people` (`name`, `age`) VALUES ('bob', 13), ('john', 25), ('mike', 25), ('steve', 8), ('sue', 13);

How would I select only the rows with the highest 'age' value?
In other words, because "john" and "mike" are the oldest, I want only their rows.

http://forums.mysql.com/read.php?10,199598,199598#msg-199598

Labels:


April 14, 2008

 

MySQL Case Study - 167

How to get the oldest N rows per user with the totol row count bigger than some threshold?

I have a log table which record user's activity.

Simple table design as follows.

CREATE TABLE log (
id int unsigned not null auto_increment,
user_id int unsigned not null,
date int unsigned not null,
...,
PRIMARY (id)
KEY (user_id, date)
) engine=innodb


I want to get the oldest N(for example count(id) - 2) rows per user whose total record count exceeds certain limit (for example 2). The N per user may vary according to the total record count per each user.

For example, the rows of table log may be as follows.
id, user_id, date,
1 1000 20071102
2 1001 20071103
3 1000 20071104
4 1000 20071101
5 1000 20071108
6 1000 20071109
7 1001 20071102
8 1001 20071109
9 1002 20071107
10 1001 20071114

I want to get the result set like following.

4 1000 20071101
1 1000 20071102
3 1000 20071104
7 1001 20071102
2 1001 20071103

normal gourp by clause will return only one row per each group.
So it is hard to do this kind of query.


select id, user_id, regdate from log as l inner join (select user_id, .. FROM log GROUP BY user_id HAVING COUNT(id) as cnt > 2) as s on (l.user_id = s.user_id) ) order by user_id, regdate limit cnt-2

This join does not achieve my result set, because limit cannot limit result set per user_id.

Is there any efficient way to achieve my purpose?

http://forums.mysql.com/read.php?24,193764,193764#msg-193764

Labels:


Archives

June 2001   July 2001   January 2003   May 2003   September 2003   October 2003   December 2003   January 2004   February 2004   March 2004   April 2004   May 2004   June 2004   July 2004   August 2004   September 2004   October 2004   November 2004   December 2004   January 2005   February 2005   March 2005   April 2005   May 2005   June 2005   July 2005   August 2005   September 2005   October 2005   November 2005   December 2005   January 2006   February 2006   March 2006   April 2006   May 2006   June 2006   July 2006   August 2006   September 2006   October 2006   November 2006   December 2006   January 2007   February 2007   March 2007   April 2007   June 2007   July 2007   August 2007   September 2007   October 2007   November 2007   December 2007   January 2008   February 2008   March 2008   April 2008   July 2008   August 2008   September 2008   October 2008   November 2008   December 2008   January 2009   February 2009   March 2009   April 2009   May 2009   June 2009   July 2009   August 2009   September 2009   October 2009   November 2009   December 2009   January 2010   February 2010   March 2010   April 2010   May 2010   June 2010   July 2010   August 2010   September 2010   October 2010   November 2010   December 2010   January 2011   February 2011   March 2011   April 2011   May 2011   June 2011   July 2011   August 2011   September 2011   October 2011   November 2011   December 2011   January 2012   February 2012   March 2012   April 2012   May 2012   June 2012   July 2012   August 2012   October 2012   November 2012   December 2012   January 2013   February 2013   March 2013   April 2013   May 2013   June 2013   July 2013   September 2013   October 2013   January 2014   March 2014   April 2014   May 2014   July 2014   August 2014   September 2014   October 2014   November 2014   December 2014   January 2015   February 2015   March 2015   April 2015   May 2015   June 2015   July 2015   August 2015   September 2015   January 2016   February 2016   March 2016   April 2016   May 2016   June 2016   July 2016   August 2016   September 2016   October 2016   November 2016   December 2016   January 2017   February 2017   April 2017   May 2017   June 2017   July 2017   August 2017   September 2017   October 2017   November 2017   December 2017   February 2018   March 2018   April 2018   May 2018   June 2018   July 2018   August 2018   September 2018   October 2018   November 2018   December 2018   January 2019   February 2019   March 2019   April 2019   May 2019   July 2019   August 2019   September 2019   October 2019   November 2019   December 2019   January 2020   February 2020   March 2020   April 2020   May 2020   July 2020   August 2020   September 2020   October 2020   December 2020   January 2021   April 2021   May 2021   July 2021   September 2021   March 2022   October 2022   November 2022   March 2023   April 2023   July 2023   September 2023   October 2023   November 2023  

This page is powered by Blogger. Isn't yours?