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 would like to sum two columns in SQL and if the sum is greater than 0, then output it to one of the columns and if it is lesser than 0, then output to another column.
My code looks like this:
SELECT IF(SUM(`processed_quantity_long`,-1*`processed_quantity_short`) > 0,SUM(`processed_quantity_long`,-1*`processed_quantity_short`) AS `Position Long`,SUM(`processed_quantity_long`,-1*`processed_quantity_short`) AS `Position Short`)
From table A
GROUPBY date
It is returning me this error:
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '-1*processed_quantity_short)>0,SUM(processed_quantity_long,-1*`processed_qua' at line 5
Not sure how to resolve this error.
SUM does not take two arguments. Just subtract the two numbers before calculating the sum:
SELECT
CASE WHEN SUM(processed_quantity_long - processed_quantity_short) >= 0 THEN SUM(processed_quantity_long - processed_quantity_short) END AS `Position Long`,
CASE WHEN SUM(processed_quantity_long - processed_quantity_short) < 0 THEN SUM(processed_quantity_long - processed_quantity_short) END AS `Position Short`
FROM tablea
GROUP BY date
you did wrong on inside if
SELECT IF(
SUM(processed_quantity_long-processed_quantity_short) > 0,
SUM(`processed_quantity_long`-`processed_quantity_short`) ,
SUM(`processed_quantity_long`-`processed_quantity_short`) AS `Position Short`
) From tableA GROUP BY date
general if statement is like below
SELECT IF(500<1000, "YES", "NO")
Or use case when
case when SUM(`processed_quantity_long`-`processed_quantity_short`) > 0
then SUM(`processed_quantity_long`-`processed_quantity_short`)
else SUM(`processed_quantity_long`-`processed_quantity_short`) as position
from tableA GROUP BY date
Perhaps the simplest way to write the code is:
SELECT GREATEST(SUM(processed_quantity_long - processed_quantity_short), 0) AS Position_Long,
LEAST(SUM(processed_quantity_long - processed_quantity_short), 0) AS Position_Short
FROM tablea
GROUP BY date
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 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;
SELECT * FROM `your_table` LIMIT 0, 10
->This will display the first 1,2,3,4,5,6,7,8,9,10
SELECT * FROM `your_table` LIMIT 5, 5
->This will show records 6, 7, 8, 9, 10
I want to Show data 2,3,4,5,6,7,8,9,10,1 and
next day 3,4,5,6,7,8,9,10,1,2
day after next day 4,5,6,7,8,9,10,1,2,3
IS IT POSSIBLE with out updating any data of this table ???
You can do this using the UNION syntax:
SELECT * FROM `your_table` LIMIT 5, 5 UNION SELECT * FROM `your_table`
This will first select rows within your limit, and then combine the remainder from the second select. Note that you don't need to set a limit on the second select statement:
The default behavior for UNION is that duplicate rows are removed from the result. The optional DISTINCT keyword has no effect other than the default because it also specifies duplicate-row removal. With the optional ALL keyword, duplicate-row removal does not occur and the result includes all matching rows from all the SELECT statements.
I don't think this might be achieved using a simple Select (I may be wrong). I think you'll need a stored procedure.
You've tagged this as Oracle, though your SQL syntax would be invalid for Oracle because it doesn't support LIMIT
However, here's a solution that will work in Oracle:
select *
from ( select rownum as rn,
user_id
from admin_user
order by user_id
) X
where X.rn > :startRows
and X.rn <= :startRows + :limitRows
order by case when X.rn <= :baseRef
then X.rn + :limitRows
else
X.rn
end ASC
;
where :startRows and :limitRows are the values for your LIMIT, and :baseRef is a value between 0 and :limitRows-1 that should be incremented/cycled on a daily basis (ie on day 1 it should be 0; on day 2, 1; on day 10, 9; on day 11 you should revert to 0). You could actually use the current date, converted to Julian and take the remainder when divided by :limitRows to automate calculating :baseRef
(substitute your own column and table names as appropriate)
Well, it might be a little bit late for the author of the question, but could be useful for people.
Short answer: It is possible to do the "spin" like author asked.
Long answer: [I'm going to explain for MySQL first - where I tested this]
Let's imagine that we have table your_table (INT rn, ...). What you want is to sort in specific way ("spin" with beginning at the rn=N). First condition of ordering is rn >= N desc. The idea (at least how I understand this) is we change the order from asc to desc and split our table in two parts (<N and >=N). Then we order this back by rn but asc order. It will execute sorting for each group independently. So here is our query:
select * from your_table where rn between 1 and 10
order by rn >= N desc, rn asc;
If you don't have rn column - you always can use the trick with parameter
select t.*, #rownum := #rownum + 1 AS rn
from your_table t,
(SELECT #rownum := 0) r
where #rownum < 10 /* here be careful - we already increased by 1 the rownum */
order by #rownum >=N - 1 desc, /* another tricky place (cause we already increased rownum) */
#rownum asc;
I don't know if the last one is efficient, though.
For Oracle, you always can use rownum. And I believe that you will have the same result (I didn't test it!).
Hope it helps!