I have millions of records in a table with following columns:
id, item_id, customer_id, date
I want to find customer_id for a specific month which are inserted first time in the table in that month.
What is best and simple query for this.
Thanks
select customer_id , min(date) as min_date
from theTable
group by customer_id
having min_date>=<the desired date>
try something like:
SELECT
date_format(date,"%M") month,group_concat(customer_id ORDER BY customer_id) customer_ids
FROM
<table_name>
GROUP BY month;
Something like this may be:
select distinct(id) from customer
where month(date)='Your_month'
order by date
Related
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
I've a table cart which structure is following: id, item_id, session_id, date, num, so I need rows to be grouped by session_id column and sorted by date column in the same time, is it possible?
Yes it is possible.
... order by session_id, date
This will order firstly by session_id, and then by date. I believe that is what you want?
Try this:
SELECT * FROM YourTable
GROUP BY session_id ORDER BY date DESC;
Hope this helps!
You cant group by session_id and in the same time order by date.
You will get:
ORDER BY clause because it is not contained in either an aggregate function or the GROUP BY clause
what you can do is:
SELECT [session_id],[date]
From TableName
group by [session_id], [date]
order by [date] desc
Database is mysql. The table likes that:
Create table Persons (_id int8 primary key, name varchar, birthday long);
the birthday is the birthday's timestamp.
so I want to select the data that group by date, it means that the result should be one record for one day
select sum(_id) from Persons group by ....
Who can help me, please?
Try this way,
select sum(_id) FROM Persons GROUP BY DATE_FORMAT(birthday, '%Y%m%d')
perhaps:
select sum(_id) from Persons group by DATE_FORMAT(FROM_UNIXTIME(birthday), '%d-%m-%Y')
select sum(_id) from Persons group by date_format(birthday,'%y-%m-%d')
I have a column Date with the same name in four tables Products, Customers, Shops, World in mysql. what i want to do is that i want to find the greatest date in the DATE COLUMN of all four tables. I don't want the greatest date of every table but the greatest one in all four tables. I hope you are understanding. Please help me
The most efficient way I can think of would be using the greatest function:
select greatest(
(select max(date) from products),
(select max(date) from customers),
(select max(date) from shops),
(select max(date) from world)
) greatest_date
SQLFiddle demo
This might work:
SELECT MAX(Date) as Max_Date
FROM (
SELECT Date FROM Products
UNION
SELECT Date FROM Customers
UNION
SELECT Date FROM Shops
UNION
SELECT Date FROM World
) AS P;
So I have a schema that goes something like this
Historical -- Table
--CID
--ID
--LOCATION
--STATUS
--TIME
CID doesn't matter for this, but I do want to get the 1 oldest TIME (TimeStamp) for each ID
So what I'm trying to do is something like
SELECT DISTINCT(id) from Historical order by TIME asc limit 1 But where I get each ID, and the oldest time for it.
Thanks
Does this do what you want?
SELECT id, min(time) FROM Historical GROUP BY id;
SELECT id, MIN(TIME) FROM Historical GROUP BY id;
This will select distinct IDs (by grouping) and the MIN TIME for each grouped ID.