Trouble with Syntax format for Datediff - SQL - sql

I have a syntax formatting issue with the query below.
I am trying to get the difference between two time columns and then subtract 20 to get whatever the difference is minus 20. I also want to take the max value of either that or 0 so anything less than 0 will be 0.
select id, sum(max(0, (date_diff('minute', time_a, time_b)) - 20)) as mins
FROM tbl
What am doing wrong in the query above that is erorring out?
Thanks!

sum(max()) is highly suspicious. Perhaps you intend:
select id, sum(greatest(0, date_diff('minute', time_a, time_b) - 20)) as mins
from tbl

Related

ORA 00933 sql command not properly ended

I wrote this block of code
SELECT max(order)
FROM orders_table
GROUP BY UNIX_TIMESTAMP(timestamp) DIV 30 ;
Order is a column that I'm trying to take the max of every 30 seconds from a table called orders_table. I found the last line of code on here in the answer to someone elses program. However, I get an error when I try to run this code.
Thanks in advance
Your query uses MySQL syntax. In Oracle server, neither DIV nor UNIX_TIMESTAMP exists.
To do integer division, you may just TRUNC the results of the division.
To compute the number of seconds since January 1st, 1970, you could use the following expression (since Oracle, when substracting dates, returns the result as a number of days) :
(date_column - TO_DATE('1970-01-01', 'yyyy-mm-dd')) * 60 *60 *24
You probably want :
SELECT MAX(o.order)
FROM orders_table o
GROUP BY TRUNC(o.timestamp - TO_DATE('1970-01-01', 'yyyy-mm-dd')) / 30 )

sqlite3 unixtime interval query by multiplication and division

I have been trying to create a query for my sqlite3 database that provides me with a count of all records at 10 minute intervals between a maximum and minimum time.
I found this answer on the internet, and it seems to work:
select (((`unixtime`)/600000)*600000) as timeslice,
count(*) as mycount from mytable
where
`unixtime` >= 1413902772599
and
`unixtime` <= 1413972793000
group by timeslice;
The result I get is something like this:
timeslice mycount
------------- ----------
1413930000000 9
1413930600000 1013
1413931200000 265
1413932400000 410
1413933000000 643
This seems like sort of a hackish way to go about doing this query. It also doesn't include datapoints that have a zero count, which is an edge-case that I am going to have to fix outside of the database scope (unless there is an SQL solution for this).
Is there a better way to go about this? Are there edge cases for this if I proceed to continue using this query? Will this catastrophically fail under certain scenarios that I'm not considering?
There is no better way to round to multiples of 600000; SQLite has the round() function, but you would still need to convert to/from a value that can be rounded to some decimal fraction.
If you had SQLite 3.8.3 or later, you could use a recursive common table expression to generate the intervals:
WITH RECURSIVE intervals(t) AS (
VALUES(1413902400000)
UNION ALL
SELECT t + 600000
FROM intervals
WHERE t < 1413972000000
)
SELECT intervals.t,
COUNT(*)
FROM intervals
LEFT JOIN MyTable
ON MyTable.unixtime BETWEEN intervals.t
AND intervals.t + 599999
GROUP BY 1;

In tsql how to select records based on even or uneven dates

I am trying to figure out how to be able to select records based on even or uneven dates.
I have a table with 4 columns and one has the sign up date and I would like to segment them into two groups based on their sign up dates (using the day as the denominator). So 12/4/2013 would be in the even and 4/3/2012 in the uneven.
I am not sure how to construct this query, I was looking at the datepart but wasn't sure if there is something more straight forward.
thanks
SELECT signed_on, DAY(signed_on) % 2 AS uneven
FROM YourTable
uneven will be either 0 or 1
SELECT DAY('2013-01-03') % 2 -- 1
SELECT DAY('2013-01-02') % 2 -- 0
If you want to name your column even rather than uneven you could just inverse the result.
SELECT signed_on, POWER(DAY(signed_on) % 2 - 1, 2) AS even
FROM YourTable
HINT (for now unless you can show you've tried something yourself) You could do something like DATEPART(day, date) % 2 with a case statement (case statement isn't necessary, but could be if you want to easily go between even and uneven without changing the query of course depending on your environment).
DATEPART(dd, your_date) should give you the date part of your date and then use % to find if it is even or not in the WHERE clause.
I believe something like this should be straight forward -
SELECT COL,CASE WHEN DAY(COL)%2=0 THEN 'EVEN' ELSE 'UNEVEN' END AS [EVEN_UNEVEN]
FROM TEST
Sample output from my sample table-
COL EVEN_UNEVEN
2014-01-09 22:39:51.203 UNEVEN
2014-01-10 22:39:51.210 EVEN
2014-01-12 22:39:51.210 EVEN
2014-01-13 22:39:51.213 UNEVEN

SQL: Difference between "BETWEEN" vs "current_date - number"

I am wondering which of the following is the best way to implement and why.
select * from table1 where request_time between '01/18/2012' and '02/17/2012'
and
select * from table1 where request_time > current_date - 30
I ran the two queries through some of my date tables in my database and using EXPLAIN ANALYZE I found these results:
explain analyze
select * from capone.dim_date where date between '01/18/2012' and '02/17/2012'
Total runtime: 22.716 ms
explain analyze
select * from capone.dim_date where date > current_date - 30
Total runtime: 65.044 ms
So it looks like the 1st option is more optimal. Of course this is biased towards my DBMS but these are still the results I got.
The table has dates ranging from 1900 to 2099 so it is rather large, and not just some dinky little table.
Between has the inclusive ranges i.e when you issue a query like id between 2 and 10 the value of 2 and 10 will also be fetched.If you want to eliminate these values use > and <.
Also when indexes are applied say on date column > and < makes a good use of index than between.

how to get data whose expired within 45 days..?

HI all,
i have one sql table and field for that table is
id
name
expireydate
Now i want only those record which one is expired within 45 days or 30 days.
how can i do with sql query .?
I have not much more exp with sql .
Thanks in advance,
If you are using mysql then try DATEDIFF.
for 45 days
select * from `table` where DATEDIFF(now(),expireydate)<=45;
for 30 days
select * from `table` where DATEDIFF(now(),expireydate)<=30;
In oracle - will do the trick instead of datediff and SYSDATE instead of now().[not sure]
In sql server DateDiff is quite different you have to provide unit in which difference to be taken out from 2 dates.
DATEDIFF(datepart,startdate,enddate)
to get current date try one of this: CURRENT_TIMESTAMP or GETDATE() or {fn NOW()}
You can use a simple SELECT * FROM yourtable WHERE expireydate < "some formula calculating today+30 or 45 days".
Simple comparison will work there, the tricky part is to write this last bit concerning the date you want to compare to. It'll depend of your environment and how you stored the "expireydate" in the database.
Try Below:-
SELECT * FROM MYTABLE WHERE (expireydate in days) < ((CURRENTDATE in days)+ 45)
Do not execute directly! Depending of your database, way of obtaining a date in days will be different. Go look at your database manual or please precise what is your database.