I'm making some report, my data look like this :
text date text date
a 18/01/2016 12:01 a 18/01/2016 13:01
b 18/01/2016 12:01 b 18/01/2016 13:01
c 18/01/2016 12:01 c 18/01/2016 13:01
d 18/01/2016 12:01 x 18/01/2016 13:01
e 18/01/2016 12:01 y 18/01/2016 13:01
f 18/01/2016 12:01 z 18/01/2016 13:01
g 18/01/2016 12:01 g 18/01/2016 13:01
h 18/01/2016 12:01 h 18/01/2016 13:01
i 18/01/2016 12:01 i 18/01/2016 13:01
To get the total number of text :
SELECT *, COUNT(*) AS total FROM table
WHERE DAY(date) = DAY('2016-01-18')
AND MONTH(date) = MONTH('2016-01-18')
AND YEAR(date) = YEAR('2016-01-18')
GROUP BY text ORDER BY total DESC
Result :
a 2
b 2
c 2
d 1
e 1
f 1
g 1
h 1
i 1
x 1
y 1
z 1
My problem is, i only want to use top 5 data every hour, and want a result like this :
data :
a 18/01/2016 12:01 a 18/01/2016 13:01
b 18/01/2016 12:01 b 18/01/2016 13:01
c 18/01/2016 12:01 c 18/01/2016 13:01
d 18/01/2016 12:01 x 18/01/2016 13:01
e 18/01/2016 12:01 y 18/01/2016 13:01
result :
a 2
b 2
c 2
d 1
e 1
x 1
y 1
The query shown below will give the result required,
SELECT text, max(date) as maxdate, COUNT(*) AS total
FROM table
WHERE
DAY(date) = DAY('2016-01-18')
AND MONTH(date) = MONTH('2016-01-18')
AND YEAR(date) = YEAR('2016-01-18')
GROUP BY text ORDER BY maxdate DESC
I hope this helps.
That is simple using Mysql Limit, you can use "LIMIT 5", in your case your code will be like
SELECT *, COUNT(*) AS total FROM table
WHERE DAY(date) = DAY('2016-01-18')
AND MONTH(date) = MONTH('2016-01-18')
AND YEAR(date) = YEAR('2016-01-18')
GROUP BY text ORDER BY total DESC LIMIT 5
it should return only top 5 results.
Related
good evening,
i have a table:
A B C
45 1 1
22 2 1
40 3 1
43 1 2
21 2 2
61 3 2
49 4 2
60 5 2
76 1 3
41 2 3
57 3 3
i find max(A) from max(B) group by C. The result should be 60 - max number in A from last row in B from each group (C)
Thank you for your help
If i understand correctly your question you could use an inner join on select max(b):
select max(A)
from my_table m
inner join (
select C,
max(B) act_B
from my_table
group by C
) t on t.act_B = m.B and t.c = m.c
I have two table. one with 50 records in table A and other with 100 in table B.
Both table have customer id and date on it.
Both table have duplication but there are less number of unique customer in table with 50.
Now i wish to put date of customer in table B in front of customer in table A. a customer can appear more than 2 time in table B.
So number of time it appear, number of column appended with date in front of the customer.
Id Date
1 1/01/2016
2 2/01/2016
2 3/01/2016
3 1/01/2016
4 4/01/2016
5 5/01/2016
6 1/01/2016
7 3/01/2016
8 4/01/2016
9 1/01/2016
1 6/01/2016
10 1/01/2016
For eg. this is table A
and Table B is
Id Date
1 1/01/2016
2 2/01/2016
2 3/01/2016
3 1/01/2016
4 4/01/2016
5 5/01/2016
6 1/01/2016
7 3/01/2016
8 4/01/2016
9 1/01/2016
1 6/01/2016
10 1/01/2016
1 15/01/2016
2 16/01/2016
2 17/01/2016
3 18/01/2016
4 19/01/2016
5 20/01/2016
6 21/01/2016
7 16/01/2016
8 20/01/2016
9 21/01/2016
1 16/01/2016
10 18/01/2016
1 19/01/2016
2 2/02/2016
2 30/01/2016
3 16/01/2016
4 31/01/2016
5 21/01/2016
6 18/01/2016
7 19/01/2016
8 16/01/2016
9 2/02/2016
1 18/01/2016
10 19/01/2016
I wish to have output given under
Id Date Date1 Date2
1 1/01/2016 15/01/2016 19/01/2016
2 2/01/2016 16/01/2016 2/02/2016
2 3/01/2016 17/01/2016 30/01/2016
3 1/01/2016 18/01/2016 16/01/2016
4 4/01/2016 19/01/2016 31/01/2016
5 5/01/2016 20/01/2016 21/01/2016
6 1/01/2016 21/01/2016 18/01/2016
7 3/01/2016 16/01/2016 19/01/2016
8 4/01/2016 20/01/2016 16/01/2016
9 1/01/2016 21/01/2016 2/02/2016
1 6/01/2016 16/01/2016 18/01/2016
10 1/01/2016 18/01/2016 19/01/2016
Any help is much much appreciable. Thanks
This should explain how to use joins. Once you learn about that you can write SQL yourself.
http://www.w3schools.com/sql/sql_join.asp
try understanding LEFT and Inner Joins
try this (not easy)
with tmp1 as (
select row_number() over(order by (select null) ) - 1 newid , (row_number() over(order by (select null)) - 1) %12 rg1 , f1.*
from table2 f1
),
tableranked as (
select DENSE_RANK() over(partition by rg1 order by newid) rg2, f2.*
from tmp1 f2
)
select f1.id, f1.date, f4.date1, f4.date2
from tableranked f1 inner join (select f2.id, f2.date as date1, f3.date as date2 , f2.rg1
from tableranked f2
inner join tableranked f3 on f2.id=f3.id and f2.rg2+1=f3.rg2 and f2.rg1=f3.rg1
where f2.newid>=12 and f3.newid>=12
) f4 on f1.id=f4.id and f1.rg1=f4.rg1
where f1.newid<12
I want to order by type, by a pattern. Records now:
type name
1 a
2 b
1 c
4 d
4 e
3 f
2 g
3 h
my pattern is 2,4,3,1 so I would like to get:
2 b
2 g
4 d
4 e
3 f
3 h
1 a
1 c
use order by field (type,2,4,3,1). This will give you the correct result.
I have a table like this:
T A B C ID
2015-07-19 a b c 1
2015-07-16 a y z 2
2015-07-21 a b c 1
2015-07-17 a y c 2
2015-07-18 a y c 1
2015-07-20 a b c 1
2015-07-17 a y c 1
2015-07-19 a b c 2
2015-07-16 a y z 1
2015-07-20 a b c 2
2015-07-15 a y z 1
2015-07-22 x b c 1
2015-07-21 a b c 2
2015-07-18 a y c 2
2015-07-15 a y z 2
2015-07-22 a y c 2
2015-07-14 x b c 1
I need to get an ordered result by datetime column T, but I need that the query detects and avoid repeated rows in columns A, B and C. And all this ordered and separated by ID.
It could be a stored procedure. It's important to be fast, because is a huge log table. With millions of rows.
The result should be like this:
T A B C ID
2015-07-22 x b c 1
2015-07-19 a b c 1
2015-07-17 a y c 1
2015-07-15 a y z 1
2015-07-14 x b c 1
2015-07-22 a y c 2
2015-07-19 a b c 2
2015-07-17 a y c 2
2015-07-15 a y z 2
Any ideas?
This query gives the expected result (tested):
SELECT t1.* FROM mytable t1
LEFT JOIN mytable t2 ON t1.t = t2.t + INTERVAL 1 DAY AND t1.A = t2.A AND t1.B = t2.B AND t1.C = t2.C AND t1.ID = t2.ID
WHERE t2.T IS NULL
ORDER BY t1.ID, t1.T DESC
Try this query:
SELECT max(t),a,b,c,id FROM table GROUP BY A,B,C,id ORDER BY ID, max(T)
Try this query:
SELECT * FROM table GROUP BY A,B,C,T ORDER BY ID, T
Edit: added T to group by
I have the following tables:
TABLE1:
A B C
1 2 3
2 4 6
3 6 9
TABLE2:
A B C
4 8 12
5 10 15
6 12 18
TABLE3:
A D
2 X
4 Y
6 Z
I need one query that gives:
A B C D
1 2 3
2 4 6 X
3 6 9
4 8 12 Y
5 10 15
6 12 18 Z
Is that possible?
I can do it in 2 queries, but the person I'm doing it for wants it in 1.
Thanks!
Try this (example on sqlfiddle):
SELECT x.a, x.b, x.c, d
FROM (
SELECT a, b, c FROM table1
UNION ALL
SELECT a, b, c FROM table2
) x
LEFT JOIN table3 ON ( table3.a = x.a )
Sure:
select v1.*, table3.d
from
(select table1.a, table1.b, table1.c
from table1
union all
select table2.a, table2.b, table2.c
from table2
) v1
left join table3 on v1.a = table3.a