how to find the most repeated values in sql - php

In my table i have something like
id file_id
1 1
1 2
1 4
2 1
2 4
2 6
I need to find the most frequent file_id regardless of the id, so for example i need the output something like
file_id count
1 2
4 2
.. ..
Im using php and phpmyadmin

Use this query
select
file_id,
count(*) as count
from table_name
group by file_id;

select file_id, count(*) as count
from files
group by file_id

SELECT file_id, count(id)
FROM f
GROUP BY file_id
ORDER BY count(file_id) DESC;

In standard SQL, you can do:
select f.file_id, cnt
from (select file_id, count(*) as cnt,
rank() over (order by count(*) desc) as seqnum
from t
group by file_id, id
) f
where seqnum = 1;

Simple SQL in your case:
select file_id, count(id)
from ur_table
group by file_id;

Using aggregate function
SELECT count(id) AS count FROM table GROUP BY file_id

Related

SQL: How can I find repeated value's till separation

What I want to do with SQL is find repeating values in a column until there's an other value sorted by date.
This is my table. I use "CURRENT_TIMESTAMP" for the date column.
Name |date
-------------------
Bart |12-12-2014
Bart |23-12-2014
Joost |24-12-2014
Bart |25-12-2014
Bart |26-12-2014
Bart |27-12-2014
So in this example I want the number "3" returned and the last known value of name, so in this case "Bart".
I hope I made myself clear, sorry for the unclear title!
Hmmm . . . Here is one method where the logic is placed in the where clause:
select count(*), max(lastname.name)
from tablename t cross join
(select t2.name from tablename t2 order by date desc limit 1) as lastname
where t.name = lastname.name and
t.date > (select max(t2.date) from tablename t2 where t2.name <> lastname.name);
You can use the following query:
SELECT Name, COUNT(*) AS cnt
FROM (
SELECT Name, [date],
ROW_NUMBER() OVER (ORDER BY [date]) -
ROW_NUMBER() OVER (PARTITION BY Name ORDER BY [date]) AS grp
FROM mytable ) AS t
GROUP BY Name, grp
ORDER BY COUNT(*) DESC
This query tries to identify islands of records, i.e. consecutive rows having the same Name: grp calculated field does exactly this.
If you want the Name having the largest number of consecutive records, just use TOP 1 in the above query.
Demo here

php mysql Join 2 tables without common values

I have 2 tabes with the following fields
Table1::
Id, Name, startDate, File, Current test
Data set:
1 nm1-tbl1 25-10-2013 file1 yes 1
1 nm2-tbl1 27-10-2013 file2 yes 1
Table2::
Id, Name, startDate, File, Enddate
Data
1 nm1-tbl2 24-10-2013 file1 11-11-2014
1 nm2-tbl3 26-10-2013 file2 11-11-2014
I need the out put as
1 nm1-tbl2 24-10-2013 file1
1 nm1-tbl1 25-10-2013 file1
1 nm2-tbl3 26-10-2013 file2
1 nm2-tbl1 27-10-2013 file2
Both tables have no common values. But I need to combile these 2 tables in order by the ASC OR DESC
select a.*, b.*
from table1 as a, table2 as b
where a.File <> '' AND b.File <> '' AND a.startDate <> '0000-00-00'
AND b.startDate <> '0000-00-00' order by a.startDate ASC, b.startDate ASC
But it is not working as expected. It first orders the table1 and then table2. But I need as combination of 2. How to achieve this. Please help me.
(
select
Id,
Name,
startDate,
File
from
table1
)
union
(
select
Id,
Name,
startDate,
File
from
table2
)
order by
startDate DESC;
I think you need to use UNION here:
(SELECT * FROM Table1)
UNION
(SELECT * FROM Table2)
ORDER BY startDate DESC;
UNION is used to combine the result from multiple SELECT statements into a single result set.
http://dev.mysql.com/doc/refman/5.0/en/union.html
Use union query to get this result
SELECT Id,Name,startDate,File
FROM table1
UNION
SELECT Id,Name,startDate,File
FROM table2
ORDER BY startDate ASC

How to show rank using mysql query

user_id | date | point
1 20 4
2 20 3
3 20 2
1 21 1
2 21 3
3 21 5
1 23 2
2 23 4
3 23 5
And query:
SELECT user_id, SUM(point) AS point, #row:=#row+1 rank FROM users GROUP BY user_id
How to show rank in this query ?
first you need to delcare #row as variable first
Something like set #row=0; select user_id,SUM(point) as point,#row := #row + 1 as rank from users GROUP BY user_id order by SUM(point) desc;
try this
SET #row=0;
SELECT user_id, SUM(point) AS point, #row:=#row+1 rank FROM users GROUP BY user_id
You can work around the declaring part using this way:
SELECT user_id, SUM(point) AS point, #row:=#row+1 rank FROM users, (SELECT #row := 0) r GROUP BY user_id
I guess you've to write it like:
SELECT user_id, SUM(point) AS point, #row:=#row+1 AS rank FROM users GROUP BY user_id JOIN (SELECT #row := 0) r;
Try this..
SELECT user_id,points, #row:=#row+1 AS Row
from (
SELECT user_id,
SUM(point) AS points rank
FROM users
GROUP BY user_id
Sort by points);
Should have an AS to alias #row to rank, and an order by on point to get them in the right order to rank:-
SELECT user_id, SUM(point) AS point, #row:=#row+1 AS rank
FROM users
CROSS JOIN (SELECT #row:=0) Sub1
GROUP BY user_id
ORDER BY point DESC

mysql get duplicate rows using where

pid fid
6 19
7 19
8 19
9 19
7 30
6 30
I have a table like this. I want to select duplicate rows,
when I'm sending 19, 30 ids Using an IN clause like:
Select pid from tablename where fid IN (19,30);
I want these results
pid
7
6
Is there any mysql statement to get these results?
SELECT pid
FROM tableName
WHERE fid IN (19,30)
GROUP BY pid
HAVING COUNT(*) = 2
if unique constraint was not defined on fid for each pid, then you need to have DISTINCT inside COUNT
SELECT pid
FROM tableName
WHERE fid IN (19,30)
GROUP BY pid
HAVING COUNT(DISTINCT fid) = 2
SQLFiddle Demo
Use distinct
Select distinct pid from tablename where fid IN (19,30);
or to find the duplicates
select pid from tablename group by fid having count(*)>1
SELECT
pid
FROM tablename
where fid IN (19,30)
group by
pid
First, you should group pid's and count them, and then take the ones with more than one occurence
SELECT pid, count(*) AS cnt
FROM tablename
WHERE fid IN (19,30)
GROUP BY pid
HAVING cnt > 1

Select latest record if column has 2 of the same

I have table
**id name status date**
1 john 2 01.01.2010
2 mike 5 04.01.2010
3 john 2 06.01.2010
4 sam 1 08.01.2010
john has status 2 twice and i need to select john,mike from this table where status = 2 but i need to show latest record.
I cannot use order by i use it already for something else.
You can use order by for multiple criteria like this:
ORDER BY date desc, status desc
You need to use a correlated subquery such as this:
select *
from table t1
where t1.date = ( select max( t2.date )
from table t2
where t1.name = t2.name
and t1.status = t2.status )
The query would go much faster if you didn't need the ID field:
SELECT t.name, t.status, max(t.date) date
FROM table t
GROUP BY t.name, t.status
ORDER BY [whatever]
If you DID need id, AND the ID is guarenteed to be larger on the record with the newer date, you could just add max(t.id) id to the field list.
SELECT *
FROM table t
WHERE status = 2
AND date = (SELECT MAX(date) FROM table tmp WHERE tmp.name = t.name GROUP BY name)

Categories