So basically, I want to create reports for my Attendance Records Table.
Table has :user_id, :clock_in, :clock_out, :seconds columns that are being updated.
Basically my report needs: to calculate (sum of :seconds) for a given period.
I.e. Jan 1, 2014 to Jan 31, 2014 ... I would like to list all records from this daterange, and in the end would calculate sum of :seconds.
Any ideas how would I approach this?
Your question is very general, please, before question try to do some work and come with technical questions.
Tips
Your question is related about SQL, basically you need to perform a select query
select * from your_table where date <= your_date and date >= your_date
Need help on mysql date range query
In rails, where clauses are build by ActiveRecord
YourModel.where('date <= ? and date >= ?', your_date. your_date)
Rails where date is greater than given date query
If you need something more advanced, use Ransack
http://railscasts.com/episodes/370-ransack
Hope I can help you ;)
Related
I tried researching this problem but I couldn't find anything that fits.
I have a query that pulls data the way I want, but it shows me results for the last 30 days only, the archive is about 3 years and I would like to add a parameter that will tell me the exact date, for example August 2019.
My query:
SELECT FROM_UNIXTIME(datetimeOrigination),
callingPartyNumber,originalcalledPartyNumber,
from_unixtime(datetimeDisconnect)
FROM corn.originalcdr
WHERE callingPartyNumber like '9000'
ORDER BY datetimeorigination DESC;
Could you please advise me on the best approach?
If you want to restrict, e.g., the origination datetime field to August of 2019, then add an appropriate criteria to the WHERE clause:
SELECT *
FROM corn.originalcdr
WHERE
callingPartyNumber = '9000' AND
FROM_UNIXTIME(datetimeOrigination) >= '2019-08-01' AND
FROM_UNIXTIME(datetimeOrigination) < '2019-09-01'
ORDER BY
datetimeOrigination DESC;
I have attendance data for employees stored in the table attendance with the following column names:
emp_id (employee ID)
date
type (leave, absent, etc.)
(there are others but I'm omitting them for the sake of simplicity)
My objective is to retrieve all dates of the given month on which the employee was on leave (type = 'Leave') and the last leave taken in the last month, if any.
It's easy to do it using two queries (I'm using PHP to get process the data), but is there any way this can be done in a single query?
I'm answering my own question so as to close it. As #bpgergo pointed out in the comments, UNION will do the trick here.
SELECT * FROM table_name
WHERE type="Leave" AND
date <= (CURRENT_DATE() - 30)
Select the fields, etc you want then se a combined where clause using mysql's CURRENT_DATE() function. I subtracted 30 for 30 days in a month.
If date is a date column, this will return everyone who left 1 month or longer ago.
Edit:
If you want a specific date, change the 2nd month like this:
date <= (date_number - 30)
Yes I've read the docs here
I have the following query:
SELECT * FROM db.table
WHERE event_type = 3
AND (YEAR(event_time) = 2014 AND MONTH(event_time) = 10)
This returns all records from October 2014. Great. Only wanted to ask, is there a shorter, better or neater way than having to write (YEAR(event_time) = 2014 AND MONTH(event_time) = 10)? Is this the "conventional" way of extracting a particular month in a given year?
Learning here.
If you have an index on the event_time column, using YEAR and MONTH (or pretty much any function) will prevent the index being used to optimize the query fully. Something like this will be fastest; I'll leave it to you to determine if it's neatest:
SELECT * FROM db.table
WHERE event_type = 3
AND event_time >= '2014-10-01' AND event_time < '2014-11-01'
That will give you everything in October 2014, and it's fully optimizable.
You can create a temporary view using the with clause of the records in the year 2014 and then extract only October from that view.
Because everytime the query needs to check if it is 2014 and month October.
So I think views will do it better.
Also you can enhance the performance by adding index
i need to retreive data from database with the condition from date to to date using between query,
my query is,
select * from Master where Date between '01-08-2013' and '30-08-2013'
but it retreive all data from the table...
i need only data with in that date..
i tried another one like,
select * from PatientMaster where EntryDate >= '01-08-2013' and EntryDate<= '30-08-2013'
how its posible..
whats wrong with my query...
sorry im very bad in english...
thank you in advance...
A date string has the syntax YYYY-MM-DD and not DD-MM-YYYY
select * from Master
where `Date` between '2013-08-01' and '2013-08-30'
for that you can use
select * from Master where Date >='01-08-2013' and dateadd(dd,1,'30-08-2013')
You have to convert your strings to dates. This page shows you how to do it in mysql, which is what you have tagged. For sql server, which is in your subject line, use this page.
Then you do a slight modification of your 2nd attempt. Instead of
and EntryDate <= the end date
you want
and EntryDate < the day after the end date
That takes care of any time components. It might not matter in your case, but it's a good habit to get into.
You'll be looking for an query that works with your format? (dd-mm-yy)
CAST to the desired format!
http://www.w3schools.com/sql/func_convert.asp
105 = dd-mm-yy
SELECT * FROM Master
WHERE CONVERT(date, Date, 105) BETWEEN '01-08-13' and '30-08-13'
Be conscious with regards of the choice of data type for date Columns,
with or without time, day or year first etc. and please do not use varchar
for dates...
know that it CAN be confusing to call a date column for only Date...
be consistent with high/lower case.
I have a mysql database that looks like this:
id | userid | timestamp | activity
Timestamp is a datetime data type, I need to get the data grouped by month, day and hour. I am using mysql and php for my scripts.
I am able to do it by month and day with the following query:
$query = "SELECT COUNT(id) as totals FROM security_transactions WHERE YEAR(timestamp) = 2012 GROUP BY MONTH(timestamp), DAY(timestamp)";
I need to do it by month day and hours.
Please help.
Thanks.
You can add , HOUR(TIME(timestamp)) to your group by query providing your column is of DATETIME format
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_hour
Also, from the error messages put in the comments below, it looks like #Aprentice is not using mysql, but I've improved this answer for others looking for mysql.
I have never used mssql, so I can't test this but the following might work to group by nearest hour:
GROUP BY dateadd(hour, datediff(hour, 0, timestamp, 0)
Just take it one step further and use HOUR() as well. You will first need to extract the time portion of the timestamp. But guess what, there is a function for that as well ;)