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
Related
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 have just started a new question to make this a bit more clear.
I have just migrated from MS SQL to MySQL this query worked in MS SQL
This is rstCombinedChartData
Then the result which should have 2 column 'yes' and 'no' with values for some reason on mysql I'm getting just one and itemcolumn false.
As you can see for some reason it's adding up all the results giving 551 where it should be like this:
Yes x
No x
Why?
This is the SQL Query:
SELECT
itemColumn
,SUM(valueColumn) AS valueColumn
,label
FROM
rstCombinedChartData
GROUP BY
label
,itemColumn
ORDER BY
label DESC
,itemColumn DESC
Plase try:
SELECT itemColumn, SUM(CAST(valueColumn AS SIGNED)), label FROM rstCombinedChartData GROUP BY label, itemColumn ORDER BY label DESC, itemColumn DESC
hope someone can help,
What i`m trying to do is pull all results from the database for any given month,
Is the possible using a mysql query only (no php).
Im using a template app with the only access i have is through i mysql where statement, So i need to work out from "2013-04-01" what the month is and grab it if its the one i want.
The code below works but obviously ties me too the 2013 year.
`SELECT * FROM table_name WHERE Display_Date => 2013-04-01 AND Display_Date <= 2013-04-30 LIMIT 10`
SELECT
*
FROM
table_name
WHERE
MONTH(Display_Date) = MONTH(CURDATE())
AND YEAR(Display_Date) = YEAR(CURDATE())
reference
MONTH()
YEAR()
I am running the following query but can't seem to get the datediff() filter to work. The query is executing successfully but even if I change the datediff() to different values I still get the same result.
SELECT project_id
, google_rank
, COALESCE(
( SELECT google_rank
FROM eig_ranking mi
WHERE mi.project_id = m.project_id
ORDER BY project_id limit 1
)
, 0) - google_rank AS movement
, keyword
, domain
FROM eig_ranking m
WHERE (DATEDIFF(rank_date, NOW())) / 7 <= 12
AND google_rank != 0
ORDER BY movement DESC
thanks for your replies. I figured it out. I am not sure if its a MYSQL bug or documentation fault but interchanging the variables in the datediff() function to DATEDIFF(NOW(), rank_date) makes it work fine for all my queries and the results are all correct