473,378 Members | 1,456 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

How do I filter/limit the number rows in the query output ?

290 100+
Hi,
I want to get a subset from my table that includes rows that have an item (cb_id) with a unique ip address ( ip_adr). To be in the subset there should be at least two rows and if there are more than four rows then the rows above the forth should be filtered out.

eg. table contains:

EBFORTUNE, IP add: 55.167.889.182
EBFORTUNE, 78.167.109.182
FEDYDE, : 85.106.68.197
FEDYDE, 85.176.68.227
FEDYDE, 89.106.68.127
FHHHH45,78.167.109.182 // only one - not enough - filter out
P97GFRH, 56.107.9.182 // only one - not enough - filter out
RSEDE38M, 78.167.17.182
RSEDE38M, 78.167.109.182
RSEDE38M, 23.167.109.182 // 7 the extra 3 rows must be filtered out
RSEDE38M, 17.167.19.2
RSEDE38M, 712.17.39.182
RSEDE38M, 78.16.143.182
RSEDE38M, 78.19.109.102
GGG72FHH, 178.7.109.18 // only one - not enough - filter out


Then the result I want to get is (min 2, max 4):

EBFORTUNE,55.167.889.182
EBFORTUNE, 78.167.109.182
FEDYDE, 85.106.68.197
FEDYDE,85.176.68.227
FEDYDE, 89.106.68.127
RSEDE38M, 17.167.19.2
RSEDE38M, 712.17.39.182
RSEDE38M, 78.16.143.182
RSEDE38M, 78.19.109.102


I am quite close to this but
I just need to take out the extra entries where the number
of rows exceeds the ceiling.


Expand|Select|Wrap|Line Numbers
  1. $days7ago = $today-604800; // Also data must be within last 7 days.
  2.  
  3. $min_hits = 2;
  4. $ceiling = 4;
  5.  
  6. echo "<br>Min_hits: $min_hits, Ceiling: $ceiling<br>Having sql:<br>";
  7.  
  8. $min_hits = $min_hits-1; // because I use the less than operator.
  9.  
  10. $sql = "SELECT DIC.ip_adr, COUNT(*) AS howmany FROM (SELECT DISTINCT ip_adr, cb_id FROM cb_promo WHERE link_dt BETWEEN $days7ago and $today)
  11.  AS DIC    GROUP BY DIC.cb_id HAVING COUNT(*) > $min_hits";
  12.  
  13. $result = mysql_query($sql)    or die("could not find PROMO". mysql_error());  
  14. while( $row = mysql_fetch_assoc($result)) {
  15.  print_r($row);
  16.  echo "<br>";
Result:

Min_hits: 2, Ceiling: 4
Having sql:
Array ( [ip_adr] => 478.167.109.182 [howmany] => 6 )
Array ( [ip_adr] => 78.14.109.162 [howmany] => 2 )

So just need to get rid of (count-ceiling) rows from the array
but not sure how to do that one.

Any ideas on how I can complete my filtering ?

Either by expanding the query or by processing the resulting array
that I already have.

Thanks
Jan 18 '10 #1
1 2075
dlite922
1,584 Expert 1GB
This would be answered faster if asked in a MySQL forum.

Dan
Jan 18 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Phil Powell | last post by:
http://www.php.net/array_filter I went there at first for my information on filtering mySQL query results using PHP, to no avail. This is more of a Vignette construct (my native environment)...
0
by: krystoffff | last post by:
Hi I would like to paginate the results of a query on several pages. So I use a query with a limit X offset Y to display X results on a page, ok. But for the first page, I need to run the...
3
by: Jay K | last post by:
Hi, I have multiple queries like this: SELECT col1, col2, col3, col4 FROM table1, table2 where table1.col1 = table2.col1 and table1.col2 = 1 ORDER BY col3 desc LIMIT 5 and
4
by: rod | last post by:
Hello SQL gurus! I am trying to write a query that will return a set of continguous rows from a table, and limit the number of rows returned when a maximum total has been reached by adding a...
4
by: emily_g107 | last post by:
Hi, I need to limit results in the following query type: ...
0
by: D. Dante Lorenso | last post by:
I need to know that original number of rows that WOULD have been returned by a SELECT statement if the LIMIT / OFFSET where not present in the statement. Is there a way to get this data from PG ?...
4
by: Chris Tremblay | last post by:
I am trying to figure out how to go about retrieving the number of results returned from my queries in SQL server from VB.NET without using a the Select Count(*) query. The method that I was using...
3
by: Kriston-Vizi Janos | last post by:
Dear Mr. Kern, and Members, Thank you very much for the fast answer, my question became over-simplified. My source code is appended below. It uses two text files (L.txt and GC.txt) as input...
1
by: lorirobn | last post by:
Hi, I have a report that works just fine. Now I would like to add the capability to choose selection criteria to limit what is displayed. I created several reports that do this, but they used...
6
by: jmarcrum | last post by:
Hi! I have created a Union Query in ACCESS 2003, that combines 130 records from one query (Extra Foreman Radios) and 250 records from another query (Forman Main Radios). I have created a...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.