Limit Access Query By Date - ms-access

I am trying to run a query in Access that would return all the records created in the current month and the previous month. Selecting the table and the fields is not the issue. In this particular query the field to limit the query is ReleaseDate and has the value of Date/Time. This query would have to take in account that the previous month may be in the previous year when the current month is January.

Use DateSerial() to determine the start and end of your target date range.
For example, the start of the previous month from today (4/19/2021) ...
? DateSerial(Year(Date()), Month(Date()) -1, 1)
3/1/2021
And the first of next month ...
? DateSerial(Year(Date()), Month(Date()) +1, 1)
5/1/2021
So you could apply those conditions to ReleaseDate in your query's WHERE clause like this ...
WHERE ReleaseDate >= DateSerial(Year(Date()), Month(Date()) -1, 1)
And ReleaseDate < DateSerial(Year(Date()), Month(Date()) +1, 1)
Note that DateSerial() will adjust the year part of its output date when you give it a month value less than 1 or greater than 12 ...
? DateSerial(2021, 0, 1)
12/1/2020

Related

In Case statement update date not greater than today date

Hi as per current statement i am updating the date in table as per below statement. I want to update the date that will be not more than today date. If the date will be more than today date then it will update with today date.
Update visiting
set OriginArrvDate
WHEN DATEPART(weekday,ORDDATE)=6 THEN DATEADD(dd,3,ORDDATE)
WHEN DATEPART(weekday,ORDDATE)=7 THEN DATEADD(dd,3,ORDDATE)
WHEN DATEPART(weekday,ORDDATE)=1 THEN DATEADD(dd,2,ORDDATE)
WHEN DATEPART(weekday,ORDDATE)=2 THEN DATEADD(dd,1,ORDDATE)
ELSE ORDDATE END
where
orddate>=CONVERT(DATE,DATEADD(dd,-100,getdate()))
In above script i am trying that when after calculating the date in ( DATEADD(dd,3,ORDDATE) ) if output will be greater than today date then it will be update with today date.
Example : ORDATE is '2021-09-26' so as per current condition ORDDATE will be 2021-09-29 but i want the result after calculating the date not more than today date. So the date will be ' 2021-09-27'
If the ORDATE calculation below today date then it will be fine and not change in date required.
Thanks for your reply
UPDATE : -
As per suggestion i have add data with Script on sqlfiddle . If you review the data for visiting table for Oid 1 and 5 . Both updated data for OriginArrvDate is more than today date. I want to update the OriginArrvDate with today date when new ordDate is more than current date.
I would use a CTE which makes it very easy to test your logic by applying a CASE expression (not a CASE statement) to the output of your existing CASE expression.
;WITH x AS
(
SELECT Oid, ORDDATE, OriginArrvDate,
CalcDate = CASE DATEPART(weekday, ORDDATE)
WHEN 6 THEN DATEADD(DAY, 3, ORDDATE)
WHEN 7 THEN DATEADD(DAY, 3, ORDDATE)
WHEN 1 THEN DATEADD(DAY, 2, ORDDATE)
WHEN 2 THEN DATEADD(DAY, 1, ORDDATE)
ELSE ORDDATE END
FROM dbo.visiting
WHERE orddate >= CONVERT(DATE,DATEADD(DAY, -100, getdate()))
AND OriginArrvDate IS NULL
)
--SELECT *,
/* -- */ UPDATE x SET
OriginArrvDate = CASE
WHEN CalcDate > GETDATE() THEN GETDATE()
ELSE CalcDate END
--FROM x;
Thanks for the fiddle, but I wrote a db<>fiddle here because I couldn't get SQLFiddle to properly show the update.
In addition to using DAY instead of dd I would also make sure to always specify a table's schema.

MySQL-Select start and end of week along with average of a column (week-by-week)

I am trying to get average of a column player_count week-by-week over the past 6 weeks. But the problem is that I also want the start and the end of the week date which corresponds to a specific average.
What I have tried:
SELECT AVG(player_count) as average,
updated_at,
updated_at + INTERVAL WEEKDAY(updated_at) + 7 DAY as EndDate
FROM `gtan_servers`
WHERE server_short_name = 'FiveRP'
GROUP BY WEEK(updated_at)
ORDER BY updated_at DESC
LIMIT 6
The updated_at column is intended to be taken as start of the week and EndDate is going to be taken as end of the week in which a specific average of the player count is provided.
But this query is not working correctly regarding the week dates. I can fetch the average yes but the week dates are not being fetched correctly. Any help would be highly appreciated.
You need an expression that truncates an arbitrary date to the first day of the week in which it occurs. That is, it returns 2017-05-21 (Sunday) if you give it 2017-05-24
This expression does that, assuming your weeks start on Sundays. Here's an explanation.
FROM_DAYS(TO_DAYS(datestamp) -MOD(TO_DAYS(datestamp) -1, 7))
Then you need to use that as a GROUP BY expression and a WHERE expression.
SELECT AVG(player_count) as average,
FROM_DAYS(TO_DAYS(updated_at) -MOD(TO_DAYS(updated_at) -1, 7)) week_beginning,
FROM_DAYS(TO_DAYS(updated_at) -MOD(TO_DAYS(updated_at) -1, 7)) + INTERVAL 6 DAY week_ending
FROM `gtan_servers`
WHERE server_short_name = 'FiveRP'
AND updated_at >= FROM_DAYS(TO_DAYS(NOW()) -MOD(TO_DAYS(NOW()) -1, 7)) - INTERVAL 6 WEEK
GROUP BY FROM_DAYS(TO_DAYS(updated_at) -MOD(TO_DAYS(updated_at) -1, 7))
ORDER BY 2 DESC
LIMIT 6
The WHERE automatically filters out records from your table that are too old for your report.
This query gets a little repetitive, but it works nicely.
You could create a stored function like this:
DELIMITER $$
DROP FUNCTION IF EXISTS TRUNC_SUNDAY$$
CREATE
FUNCTION TRUNC_SUNDAY(datestamp DATETIME)
RETURNS DATE DETERMINISTIC NO SQL
COMMENT 'returns preceding Sunday'
RETURN FROM_DAYS(TO_DAYS(datestamp) -MOD(TO_DAYS(datestamp) -1, 7))$$
DELIMITER ;
Then your query becomes more readable:
SELECT AVG(player_count) as average,
TRUNC_SUNDAY(updated_at) week_beginning,
TRUNC_SUNDAY(updated_at) + INTERVAL 6 DAY week_ending
FROM `gtan_servers`
WHERE server_short_name = 'FiveRP'
AND updated_at >= TRUNC_SUNDAY(NOW()) - INTERVAL 6 WEEK
GROUP BY TRUNC_SUNDAY(updated_at)
ORDER BY TRUNC_SUNDAY(updated_at) DESC
LIMIT 6
If your weeks start on Mondays change the -1 to a -2.

How to do YTD, MTD calculations based on the Today()

I am very new to SSRS, I have below scenario.
I have to calculate measures for YTD, MTD, Last 7 Days and Previous day based on the today(). I need to use these YTD, MTD, Last 7 days and Previous day variables in the calculations while writing the IIF syntax
For example:
sum(IIF(Year="this year is Year to Date", value, sales)
sum(IIF(CreatedDate="the last 30days values ",sales)
Can any one please tell me how to achieve this
Thanks
You can use the YEAR function to get the year of a date field. An IIF statement is used to evaluate an expression (in your case if the date is within the current year) then the desired result if the expression is true then the result if false. So your YTD formula could look like:
=SUM(IIF(YEAR(Fields!YourDateField.Value) = Year(TODAY()), Fields!Sales.Value, 0)
This reads: The sum of (if the year = current year then Sales else 0).
The last 30 days is similar but you would use the DATEADD function to figure what the date was 30 days ago:
=SUM(IIF(Fields!YourDateField.Value >= DATEADD("d", -30, TODAY()), Fields!Sales.Value, 0)
https://technet.microsoft.com/en-us/library/aa337194(v=sql.100).aspx

SQL for Selecting Current Month in a Date Field and then only if Current date in greater that a certain date

This sql with mysql db does not work:
SELECT *
FROM `house1_rent_2014`
WHERE MONTH( `FIRST_OF_MONTH` ) = MONTH( NOW() )
AND DATE_ADD(MONTH(`FIRST_OF_MONTH`,INTERVAL 15 DAY) <= CURDATE();
what I want to do is get all the current month vales from the column FIRST_OF_MONTH (that works OK by itself) and then add the additional test if it's a current month value of course in the date field and today's date is greater than the 15th of the current month then select that. In other words a field has 2014-09-01 (again, I only want date fields with the current month) if today is 9/15 or later then select it.
I'm getting mixed up w/syntax and perhaps proper logic. Grateful for help
The syntax you have is way more convoluted than it needs to be, and I think the specification is overly confusing too.
"get all the current month vales from the column FIRST_OF_MONTH"
Did you want to return values from September of last year too? (The MONTH() function only returns the month value (1 thru 12), and doesn't include the year, which seems a bit odd.
To get rows for the "current month", we'd typically do something like this:
WHERE t.first_of_month >= DATE_FORMAT(NOW(),'%Y-%m-01')
AND t.first_of_month < DATE_FORMAT(NOW(),'%Y-%m-01') + INTERVAL 1 MONTH
Predicates on a "bare column" allow MySQL to make effective use of an index, in this case, a range scan of an index with leading column of first_of_month. If we wrap those columns in a function, then MySQL has to evaluate the function on every row on the table.
"then add the additional test if it's a current month value of course in the date field and today's date is greater than the 15th of the current month then select that."
You already have a predicate that checks for dates in the "current month", there's no need to repeat that.
This predicate will evaluate to TRUE if the current date is after the 15th, and will return FALSE if the current date is the 15th or earlier.
AND DATE(NOW()) > DATE_FORMAT(NOW(),'%Y-%m-15')
Examples
If we have these rows in the table:
row first_of_month
--- --------------
1 2014-09-30
2 2014-10-02
3 2014-10-14
4 2014-10-16
5 2014-10-31
6 2014-11-01
If we run a query with these predicates:
WHERE t.first_of_month >= DATE_FORMAT(NOW(),'%Y-%m-01')
AND t.first_of_month < DATE_FORMAT(NOW(),'%Y-%m-01') + INTERVAL 1 MONTH
AND DATE(NOW()) > DATE_FORMAT(NOW(),'%Y-%m-15')
If DATE(NOW()) returns '2014-10-11', then the third predicate will evaluate to FALSE, and no rows will be returned.
If DATE(NOW()) returns '2014-10-16', then all of the rows with first_of_month in the current month are returned, that is, rows 2 thru 5 in the example data.
If DATE(NOW()) returns '2014-11-07', then third predicate is false, and no rows are returned.
If DATE(NOW()) returns '2014-09-25', the query will return row 1 in the example data.

MySQL - How do I get a range from the point closest to start?

Table structure-
int PrimaryKey
DateTime Date
int Price
What I would like to do is pass in a start and end date, and get the range
From:
The greatest date that is less than or equal to start UNLESS there is no such date, in which case the start of the range should be the lowest date available.
To:
The end date.
My query so far is:
SELECT Date, Price
FROM table1
WHERE Date <= $end
AND Date >=
(
SELECT Date
FROM table1
WHERE Date <= $start
ORDER BY Date DESC
LIMIT 1
)
The subquery gets the largest date that is less than or equal to the start value, which is then used as the low end of the range for the main query. Any suggestions for getting the 'UNLESS' clause in there?
For example, given a table with dates:
Jan 1, 2009
Feb 1, 2009
Mar 1, 2009
Apr 1, 2009
I want a query with $start='Feb 18, 2009' and $end='Apr 30, 2009' to return the three rows Feb 1, Mar 1, Apr 1 (Feb 1, 2009 is the largest date that is less than or equal to $start).
But if I provide $start='Dec 1, 2009' and $end='Apr 30, 2009' I want all four rows returned (There is no date that is less than or equal to $start, so the range starts at the lowest date)
you don't need the "UNLESS there is no such date, in which case the start of the range should be the lowest date available" logic because if you simply select all dates after $start in the case of there not being a previous date, you automatically get the next latest date anyway. and you already know there are no records between $start and the next latest date, so don't bother trying to exclude them!
WHERE Date BETWEEN coalesce((select max(date)
from table1
where date <= $start)
, $start) and $end
Why don't you compare it with the $start date?
SELECT Date, Price
FROM table1
WHERE Date <= $end
AND Date >=$start
I've not tested it but it seems to work for me (if i've understood what you want to do)