MS Access. SQL. get current time - sql

I'm using MS Access and I'm trying to build SQL query. I have this table:
VISIT
VISIT_ID DATE_OF_VISIT COST.
I need to get current date in query, then I need to get year from this date, then I need to get sums of money for each month of that year as a result.
Does anyone know how to get current date?
Do I need to use some sort of cycles?

SELECT Month(date_of_visit), Sum(cost)
FROM VISIT
WHERE Year(date_of_visit) = Year(Date())
GROUP BY Month(date_of_visit);

In MS Access, for a query like this, I use the design tool. If I want the SQL, I use the design tool and then select the SQL view. Within the design tool, consider using the date() function or the now() function, or take a look at Access help. There are a number of options available and one should give you exactly what you are needing. That way, MS Access does all the coding work.

Related

Date/Timestamp column in DBeaver (SQL Client) but I only want the date (using a CSV file)

I'm fairly rookie when it comes to SQL but as of recent I've been having to use it in it's basic form to do very simple tasks like only recalling relevant columns from a table etc.
I'm currently using DBeaver as my SQL Client and for this example I'm tapping straight into a CSV, no problems there. The data I'm working with is transaction data and the table is structured as follows
My problem is that the data is in 15 minute intervals whereas I need a value per day per store per metric (I.E. in the image example, it would return "Site" = 101 - "Metric" = FOODSER3 - "Date" = 2020-08-09 - "Value" = 6.0000)
Firstly, is this possible
Secondly, if so then please could someone let me in on the secret of how and maybe an explanation as to what the resolution is and why so that I can really understand what's going on.
I'm fairly proficient in Javascript and VBA, but so far SQL defeats me at every hurdle.
The structure of such a query is aggregation. Date/time functions are notoriously dependent on a database, but the idea is:
select cast(date as date), site, metric, sum(value)
from t
group by cast(date as date), site, metric;

Subtract hours from SQL Server 2012 query result

I am running queries on an alarm system signal automation platform database in SQL Server 2012 Management Studio, and I am running into some hiccups.
My queries run just fine, but I am unable to refine my results to the level that I would like.
I am selecting some columns that are formatted as DATETIME, and I simply want to take the value in the column and subtract 4 hours from it (i.e., from GMT to EST) and then output that value into the query results.
All of the documentation I can find regarding DATESUB() or similar commands are showing examples with a specific DATETIME in the syntax, and I don't have anything specific, just 138,000 rows with columns I want to adjust time zones for.
Am I missing something big or will I just need to continue to adjust manually after I being manipulating my query result? Also, in case it makes a difference, I have a read-only access level, and am not interested in altering table data in any way.
Well, for starters, you need to know that you aren't restricted to use functions only on static values, you can use them on columns.
It seems that what you want is simply:
SELECT DATEADD(HOUR,-4,YourColumnWithDateTimes)
FROM dbo.YourTable
Maybe it will be helpful
SELECT DATEPART(HOUR, GETDATE());
DATEPART docs from MSDN

Access SQL Query using BETWEEN Statement and dates from a form

I'm probably missing something basic here, but I can't for the life of me get this working.
I have a database that is tracking when certain projects are completed, and I want to be able to show in a list the completed projects between a date range.
The date range to check between is set by the user on a form.
I have built a query in Access:
SELECT Logs.Completed
FROM Logs
WHERE Logs.Completed BETWEEN Forms!UIBrowseCompleted!Text53 AND Forms!UIBrowseCompleted!Text55
ORDER BY Logs.Completed;
I have got the dates formatted in the text box so they are in #MM/DD/YYYY# format (I have manually put these dates direct in the query and this works) but when I run the query I get the following error:
This expression is typed incorrectly, or it is too complex to be evaluated. For example, a numeric expression may contain too many complicated elements. Try simplifying the expression by assigning parts of the expression to variables.
I have tried modifying the query to take the # out of the text fields in the form, and including ' around the Forms!UIBrowseCompleted!Text53 but I've still not got any joy from it.
has anyone had this issue before, or can anyone point me in the right direction.
Thanks
Do you want MS Access query or SQL query?
If you want MS Access query then you can try with following
SELECT Logs.Completed
FROM Logs
WHERE Logs.Completed >= Forms!UIBrowseCompleted!Text53 AND Logs.Completed <= Forms!UIBrowseCompleted!Text55
ORDER BY Logs.Completed;
between query will take time normally we use >= and <= to check date range
like
>= Forms!UIBrowseCompleted!Text53 AND Logs.Completed <= Forms!UIBrowseCompleted

SELECT query causes "expression is too complex to evaluate or there is an error"

Initially there were 1000 records to evaluate.Now there are 40000.Please help!
I'm only trying to obtain the week number of a transaction based on transaction date and start date.
SELECT [1_Webtime_By_Date].Badge,Int(((([1_Webtime_By_Date].Date-Forms![Date Form].StartDate)+1.99)/7)+1) AS Week
FROM 1_Webtime_By_Date
GROUP BY [1_Webtime_By_Date].Badge,Int(((([1_Webtime_By_Date].Date-Forms![Date Form].StartDate)+1.99)/7)+1);
This is a known issue with the compiler used by Access. Limits of 64K segments were lifted after Access 97 but the amount of data you are quering is simply too much for Access. There are a few tips given on the following page that may help but it seems to me that you need to use a proper database system such as MS SQL. There is a free version available (SQL Express) if it's cost that is the problem.
ACC: "Out of Memory" or "Query Too Complex" with Query/Report
SQL Server Express Download page
You may find that using SQL Server as a database and Access as a front end may help your problems if you are tied to using Access for end users.
The best tip given is to use aliasing to shorten the length of your query and to try to remove nested queries:
Access SQL: FROM clause
My first inclination is to add some white space before and after both the minus signs.
My next test would to make the form field into a DATE type, and use something like
DATEPART(ww,[1_Webtime_By_Date].Date - Forms![Date Form].StartDate ) AS WK

How could i write this code in a more performant way?

In our app people have 1 or multiple projects. These projects have a start and an end date. People have a limited amount of available days.
Now we have a page that displays the availability of a given person on a week by week basis. It currently shows 18 weeks.
The way we currently calculate the available time for a given week is like this:
def days_available(query_date=Date.today)
days_engaged = projects.current.where("start_date < ? AND finish_date > ?", query_date, query_date).sum(:days_on_project)
available = days_total - hours_engaged
end
This means that to display the page descibed above the app will fire 18(!) queries into the database. We have pages that lists the availability of multiple people in a table. For these pages the amount of queries is quickly becomes staggering.
It is also quite slow.
How could we handle the availability retrieval in a more performant manner?
This is quite a common scenario when working with date ranges in an entity. Easy and fastest way is in SQL:
Join your events to a number generated date table (see generate days from date range) so that you have a row for each day a person or people are occupied. Once you have the data in this form it is simply a matter of grouping by the week date part of the date and counting the rows per grouping.
You can extend this to group by person for multiple person queries.
From a SQL point of view, I'd advise using a stored procedure and pass in your date/range requirement, you can then return a recordset for a user or possibly multiple users. This way your code just has to access db once.
You can then output recordset data in one go, by iterating through.
Hope this helps.
USE Stored procedure to fire your query to SQL to get data.
Pass paramerts in your case it is today's date to the SQl query.
Apply your conditions and Logic in the SQL Stored procedure , Using procedure is the goood and fastest way to retrieve data from the SQL , also it will prevent your code from the SQL injection too.
Call that SP from your Code as i dont know the Ruby on raisl I cant provide you steps about how to Call the Stored procedure from it.
After that the data fdetched as per you stored procedure will be available in Data table or something like that.
After getting the data you can perform all you need
Hope this helps
see what query is executed. further you may make comand explain to your query
explain select * from project where start_date < any_date and end_date> any_date2
you see the plan of query . Use this plan to optimized your query.
for example :
if you have index using field end_date replace a condition(end_date> any_date2 and start_date < any_date) . this step will using index if you have index on this field. But it step is db dependent . example is for nysql. if you want use index in mysql you must have using index condition on left part of where
There's not really enough information in your question to know exactly what you're trying to achieve here, e.g. the code snippet doesn't make use of the returned database query, so you could just remove it to make it faster. Perhaps this is just a bug in the code you posted?
Having said that, there are some techniques you should look into to implement your functionality.
I would take a look at using data warehouse techniques. I would think of your 'availability information' as a Fact table in a star schema, with 'Dates' and 'People' as Dimension tables.
You can then use queries to get stuff like - list of users for this projects for this week, and their availability.
Data warehousing has a whole bunch of resources you can tap into to help make this perform well, but there's also a lot of terminology that can be confusing, but for this type of 'I need to slice and dice my data across several sets of things (people and time)', Data Warehousing techniques can be quite powerful.
As I dont understand ruby on rails,from sql point of view i suggest you to write a stored procedure and return a dataset.And do the necessary table operations on the dataset from front end.It will reduce the unnecessary calls to DB.