SQL Date interval not working with AND clause - mysql

I am trying to count views in time interval. The request below works only without the AND clause
If I take out the AND s.timestamp < 2019-01-31 it works just fine.
SELECT s.category_id, c.name, count(s.category_id) AS ViewCount
FROM stat_product_category s
JOIN category c ON s.category_id = c.id
WHERE s.timestamp > 2019-01-01 AND s.timestamp < 2019-01-31
GROUP BY s.category_id
ORDER By ViewCount Desc
LIMIT 10

you need single quote on date value
SELECT s.category_id, c.name, count(s.category_id) AS ViewCount
FROM stat_product_category s
JOIN category c ON s.category_id = c.id
WHERE s.timestamp > '2019-01-01' AND s.timestamp <'2019-01-31'
GROUP BY s.category_id ,c.name
ORDER By ViewCount Desc
LIMIT 10

Those aren't timestamps - without quotes, you just have a couple of ints you're subtracting. 2019-01-01 is evaluated as 2019-1-1, or 2017. 2019-01-31 is evaluated as 2019-1-31, or 1987. There is no number that's greater than 2017 but smaller than 1987, so you get no results. Surrounding these values with quotes will make them a string literal, and allow the database to perform an implicit conversion to a date:
SELECT s.category_id, c.name, count(s.category_id) AS ViewCount
FROM stat_product_category s
JOIN category c ON s.category_id = c.id
WHERE s.timestamp > '2019-01-01' AND s.timestamp < '2019-01-31'
-- Here ------------^----------^-------------------^----------^
GROUP BY s.category_id
ORDER By ViewCount Desc
LIMIT 10

that script is looking good you just need to pop qoutes around your dates so that SQL knows where the values start & stops.
SELECT s.category_id, c.name, count(s.category_id) AS ViewCount
FROM stat_product_category s
JOIN category c ON s.category_id = c.id
WHERE s.timestamp > '2019-01-01' AND s.timestamp < '2019-01-31'
GROUP BY s.category_id
ORDER By ViewCount Desc
LIMIT 10
(tested & working!)
Good luck with the project!

Related

Select accounts where last order placed was older than X date

I am trying to identify shoppers who last placed an order on or before a specific date.
I want to do this so that I can identify stale accounts and unsubscribe them from mailings.
So far I have this query, but it seems to be retrieving accounts that have ordered more recently than the specified date - which is not what I want!
SELECT
s.id,s.first_name,s.last_name,s.email, latest_orders.last_order_date, au.verified, au.unh
FROM
(SELECT
shopper_id, MAX(order_date) AS last_order_date
FROM
order_list WHERE order_date <= ?
GROUP BY
shopper_id) AS latest_orders
INNER JOIN
shoppers s
ON
s.id = latest_orders.shopper_id
JOIN auth_users au ON au.shopper_id=s.id
WHERE s.mail_outs='Y' LIMIT 50
Use having:
SELECT shopper_id, MAX(order_date) AS last_order_date
FROM order_list
GROUP BY shopper_id
HAVING MAX(order_date) <= ?;
If you want all information about the shoppers, then join back to shoppers:
SELECT s.*, os.last_order_date
FROM shoppers s JOIN
(SELECT shopper_id, MAX(order_date) AS last_order_date
FROM order_list
GROUP BY shopper_id
HAVING MAX(order_date) <= ?
) os
ON os.shopper_id = s.id;

Trouble Understanding Joining Tables SQL

Please see attached Tables image.
The question I have:
Find the top 5 occupations that borrowed the most in 2016
The code I have:
select c.occupation, count(*) no_mostborrow
from client c
Inner Join client c on c.clientID = b.clientID
where b.borrowDate >= '2016-01-01' and b.borrowDate < '2017-01-01'
group by c.clientoccupation, c.clientid
order by count(*) asc
limit 5
I feel like I am missing something here but I am not sure what. I am sure I am completely off. Thank you so much for your time.
To answer your question, you only want occupation in the group by. And the join needs to be correct:
select c.occupation, count(*) as no_mostborrow
from client c join
borrower b
on c.clientid = b.clientid
where b.borrowDate >= '2016-01-01' and b.borrowDate < '2017-01-01'
group by c.clientoccupation
order by count(*) asc
limit 5

SQL find highest date in all entries with a date older than x

I try to obtain a list of all customers my company has not had any assignments for in the last year.
SELECT MAX(assignment_date), full_name
FROM assignments
CROSS JOIN customers
WHERE assignments.customer_id = customers.id
AND assignment_date < '2017-01-01' -- Dynamic value from backend
GROUP BY full_name
ORDER BY assignment_date DESC
This does not seem to work as intended however, since it only returns some customers we did have assignments for in that timeframe. How would I go about implementing such a feature?
Try this code:
SELECT MAX(assignment_date), full_name
FROM customers
where id not in (SELECT id FROM customers inner join assigments on customers.id = assignments.customer_id WHERE assignment_date > '2017-01-01' )
This will return all customers in your database and remove all of them who did have assigments in last year. You should get all customers without assigments before '2017-01-01' as a result
left join customers to assignments where assignments.customer_id IS NULL
and assignment_date greater than '2017-01-01' i.e.
SELECT MAX(assignment_date), full_name
FROM assignments
WHERE assignments.customer_id IN
(SELECT customers.id
FROM customers
LEFT JOIN assignments ON assignments.customer_id = customers.id
WHERE assignments.customer_id IS NULL
AND assignment_date > '2017-01-01')
GROUP BY full_name
ORDER BY assignment_date DESC
I would suggest left join, group by and having:
SELECT MAX(assignment_date), full_name
FROM customers c LEFT JOIN
assignments a
ON a.customer_id = c.id
GROUP BY c.full_name
HAVING MAX(a.assignment_date) < '2017-01-01' OR
MAX(a.assignment_date) IS NULL
ORDER BY MAX(assignment_date) DESC;
It seems you want to show all customers with their last assignment date, but you want to restrict that list to customers that had no assignment since 2017-01-01. This means the dates you will be showing will be null for those customers who had never had an assignment and a date before 2017-01-01 for the others.
So outer join the last dates to the customers and only keep the rows where that date is before 2017-01-01 or null:
select c.full_name, a.max_date
from customers c
left join
(
select customer_id, max(assignment_date) as max_date
from assignments
group by customer_id
) a on a.customer_id = c.customer_id
where a.max_date < date '2017-01-01'
or a.max_date is null
order by a.max_date desc;

MySQL 5.7 | GROUP BY | Non Aggregated Column Error

I upgraded our mysql db from 5.6 to 5.7 and am in the process of fixing some queries which are throwing some errors. One of the queries I am working involves a GROUP BY with a COALESCE.
Here is the query (abstracted) that works:
SELECT
MAX(a.id),
a.entered,
count(*) AS teh_count
FROM
a
INNER JOIN
b ON b.id = a.link_to_b_id
INNER JOIN
c ON c.link_to_b_id = b.id
WHERE
b.revision_id > 0
AND
c.terminated_at = '0000-00-00 00:00:00'
AND
a.created_at > date_sub(NOW(), INTERVAL 8 HOUR)
GROUP BY
a.entered
ORDER BY
teh_count DESC
LIMIT
6;
But I need to COALESCE a.entered with c.override, so I tried the following:
SELECT
MAX(a.id),
a.entered,
COALESCE(c.override, a.entered) AS appearance,
count(*) AS teh_count
FROM
a
INNER JOIN
b ON b.id = a.link_to_b_id
INNER JOIN
c ON c.link_link_to_b_id = b.id
WHERE
b.revision_id > 0
AND
c.terminated_at = '0000-00-00 00:00:00'
AND
a.created_at > date_sub(NOW(), INTERVAL 8 HOUR)
GROUP BY
a.entered
ORDER BY
teh_count DESC
LIMIT
6;
But MySQL 5.7 now throws the following error: Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'st_core.tuc.code_appearance_override' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
I assume I can can change the sql_mode, but I'd prefer not too. What the error is telling me makes sense, in that the COALESCE column is not aggregated, so as a test I wrapped it with MAX and it works, however it seems kind of hacky to me.
Is there a more elegant solution?
You should also include a.entered in your group by clause and that's what the error saying. Though not sure why you are grouping by an different column a.code_entered?
Your query should look like
SELECT
MAX(a.id),
a.entered,
COALESCE(c.override, a.entered) AS appearance,
count(*) AS teh_count
FROM
a
INNER JOIN
b ON b.id = a.link_to_b_id
INNER JOIN
c ON c.link_link_to_b_id = b.id
WHERE
b.revision_id > 0
AND
c.terminated_at = '0000-00-00 00:00:00'
AND
a.created_at > date_sub(NOW(), INTERVAL 8 HOUR)
GROUP BY
a.entered,
COALESCE(c.override, a.entered)
ORDER BY
teh_count DESC
LIMIT
6;
I think you intend something like this:
SELECT MAX(a.id),
COALESCE(c.override, a.entered) AS appearance,
count(*) AS the_count
FROM a INNER JOIN
b
ON b.id = a.link_to_b_id INNER JOIN
c
ON c.link_link_to_b_id = b.id
WHERE b.revision_id > 0 AND
c.terminated_at = '0000-00-00 00:00:00' AND
a.created_at > date_sub(NOW(), INTERVAL 8 HOUR)
GROUP BY appearance
ORDER BY the_count DESC
LIMIT 6;
This removes a.entered from the SELECT list so there is only one column for grouping. That column can be referenced by table alias in the GROUP BY.

MySQL SELECT subquery

I have a calendar and user_result table and I need to join these two queries.
calendar query
SELECT `week`, `date`, `time`, COUNT(*) as count
FROM `calendar`
WHERE `week` = 1
GROUP BY `date`
ORDER BY `date` DESC
and the result is
{"week":"1","date":"2014-08-21","time":"15:30:00","count":"4"}, {"week":"1","date":"2014-08-20","time":"17:30:00","count":"12"}
user_result query
SELECT `date`, SUM(`point`) as score
FROM `user_result`
WHERE `user_id` = 1
AND `date` = '2014-08-20'
and the result is just score 3
My goal is to always show calendar even if the user isn't present in the user_result table, but if he is, SUM his points for that day where calendar.date = user_result.date. Result should be:
{"week":"1","date":"2014-08-21","time":"15:30:00","count":"4","score":"3"}, {"week":"1","date":"2014-08-20","time":"17:30:00","count":"12","score":"0"}
I have tried this query below, but the result is just one row and unexpected count
SELECT c.`week`, c.`date`, c.`time`, COUNT(*) as count, SUM(p.`point`) as score
FROM `calendar` c
INNER JOIN `user_result` p ON c.`date` = p.`date`
WHERE c.`week` = 1
AND p.`user_id` = 1
GROUP BY c.`date`
ORDER BY c.`date` DESC
{"week":"1","date":"2014-08-20","time":"17:30:00","count":"4","score":"9"}
SQL Fiddle
ow sorry, i was edited, and i was try at your sqlfiddle, if you want to show all date from calendar you can use LEFT JOIN, but if you want to show just the same date between calendar and result you can use INNER JOIN, note: in this case INNER JOIN just show 1 result, and LEFT JOIN show 2 results
SELECT c.`week`, p.user_id, c.`date`, c.`time`, COUNT(*) as count, p.score
FROM `calendar` c
LEFT JOIN
(
SELECT `date`, SUM(`point`) score, user_id
FROM `result`
group by `date`
) p ON c.`date` = p.`date`
WHERE c.`week` = 1
GROUP BY c.`date`
ORDER BY c.`date` DESC
I put a pre-aggreate query / group by date as a select for the one person you were interested in... then did a left-join to it. Also, your column names of week, date and time (IMO) are poor choice column names as they can appear to be too close to reserved keywords in MySQL. They are not, but could be confusing..
SELECT
c.week,
c.date,
c.time,
coalesce( OnePerson.PointEntries, 0 ) as count,
coalesce( OnePerson.totPoints, 0 ) as score
FROM
calendar c
LEFT JOIN ( select
r.week,
r.date,
COUNT(*) as PointEntries,
SUM( r.point ) as totPoints
from
result r
where
r.week = 1
AND r.user_id = 1
group by
r.week,
r.date ) OnePerson
ON c.week = OnePerson.week
AND c.date = OnePerson.date
WHERE
c.week = 1
GROUP BY
c.date
ORDER BY
c.date DESC
Posted code to SQLFiddle