The following code gives users who contributed tags to an online community from March 1, 2016 to February 28, 2017.
SELECT userid, COUNT(*) AS tags
FROM tag_events
WHERE tstamp >= ‘2016-03-01’ AND tstamp <= ‘2017-03-01’
GROUP BY userid
ORDER BY tags DESC;
tag_events is the table, tstamp is the timestamp of the tag, userid is the user id, and each entry in the table contains information about one tag.
I'm interested in users whose first contribution within that period was at least two calendar days before their last contribution (so July 1 and 3 would count, July 1,2,3 would count, but July 1 and 2 would not count).
How can I modify the code?
You should get the result you want by calculating the difference in days between the first and last timestamp using the datediff function, like this:
SELECT userid, COUNT(*) AS tags
FROM tag_events
WHERE tstamp >= '2016-03-01' AND tstamp <= '2017-03-01'
GROUP BY userid
HAVING DATEDIFF(MAX(tstamp), MIN(tstamp)) > 1
ORDER BY tags DESC;
Sample SQL Fiddle
Related
I'm looking for a way to find all buyers who registered for the sales promotion in November 2022 only and also made at least 3 orders for the following 15 day period after the registration(OPTINDATE).
Here is my query that doesn't work:
SELECT ID, OPTINDATE,ORDERDATE, COUNT(ORDER)
FROM BUYER
INNER JOIN PURCHASE USING(ID)
WHERE OPTINDATE between '2022-11-01' and '2022-11-30'
AND ORDERDATE > OPTINDATE(day,15)
GROUP BY ID
HAVING COUNT(ORDER)>= 3;
I'Ve got syntax error.
This part doesn't make sense:
OPTINDATE(day, 15)
Perhaps you wanted to use the
ADDdate function:
ADDDATE(OPTINDATE, 15)
how can I count last 7 days data from SQL and group them by day/date (excluding today).
I should be able to use the result as $resultday1, $resultday2, $resultday3 etc.
If there was 10 total SQL entries in day 1 (yesterday) $resultday1 should show "10".
and the days should be last 7 only, and today/current day should not consider.
The following PHP SQL script shows the total count only
SELECT COUNT(1) FROM orders WHERE username='jondoe'
database is a list of referrals made by a registered user in previous days.
a single table contains all user's referral details, table name "orders" as per above example.
This is the exact query as you want
SELECT
COUNT(*), DATE(order_date) order_date
FROM
orders
WHERE
order_date < CURDATE()
AND order_date > DATE_SUB(NOW(), INTERVAL 7 DAY)
GROUP BY order_date
ORDER BY order_date DESC;
In oracle sql, how to get the count of newly added customers only for the month of april and may and make sure they werent there in the previous months
SELECT CUSTOMER ID , COUNT(*)
FROM TABLE
WHERE DATE BETWEEN '1-APR-2018' AND '31-MAY-2018' AND ...
If we give max (date) and min(date), we can compare the greater date to check if this customer is new , correct?
expected output is month count
april ---
may ---
should show the exact count how many new customers joined in these two months
One approach is to use aggregation:
select customer_id, min(date) as min_date
from t
group by customer_id
having min(date) >= date '2018-04-01 and
min(date) < date '2018-06-01';
This gets the list of customers (which your query seems to be doing). To get the count, just use count(*) and make this a subquery.
Currently, I have a table with 3 columns with id, clicks and dates. Every id has an entry with clicks for each day of a month with the format like '20180420' in the date column, let's say for 20th April 2018.
SELECT
id,
month(date),
COUNT(id)
FROM mydb
WHERE (date >= 20180101 AND date < 20180424) clicks > 0
GROUP BY id, MONTH(date);
I run this query to get if I have more than one clicks each month for this id and I want to do something like that to get the count of how many months every id has more than one clicks, but I don't get the correct result.
Am I missing something basic?
P.S: I am using MySQL 5.6
I guess the date is stored as a varchar, if so then convert string into date.Try this:
SELECT MONTH(STR_TO_DATE(A.date,'%y%m%d')) MONTH, COUNT(*) NUMBER_OF_CLICKS
FROM mydb A
WHERE STR_TO_DATE(A.date,'%y%m%d') BETWEEN STR_TO_DATE('20180101','%y%m%d') AND CURRENT_DATE
GROUP BY 1
ORDER BY 1 DESC;
In mySQL what I am trying to do is to query my table and find out how many people registered each day. In other words, I want to be able to produce the following output for one month:
1 January: 10 registrations
2 January: 150 registrations
3 January: 50 registrations
select created, regID
from registrations
Dates are in the following format in the DB: 2012-11-01 00:00:00
To get registration counts for each day of January, use this select:
select daymonth(registration_date), count(*)
from registrations
where registration_date >= '01/01/2012' and registration_date <= '01/31/2012'
group by daymonth(registration_date)
You usually use a grouping operator:
SELECT COUNT(*) AS registrations, DATE(created) AS created_date GROUP BY created_date
If created is already a DATE column, then the conversion isn't required.
Try this:
SELECT created, count(regID) FROM registrations GROUP BY created ORDER BY created ASC
SELECT DATE(DATE_REGISTERED) DATE, COUNT(*) totalRegistered
FROM tableName
GROUP BY DATE