i have an question how to use an "alias" column at the where/having clause. I know there are some question with an related topic. But I search trough the web and on stackoverfolw and doesn't find an solution.
So i hope for help at this way..
The following statement works well:
SELECT PHRASE,COUNT(*) AS A
FROM my_statistics
WHERE TIMESTAMP>NOW()-INTERVAL 7 DAY
GROUP BY PHRASE ORDER BY A DESC
LIMIT 16;
PHRASE and TIMESTAMPare columns at the table.
The code selects the top 16 PHRASES, which are inserts by users on the last 7 days.
There are also exist an column USER and so i like now to select the top 16 phrases which are inserted by more than one user.
So i tried this:
SELECT PHRASE,COUNT(*) AS A, COUNT(DISTINCT(USER)) AS B
FROM my_statistics
WHERE TIMESTAMP>NOW()-INTERVAL 7 DAY
AND B>1
GROUP BY PHRASE ORDER BY A DESC
LIMIT 16;
On other questions on stackoverflow i fond the info, that i have to use HAVING
SELECT PHRASE,COUNT(*) AS A, COUNT(DISTINCT(USER)) AS B
FROM my_statistics
WHERE TIMESTAMP>NOW()-INTERVAL 7 DAY
GROUP BY PHRASE ORDER BY A DESC
HAVING B>1
LIMIT 16;
But this returns an error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'HAVING B>1
LIMIT 16' at line 5
I have no idea, how the right syntax could be.
Hope for any kind of help here.
Thank you!
Place the ORDER BY A DESC after Having clause.
SELECT phrase,
Count(*) AS A,
Count(DISTINCT( user )) AS B
FROM my_statistics
WHERE timestamp > Now() - INTERVAL 7 day
GROUP BY phrase
HAVING b > 1
ORDER BY a DESC
LIMIT 16;
Related
SELECT TOP (2)
DATEPART(HOUR,t.callerDateTime) as Hour
,count(cli) as Count
FROM ivrtimeout t
where convert(date,t.callerDateTime) = convert(date,getdate(),103)
group by DATEPART(HOUR,t.callerDateTime)
order by DATEPART(HOUR,t.callerDateTime) desc
Above Query is working fine in case of Microsoft SQL Server and giving error using in xamp server maria-db.
Can anyone please make correction?
Try this:
select
extract(HOUR from callerDateTime) as Hour,
count(cli) as Count
from ivrtimeout
where callerDateTime >= timestamp(current_date)
group by 1
order by 1 desc
limit 2
I have the following columns in my database:
- day
- browser
- platforms
- visitors_number
I have to choose the day of the week for which the average number of visitors was maximum and display this maximum average. I searched the forum, found a similar thread, but the solution does not work, I get an error. I would add that I am sitting on it for the second day and I lack ideas. I will add that it works on phpMyAdmin.
This is my code:
select DAYNAME(DAY), avgVIS
from
(
select DAYNAME(DAY), avg(VISITORS_NUMBER) as avgVIS
from dane_2
group by DAYNAME(DAY)
)
where avgVIS = (select max(avgVIS)
from ( select DAYNAME(DAY), avg(VISITORS_NUMBER) as avgVIS
from dane_2
group by DAYNAME(DAY)))
this is output's error
#1064 - Something is wrong in your syntax obok 'where avgVIS = (select max(avgVIS)
from ( select DAYNAME(DAY), a'
Can someone helps me ?
Thank you in advance
You already have the aggregation query that computes the daily average. All that is let to do is sort and limit:
select dayname(d.DAY) day_name, avg(d.VISITORS_NUMBER) avg_vis
from dane_2 d
group by dayname(d.DAY)
order by avg_vis desc
limit 1
Note: DAY is a reserved word in MySQL, so not a good choice for a column name (I qualified it with the table alias to avoid errors).
Want all the latest visit of all the distinct user.
For this I am using below query
Event.order(time: :desc).select('DISTINCT ON(user_id) user_id, time')
Getting SQL syntax error.
ActiveRecord::StatementInvalid (Mysql2::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ON(user_id) user_id, time FROM `events` ORDER BY `events`.`time` DESC ' at line 1: SELECT DISTINCT ON(user_id) user_id, time FROM `events` ORDER BY `events`.`time` DESC LIMIT 11)
events table column names
["id", "visit_id", "user_id", "name", "properties", "time"]
can anyone help me out in this
Expected output something like this
{ 21 => "2018-12-18 09:44:59", 42 => "2018-12-19 12:08:59"}
21 is user_id and value is last visit time
Using mysql as database
So you want all user visits with last visited time.
Instead of use DISTINCT function, you can use GROUP with MAX function.
Query looks like
Events.group(:user_id).maximum(:time)
This Outputs your desired results
{21=>Tue, 18 Dec 2018 11:15:24 UTC +00:00, 23=>Thu, 20 Dec 2018 06:42:10 UTC +00:00}
Hope this works for you.
FYI
DISTINCT ON(columns). is PostgreSQL Syntax.
Like i said in the comments DISTINCT ON(column), * is PostgreSQL SQL syntax.
The query below you need to have to rewrite in MySQL
SELECT
DISTINCT ON(user_id) AS user_id, time
FROM
<table>
ORDER BY
time DESC
LIMIT 11
The most easy to do that is using SUBSTRING_INDEX(GROUP_CONCAT(..)).
The queries below should give you the correct results.
SET SESSION group_concat_max_len = ##max_allowed_packet;
SELECT
user_id
, SUBSTRING_INDEX(
GROUP_CONCAT(time
ORDER BY
time DESC
)
, ','
, 1
) AS time
FROM
<table>
GROUP BY
user_id
ORDER BY
time DESC
LIMIT 11
I've been bugging this for the past hour..
I want to get the TOP 5 from latest data from my table clickednumbers
clickednumbers has only to columns numbers as INT & numberTime as timestamp
My query
SELECT AVG( SELECT *
FROM clickednumbers
ORDER BY numberTime DESC
LIMIT 5)
FROM clickednumbers
and im always getting the error
#1064 - You have an error in your SQL syntax;check the manual that corresponds to your MariaDB server version for the right syntanx to use near
'SELECT *
FROM clicked numbers
ORDER BY numberTime DESC '
at line 1
MariaDB Version:Server type: MariaDB
Server version: 10.1.9-MariaDB - mariadb.org binary distribution
Protocol version: 10
Sorry for bothering :(
Goal : To get the average of top 5 from latest numbers based on the numbersTime
To get the average of the top 5, use a subquery in the FROM clause:
SELECT AVG(numbers)
FROM (SELECT *
FROM clickednumbers
ORDER BY numberTime DESC
LIMIT 5
) cn;
Check this out for more of an idea what's going on. I think your query needs to look more like this:
SELECT AVG(x.numbers)
FROM (SELECT numbers FROM clickednumbers ORDER BY numberTime DESC
LIMIT 5) as x
I am trying to do a mysql query to select some news stories from a table, now the key is I always need 5 results.
so I was hoping to be able to pad out my results with another where clause ie
select * from here where this = 1
if there is < 5 results then select * from here where this = 2
limit [how ever many we are short say the first query brings back 3 results this should bring back 2]
Now I've looked at using a union to do this but without outside help ie from php and another query to count the results I don't think it is possible, I know I could simply use php to do this, and will probably end up doing that, but I was just wondering if what I am trying to do is possible with one mysql query?
EDIT:
also it needs to order by date but they are not really posted in order so
order by date get upto 5 where this = 1 and if there isn't 5 pad it out with the remainder of where this = 2 also ordered by date.
Another Shameful Edit:
ask a silly question lol... it was my sleep deprivation I just assumed there was data in the table and the previous coder was using unions to do all sorts of stuff, making me think it was more complex than it should be
SELECT *
FROM
news
WHERE
( this = 45 || this= 0 )
AND
active = '1'
ORDER BY
this ASC,
date_added DESC
LIMIT 5
How about -
SELECT *
FROM here
WHERE this < 5 -- added this WHERE clause based on the idea that there will be at least one item per this
ORDER BY this ASC, `date` ASC
LIMIT 5;
Or are you after the five results then being sorted by date again -
SELECT *
FROM (
SELECT *
FROM here
WHERE this < 5 -- added this WHERE clause based on the idea that there will be at least one item per this
ORDER BY this ASC, `date` ASC
LIMIT 5
) AS tmp
ORDER BY `date` ASC
You could combine the where clauses and use limit :
select * FROM here WHERE this = 1 OR this = 2 ORDER BY this LIMIT 5
Even in there were 15 records where this is equal to 1 this would only bring back 5 records ...