Find a column with one value for 2 different dates - mysql

I have the following table:
table 1
columns are as following:
date , time , user_id , channel
I wish to find for a list of USERS watching in 2 different DATES , channel(lets say CNN, NBC...) all the entries relevant.
Means the channel in DATE 1 and DATE 2 is the same, also the user_id.
I try allready following:
select distinct monthname(date),max(date), min(date) count(distinct user_id)
from iptv_clean
group by monthname(date)
having min(date)!= max(date)
But it seems not to work well.
Any ideas?

The following gives a list of users and channels that users watched on more than one date:
select user_id, channel, count(distinct date)
from iptv_clean
group by user_id, channel
having count(distinct date) > 1;
Is this what you want?

Related

Who to the number of users who have had one transaction per day?

Here is my query:
select count(1) from
(select count(1) num, user_id from pos_transactions pt
where date(created_at) <= '2020-6-21'
group by user_id
having num = 1) x
It gives me the number of users who have had 1 transaction until 2020-6-21. Now I want to group it also per date(created_at). I mean, I want to get a list of dates (such as 2020-6-21, 2020-6-22 etc ..) plus the number of users who have had 1 transaction in that date (day).
Any idea how can I do that?
EDIT: The result of query above is correct, the issue is, it's manually now. I mean, I have to increase 2020-6-21 by hand. I want to make it automatically. In other words, I want a list of all dates (from 2020-6-21 til now) contains the number of users who have had 1 transaction until that date.
If you want the number of users who had one transaction on each day, then you need to aggregate by the date as well:
select dte, count(*)
from (select date(created_at) as dte, user_id
from pos_transactions pt
where date(created_at) <= '2020-6-21'
group by dte, user_id
having count(*) = 1
) du
group by dte;

MySQL select only one record from similar per day

I have table with users actions. One of them occurs every time when user opens certain page.
Table structure:
id, user_id, action_type, created_at...
I need to select from this table all actions per day/week... but without repeating of similar in one day. For example: user has visited 10 pages but 5 of them was the same. The result of selection should contain only unique pages per day.
Is it possible to do with only MySQL logic? Or better I should update repeated action if it occurs the same day?
One approach uses select distinct:
select distinct user_id, action_type, date(created_at) created_date
from mytable
If needed, you can also count how many times each action_type was met on a user_id and day basis with aggregation:
select user_id, action_type, date(created_at) created_date, count(*) cnt
from mytable
group by user_id, action_type, date(created_at)
I suggest the following SQL code :
SELECT DISTINCT URL
FROM table_name
GROUP BY date;
I assume that your table name is table_name, you have the URLs (pages) in the column named URL and you you track the date in the column named date;

MYSQL COUNT WITH 3 DISTINCT

Hi I have this VISITS table
What I want to achieve:
**affiliate_id** **unique visits count**
167 4
121 1
137 1
Special Condition is one IP can only be counted once per day for single affiliate_id.
So for visit_id 553 and 554, it can be only counted as one visits because both have same ip, same date and same affiliate_id.
From what I understand I need to group by ip, date and affiliate_id and count it, but not sure how to write the query.
Can you guys point me to some reference or insight to solve this problem?
Thanks in advance!
--
Update with link sample SQL:
https://dl.dropboxusercontent.com/u/3765168/tb_visits.sql
Based on your requirement i think you need the distinct ip per date and affiliate_id
select DATE(date), affiliate_id, count(distinct( ip))
from your_table
group by DATE(date), affiliate_id
If I understood correctly,
SELECT affiliate_id, count(*)
FROM (SELECT DISTINCT affiliate_id, ip, DAY(date)
FROM visits) AS q
GROUP BY affiliate_id;
What you are trying to do is group the number of unique or distinct ip's for a given affiliate_id so the only group by you need is the affiliate_id. The Unique hits are calculated using a count and to make then unique you add the DISTINCT key word
SELECT
affiliate_id, COUNT(DISTINCT ip) AS unique_visit_counts,
FROM tablename
GROUP BY affiliate_id
However since you want it by the day as well you might want to include a date clause such as:
DATE_FORMAT(date, "%y-%m-%d") AS `date`
Which will turn your date and time stamp into a day in the YY-MM-DD format.
If you group by that you can get a full list by day by affiliate_id using something like
SELECT
affiliate_id,
COUNT(DISTINCT ip) AS unique_visit_counts,
DATE_FORMAT(date, "%y-%m-%d") AS `date`
FROM tablename
GROUP BY `date`, affiliate_id
Or pick a specific date using something like
SELECT
affiliate_id,
COUNT(DISTINCT ip) AS unique_visit_counts,
FROM tablename
WHERE DATE_FORMAT(date, "%y-%m-%d") = '17-02-08'
GROUP BY affiliate_id

How to write a query to return daily returning users?

I have a very simple table phone_calls with these columns:
id, timestamp, phone_number
How to write a query which returns a daily count of returning users? It should have two columns:
Date, Number of Returning Users
Returning user:
A returning user for any given day D is the one, who has called at least once before D.
A user who has called multiple times on D, but hasn't called before D won't be counted as a returning user.
UPDATE
So here is what I have tried:
SELECT DATE(timestamp) AS date, COUNT(DISTINCT phone_number) AS user_count
FROM phone_calls
WHERE phone_number IN (SELECT phone_number FROM phone_calls GROUP BY phone_number HAVING COUNT(consumer_id) > 1)
GROUP BY DATE(timestamp)
But it's not a correct solution, because it doesn't comply with definition of Returning User mentioned above.
What I am struggling with?
For any given date, how do I filter out those phone numbers from the count, who never dialed in before that day?
SELECT
DATE(timestamp) AS date,
COUNT(DISTINCT phone_number) AS user_count
FROM
phone_calls pc
WHERE EXISTS (
SELECT *
FROM phone_calls pc1
WHERE
pc1.phone_number = pc.phone_number AND
DATE(pc1.timestamp) < DATE(pc.timestamp)
)
GROUP BY DATE(pc.timestamp)
Updated try this query
Select date(pc.timestamp) AS RDate ,count(*)
from phone_calls pc INNER JOIN phone_calls pcc
on pcc.phone_number=pc.phone_number
and date(DATE_ADD(pcc.timestamp, INTERVAL -1 DAY))= DATE (pc.timestamp) group by DATE (pc.timestamp);

MySql SUM and JOIN

I am trying to count sales made by a list of sales agents, this count is made every few minutes and updates a screen showing a 'sales leader board' which is updates using a Ajax call in the background.
I have one table which is created and populated every night containing the agent_id and the total sales for the week and month. I create a second, temporary table, on the fly which counts the sales for the day.
I need to combine the two tables to create a current list of sales for all agents in agent_count.
Table agent_count;
agent_id (varchar),
team_id (varchar),
name (varchar),
day(int),
week(int),
month(int)
Table sales;
agent_id (varchar),
day(int)
I can't figure out how to combine these tables. I think I need to use a join as all agents must be returned - even if they don't appear in the agent_count table.
First I make a simple call to get the week and month totals for all agents
SELECT agent_id, team_id, name, week, month FROM agent_count;
the I create a temporary table of todays sales, and then I count the sales for each agent for the day
CREATE TEMPORARY TABLE temp_todays_sales
SELECT s.id, s.agent_id
FROM sales s
WHERE DATEDIFF(s.uploaded, NOW()) = 0
AND s.valid = 1;
SELECT tts.agent_id, COUNT(tts.id) as today
FROM temp_todays_sales tts
GROUP BY tts.agent_id;
What is the best/easiet way to combine these to end up with a resultset such as
agent_id, team_id, name, day, week, month
where week and month also include the daily totals
thanks for any help!
Christy
SELECT s.agent_id, ac.team_id, ac.name,
s.`day` + COALESCE(ac.`day`, 0) AS `day`,
s.`day` + COALESCE(ac.`week`, 0) AS `week`,
s.`day` + COALESCE(ac.`month`, 0) AS `month`
FROM sales s
LEFT JOIN
agent_count ac
ON ac.agent_id = s.agent_id
team_id and name will be NULL if there is no record in agent_count for an agent.
If the agents can be missing from both tables, you normally would need to make a FULL JOIN but since MySQL does not support the latter you may use its poor man's substitution:
SELECT agent_id, MAX(team_id), MAX(name),
SUM(day), SUM(week), SUM(month)
FROM (
SELECT agent_id, NULL AS team_id, NULL AS name, day, day AS week, day AS month
FROM sales
UNION ALL
SELECT *
FROM agent_count
) q
GROUP BY
agent_id