I have two tables with timestamp columns.
Table #1 contains clicks, timestamp and Table #2 contains userid, timestamp. I want the counts of clicks and users by date. for example
Date clicks_count users_count
2015-07-24 10 15
2015-07-24 04 06
I think these SQL useful to you.
select a.date1,clicks_count,users_count from
(select date(Table1.timestamp)as date1, count(clicks) as clicks_count
from Table1
group by date(Table1.timestamp)) as a
join
(
select date(Table2.timestamp) date2, count(userid) as users_count
from Table2
group by date(Table2.timestamp)) b on a.date1 = b.date2
Thank you.
select date(timestamp),
sum(is_click) as clicks,
sum(is_click = 0) as user_count
from
(
select timestamp, 1 as is_click from table1
union all
select timestamp, 0 from table2
) tmp
group by date(timestamp)
You can select the timestamps from both tables together and add a calculated column that indicates from which table the timestamp came from.
Then you take that subquery result and group by by the date and count the users and clicks.
sum(is_click = 0) counts how many time the timestamp came from the users table.
Related
I've got a database which has the table updates which contains: product_name, environment, version, date, task.
I need to select only the newest of all and all at one date in time.
Something following will give you all records from max date of each product
select * from table allDates JOIN
( select max(date) maxdate, product from table group by product) maxDates
allDates.Date= maxDates.maxdate and allDates.product= maxDates.product
for all products of latest date
select * from table where date =
( select max(date) maxdate from table)
the newest of all
Anil had a good idea with
SELECT * FROM table ORDER BY date DESC LIMIT 1;
all at one date in time
SELECT * FROM table WHERE DATE(date) = '2017-11-06'
or if your date contains more than just year, month and day
SELECT * FROM table WHERE date LIKE '2017-11-06%'
I have a column named log which contains logging information. I need a query to select all rows in which the value today appears two times or three times in my log column.
id log
1 today, yesterday, today, tomorrow, today
2 now, today, now
3 now, today, today
Select id from table if `today` appears three times in log column
The id:1 will be selected
This will do your job:
select * from table where ROUND (
(
LENGTH(log)
- LENGTH( REPLACE ( log, "today", "") )
) / LENGTH("today")
) >=3
Try this :
select * from yourtable a
inner join (
select max(id) as id,log,
sum(case when log = 'today' then 1 else 0 end) as c
from yourtable
group by log
) b on b.id = a.id and b.cnt = 3
Use the GROUP BY clause with HAVING
SELECT log
FROM tbl
GROUP BY log
HAVING COUNT(*) >= 2
In my table, i have the following columns :
CRMID | user | ticket_id | | description | date | hour
what i am trying to do is to select all the rows from the table, but when two (or more) rows have the same ticket_id, i want only the newest one to appear in the results, so the row with the newest date and hour.
the problem here is that i should be addin cases, if the values from the date column are the same, then i will compare the hour colum, otherwise, its simple cauz i'll be comparing only the date column.
SELECT
n.*
FROM
table n RIGHT JOIN (
SELECT
MAX(date) AS max_date,
(SELECT MAX(hour) AS hour WHERE date = max_date) AS hour,
user,
ticket_id
FROM
table
GROUP BY
user,
ticket_id
) m ON n.user = m.user AND n.ticket_id = m.ticket_id
You may want to combine your date and hour columns, then perform the comparison
SELECT foo.*
FROM foo
JOIN (SELECT ticket_id, MAX(ADDTIME(`date`,`hour`)) as mostrecent
FROM foo
GROUP BY ticket_id) AS bar
ON bar.ticket_id = foo.ticket_id
and bar.mostrecent = ADDTIME(foo.`date`,foo.`hour`);
SELECT AnotherID, TID, TDate, COUNT(*) AS Total
FROM MyTable
GROUP BY TID
HAVING Total > 1
The above query returns the records where multiple records have same TID. Now I need to select the latest date.
Above query returns TDate but its not the most recent one, basically I need to group by TID and get those records where TDate is most recent. I only need to get those records where multiple records have same TID.
NOTE: Cant use MAX(TDate) because it does not select the proper AnotherID field
Assuming TDate is of data type date or datetime, then MAX(TDate) wil give you the latest date for each grouped TID:
SELECT t1.AnotherId, t1.TID, t2.MaxTDate, t2.Total
FROM MyTable AS t1
INNER JOIN
(
SELECT TID, MAX(TDate) AS MaxTDate, COUNT(*) AS Total
FROM MyTable
GROUP BY TID
HAVING Total > 1
) AS t2 ON t1.TID = t2.TID
AND t1.TDate = t2.MaxTDate;
I have "users" table with fields
user_name, user_id
I have data tables like
data_table_2012_10
data_table_2012_11
data_table_2012_12
data_table_2013_01
data_table_2013_02
each table contains the following fields
user_id, type ('ALARM', 'EMERGENCY', 'ALIVE', 'DEAD'), date_time
There will be millions of records in each table.
I have to select the count of type from the data_tables within the time frame given by the user, as well as have to get the corresponding name of the user with the help of user_id.
Can some one help me out with the best solution.
Try this query where DATE1 and DATE2 is your date range. You should union all tables in the inner query. Also you can try to make a query dynamically to include in the inner query only those tables that are in a date range you use:
select t.user_id,t.type, MAX(users.user_name), SUM(t.cnt)
from
(
select user_id,type,count(*) cnt
from data_table_2012_10 where date_time between DATE1 and DATE2
group by user_id,type
union all
select user_id,type,count(*) cnt
from data_table_2012_11 where date_time between DATE1 and DATE2
group by user_id,type
union all
.........................................
union all
select user_id,type,count(*) cnt
from data_table_2013_02 where date_time between DATE1 and DATE2
group by user_id,type
) t
left join users on (t.user_id=users.user_id)
group by t.user_id,t.type
Remember not to use UNION, but UNION ALL as UNION will return only merge similar rows into one and that may cause problem