i tried the code in mysql it is working well
select car_id,cust_id,due,DATEDIFF(NOW(),due) as elap from rental where car_id = '1'
issue date 2019-10-16 return date 2019-10-17
have to calucalated elap result displayed in -1 it is working well.
if i tried in sqlserver it produced wrong output
select cat_id,cust_id,date,due,DATEDIFF(dd,GETDATE(),due) as elap from rental where cat_id = '1'
have to calucalated elap result displayed in 1.wrong output right on is -1 i don't know what is the error on sqlserver code
SQL Server's DATEDIFF function uses the format:
DATEDIFF(datepart, start_date, end_date)
It returns the difference between the ending date and the starting date, in that order. So, in your example, if the due date is one day past the current date, you would get a date diff of +1.
MySQL's DATEDIFF computes the difference in the opposite order, namely taking the first date minus the second date, i.e.
DATEDIFF(date1, date2)
this would return the number of days which elapsed from date2 to get to date1, i.e. date1 - date2.
This will work correctly.
select cat_id,cust_id,date,due,DATEDIFF(dd,due,GETDATE()) as elap from rental where cat_id = '1'
Related
I am using the Graph Reports for the select below. The MySQL database only has the active records in the database, so if no records are in the database from X hours till Y hours that select does not return anything. So in my case, I need that select return Paypal zero values as well even the no activity was in the database. And I do not understand how to use the UNION function or re-create select in order to get the zero values if nothing was recorded in the database in time interval. Could you please help?
select STR_TO_DATE ( DATE_FORMAT(`acctstarttime`,'%y-%m-%d %H'),'%y-%m-%d %H')
as '#date', count(*) as `Active Paid Accounts`
from radacct_history where `paymentmethod` = 'PayPal'
group by DATE_FORMAT(`#date`,'%y-%m-%d %H')
When I run the select the output is:
Current Output
But I need if there are no values between 2016-07-27 07:00:00 and 2016-07-28 11:00:00, then in every hour it should show zero active accounts Like that:
Needed output with no values every hour
I have created such select below , but it not put to every hour the zero value like i need. showing the big gap between the 12 Sep and 13 Sep anyway, but there should be the zero values every hour
(select STR_TO_DATE ( DATE_FORMAT(acctstarttime,'%y-%m-%d %H'),'%y-%m-%d %H')
as '#date', count(paymentmethod) as Active Paid Accounts
from radacct_history where paymentmethod <> 'PayPal'
group by DATE_FORMAT(#date,'%y-%m-%d %H'))
union ALL
(select STR_TO_DATE ( DATE_FORMAT(acctstarttime,'%y-%m-%d %H'),'%y-%m-%d %H')
as '#date', 0 as Active Paid Accounts
from radacct_history where paymentmethod <> 'PayPal'
group by DATE_FORMAT(#date,'%y-%m-%d %H')) ;
I guess, you want to return 0 if there is no matching rows in MySQL. Here is an example:
(SELECT Col1,Col2,Col3 FROM ExampleTable WHERE ID='1234')
UNION (SELECT 'Def Val' AS Col1,'none' AS Col2,'' AS Col3) LIMIT 1;
Updated the post: You are trying to retrieve data that aren't present in the table, I guess in reference to the output provided. So in this case, you have to maintain a date table to show the date that aren't in the table. Please refer to this and it's little bit tricky - SQL query that returns all dates not used in a table
You need an artificial table with all necessary time intervals. E.g. if you need daily data create a table and add all day dates e.g. start from 1970 till 2100.
Then you can use the table and LEFT JOIN your radacct_history. So for each desired interval you will have group item (group by should be based on the intervals table.
I have the next table named expenses_partial_payment.
My query is the next, i need the sum of amount_paid as total_paid:
SELECT id_partial_payment, epp.id_billing, epp.id_user, epp.month_of_payment,
COALESCE(SUM(epp.amount_paid),0) AS total_paid,
(be.total - amount_paid) AS pending_total , be.total as total
FROM expenses_partial_payment epp
INNER JOIN BillingExpenses be ON epp.id_billing = be.id_billing
WHERE epp.id_user = 23
but when i add in the clause where
WHERE epp.id_user = 23 AND (Month_of_payment BETWEEN '2015-01-01' AND LAST_DAY('2016-01-01'))
I have an error with the total_piad, the query returns only the last row (300) and don't sum the amount_paid , the correct sum must be 501.00
What can i do to get the correct sum adding the clause where to filter the month_of_payment
(month_of_payment BETWEEN '2015-01-01' AND LAST_DAY('2016-01-01'))
month_of_payment BETWEEN '2015-01-01' AND LAST_DAY('2016-01-01')
last day() returns the last day of the Month not year. So it will not pull up data in the month greater than January.
Try to replace your date with this
STR_TO_DATE('01/01/2015', '%m/%d/%Y')
im using a query to get data between dates but for some reason it does not pull the data of the last date selected here is my query:
SELECT * FROM order WHERE status = "completed" AND orderdate >= ? AND orderdate <= ? ORDER BY orderid DESC
Im using is equal to or less then... but still?
what am i doing wrong ?
SELECT * FROM order WHERE status = "completed" AND date(orderdate) >= date(?) AND date(orderdate) <= date(?) ORDER BY orderid DESC
It happened with me also, but in my case instead of passing a date I was querying using a datetime variable, Please make sure you are querying with date variable only.
Make sure that orderdate is date as well as your query parameter is also date, or use appropriate function to convert them in date, than query.
Your dates are actually datetimes - so you are actually, in the case of the upperbound, saying "12 midnight" on whichever date you choose. Hence, if it tries to test a value at say 10am in the morning, it fails as being outside the range.
Either set the upperbound date one day forward, or explicitly only test the date part of the datetime...
I have the following data in my table. BTW ... this is a DD/MM/YYYY format:
Date
18/09/2012
17/09/2012
13/09/2012
11/09/2012
10/09/2012
09/09/2012
25/08/2012
24/08/2012
The result what I want are:
Date
18/09/2012
13/09/2012
11/09/2012
09/09/2012
25/08/2012
The rule:
It starts from the latest date (18/09/2012) and check the next one down (17/09/2012). If there is a date then removed that from the list because it requires to have 1 day apart. Then goes to 13/09/2012 and then check 12/09/2012 and didn't find and then move to next one so on and so on. Basically you can't have date close each other (min 1 day apart).
Now I can do this on cursor if it's on TSQL however since I'm working on MySQL, is there any such thing in MySQL? Or perhaps any sub-queries approach that can solve this query?
I'm appreciated your feedback.
Try this solution -
SELECT date FROM (
SELECT
date, #d := IF(#d IS NULL OR DATEDIFF(#d, date) > 1, date, #d) start_date
FROM
dates,
(SELECT #d:=null) t
ORDER BY
date DESC
) t
WHERE start_date = date
The subquery finds out start days (18, 13, 11...), then WHERE condition filters records. Try to run the subquery to understand how it works -
SELECT
date, #d := IF(#d IS NULL OR DATEDIFF(#d, date) > 1, date, #d) start_date
FROM
dates,
(SELECT #d:=null) t
ORDER BY
date DESC
SELECT
"MyTable1"."Date"
FROM
"MyTable" AS "MyTable1"
LEFT JOIN "MyTable" AS "MyTable2" ON
ADDDATE("MyTable1"."Date", INTERVAL 1 DAY) = "MyTable2"."Date"
WHERE
"MyTable2"."Date" IS NULL
ORDER BY
"MyTable1"."Date" DESC
As long as I know about mysql query will be quit tricky and buggy if some how you manage to write the one. I suggest go for cursor, here is the syntax of the cursor,
here is the syntax of the cursor
my query :
SELECT (
`total_hours`
)
FROM work_details
WHERE `employee_id` = '28'
AND DATE
BETWEEN '2012-02-01'
AND '2012-02-01'
LIMIT 0 , 30
// Which takes total hours worked for a given employee in given date from database table work_details
result:
total_hours
02:27:29
00:13:56
03:03:49
00:00:03
00:30:20
01:04:13
//result shows the times an employee worked in a given date,I need to get the total of these values to find total working hour in a given date.
how to get sum of these values in a field???
if i use sum(total_hours) result would be
sum( total_hours )
67870
and that is not correct.I require it in time type itself.
And i have one more help needed, this query worked because i gave same date for the BETWEEN clause.
DATE
BETWEEN '2012-02-01'
AND '2012-02-01'
But i need to calculate total working hours of days in a given range , say
DATE
BETWEEN '2012-02-01'
AND '2012-02-29'
Since a given date has multiple total hours entry , i need get total hours for each distinct day.Help??
try this (taken from here):
SELECT SEC_TO_TIME(SUM(TIME_TO_SEC(`total_hours`))) ... -- rest of your query