I have the following records like this in MySQL
RecID| LastModified
1 | 2011-10-29
1 | 2011-11-29
2 | 2011-5-29
3 | 2011-6-28
3 | 2011-8-25
I want the result like this:
RecID| LastModified
1 | 2011-11-29
2 | 2011-5-29
3 | 2011-8-25
How do I do this in MySQL?
Thanks
SELECT RecId, MAX(LastModified) FROM Table GROUP BY RecId
Related
I have a table like the below one
id | id_fk | data |
-------------------------
1 | 2 | data1 |
2 | 2 | data2 |
3 | 1 | data3 |
4 | 3 | data4 |
5 | 1 | data5 |
-------------------------
here I have the table id as 'id', foreign key from another table as id_fk.
What I try to achieve is, to get the count of each foreign key in an increment mode. that is, if the id_fk -> 2 occur on the first time, then the count should be 1, at the next occurance count become 2, and so on for all the id_fk. I tried many ways. But none give me the actual output.
From the above table, the result table will look like:
id_fk | count |
------------------
1 | 1 |
1 | 2 |
2 | 1 |
2 | 2 |
3 | 1 |
------------------
Please help me to solve this.. any help will be appreciated.
Try this
SELECT `id_fk`,
#a:=IF(id_fk=#b,#a+1,1) serial_number,
#b:=id_fk
FROM your_table,(SELECT #a:= 0,#b:=0) AS a
ORDER BY `id_fk` ASC
It works perfect with join.
select t1.id_fk,t1.id,count(*)
from your_table t1
left join your_table t2
on t1.id_fk=t2.id_fk and t1.id>=t2.id
group by t1.id_fk,t1.id
See Sql Fiddle Demo
I have numeric data and I want to show all plus average value. How should I do to make it in MySQL?
Example : (2.5 is average value)
Data
------
1
2
3
4
2.5
You can do it without union
SELECT
AVG(value)
FROM a
GROUP BY id with rollup
Output
| VALUE |
|-------|
| 1 |
| 2 |
| 3 |
| 4 |
| 2.5 |
Fiddle Demo
Try this:
SELECT Data FROM table
UNION ALL
SELECT AVG(Data) AS Data FROM table
I have a post table like following,
| ID | TITLE | NUMBER_OF_REPEAT
| 1 | post1 | 2
| 2 | post2 | 1
| 3 | post3 | 3
From the above table I need a select query to produce the row depending upon NUMBER_OF_REPEAT field.
Expected output,
| ID | TITLE
| 1 | post1
| 1 | post1
| 2 | post2
| 3 | post3
| 3 | post3
| 3 | post3
This kind of duplication should support the pagination also. Please help me.
Thanks in advance.
Something like this
SQL Fiddle
create table numbers (number int);
insert into numbers
select 1
union
select 2
union
select 3;
SELECT id, title
FROM tabl
JOIN Numbers
ON tabl.repeater >= Numbers.number
order by id
Its messy,but modify the numbers table for more repeats.
SQL Fiddle
I don't think this should do by DBMS. this can do in programe.
But you can create another table with only two rows and join with post table
let's say table dup with two rows.
SELECT ID ,TITLEfrom post, dup
I'm trying to do something like 'select groupwise maximum', but I'm looking for groupwise order number.
so with a table like this
briefs
----------
id_brief | id_case | date
1 | 1 | 06/07/2010
2 | 1 | 04/07/2010
3 | 1 | 03/07/2010
4 | 2 | 18/05/2010
5 | 2 | 17/05/2010
6 | 2 | 19/05/2010
I want a result like this
breifs result
----------
id_brief | id_case | dateOrder
1 | 1 | 3
2 | 1 | 2
3 | 1 | 1
4 | 2 | 2
5 | 2 | 1
6 | 2 | 3
I think I want to do something like described here MySQL - Get row number on select, but I don't know how I would reset the variable for each id_case.
This will give you how many records are there with this id_case value and a date less than or equal to this date value.
SELECT t1.id_brief,
t1.id_case,
COUNT(t2.*) AS dateOrder
FROM yourtable AS t1
LEFT JOIN yourtable AS t2 ON t2.id_case = t1.id_case AND t2.date <= t1.date
GROUP BY t1.id_brief
Mysql is permissive about columns which can be queries using GROUP BY. With a more stric DBMS you may need GROUP BY t1.id_brief, t1.id_case.
I strongly advise you to have the right indexes on the table:
CREATE INDEX filter1 ON yourtabl (id_case, date)
I am having troubles with creating up this sql query to find records having more than (n) entries [n=1] in example
I have table
|--id-|--user_id--|
| 1 | 1 |
| 2 | 1 |
| 3 | 1 |
| 4 | 2 |
| 5 | 2 |
| 6 | 3 |
want to retrieve duplicates in my table
|--id-|--user_id--|
| 2 | 1 |
| 3 | 1 |
| 5 | 2 |
any help is very much appreciated, thanks for reading though
UPDATE:
I am using Mysql v5.1
This would be my approach
SELECT ID, USER_ID
FROM TABLE
GROUP USER_ID
HAVING COUNT(1) > 1
MINUS
SELECT MIN(ID) ID, USER_ID
FROM TABLE
GROUP BY USER_ID
EDIT: oops, didn't see that you're using MySQL. you might be able to tweak this query to get it working in MySQL
not sure which version of SQL your using but here's the sqlserver answer:
SELECT * from [table_name] GROUP BY user_id HAVING COUNT(*) > n