I want to query for records of a month, except that month's last year.
my soql query is this
select id,name from opportunity where CreatedDate=THIS_MONTH And WEEK_IN_MONTH IN (1,2,3)
soql query to get the whole month, except its last week.
Yoou tagged it apex so can you cheat?
Date today = System.today();
Integer cutoff = Date.daysInMonth(today.year(), today.month()) - 7;
System.debug([SELECT Id
FROM Opportunity
WHERE CreatedDate=THIS_MONTH AND DAY_IN_MONTH(CreatedDate) < :cutoff]);
Related
I am trying to create a Attendance result set in SQL Server for using it in a SSRS report. The Employee Attendance table is as below:
EmpId
ADate
In
Out
1
2023-01-01
8:00
15:00
I need to calculate the Total working days for all months in a year and display the number of working days per employee. Report format should be as follows:
Saturday and Sunday being weekend, I can able to get the no of working days monthly.
Another table tbl_Holiday has entries for holidays Fromdate and ToDate. I need to consider that also when calculating working days. Several number of results i got from the internet for calculating this. But when creating a view using this data , it has to calculate workdays for each employee row
SELECT
EmpName, EmpId,
(SELECT COUNT(*) FROM tbl_EmpAttendance
WHERE EmpRecId = A.RecId
GROUP BY MONTH(Adate), YEAR(Adate)) AS WorkedDays,
dbo.fn_GetWorkDays(DATEFROMPARTS(YEAR(ADate), MONTH(ADate), 1), EOMONTH(ADate)) AS workingDays
FROM
tbl_Employee A
LEFT JOIN
tbl_EmpAttendance B ON A.RecId = B.EmpRecId
fn_GetWorkDays - calculates the working days for month.
I need to get number of holidays from tbl_holiday too, I understand that this query is becoming more complex than I thought. There must be simpler way to achieve this, can anyone please help?
I tried to get the result in one single view, for using that as SSRS report dataset
I have this query:
SELECT * FROM table1 WHERE YEAR(datecolumn) = YEAR(DATEADD(YEAR,-1,GETDATE()))
From my understanding, this query will show everything in that table that is older than 1 year. I'm trying to get a list of items that were first stocked over a year ago. Is this the correct query to be running or could it be made easier?
I need the script to be able to run without a fixed date range. So say I run it today and it gives me 100 rows, if I run it tomorrow, because the script is looking at the date I run it, it might return a different result set.
That query gives results for records of only the previous year.
To get records older than (today - 1 year) try this
SELECT *
FROM table1
WHERE datecolumn < DATEADD(YEAR,-1,CAST(GETDATE() AS DATE))
And to get records before the current year
SELECT *
FROM table1
WHERE datecolumn < DATEFROMPARTS(YEAR(GETDATE()),1,1)
This will give you everything from last "year"
SELECT *
FROM table1
WHERE YEAR(datecolumn) < YEAR(GETDATE())
But you want it to change day by date. In that case do this
SELECT *
FROM table1
WHERE datecolumn < DATEADD(YEAR,-1,GETDATE())
Is there a way I can customize this SOQL query to include all records after 6am from Today?
SELECT Case__c, Level_1__c, Level_2__c,Level_3__c
FROM Case_Type__c
WHERE createddate = today
GROUP BY Case__c,Level_1__c, Level_2__c,Level_3__c
I need to do this within the SOQL query.
Use HOUR_IN_DAY() from date functions
Something like:
WHERE CreatedDate = today AND HOUR_IN_DAY(CreatedDate) > 5
I have a table with a set of business dates I need to select the max date per month and year tried using the last_day function but that returns the last day not the max date of that month.please help me out.
MAX is an aggregate function, so you need to figure out how to group all of the days of the month together. The easiest way to do that is apply a function that will return the same value for every day in that month. LAST_DAY would work, but I prefer TRUNC (with 'MM' specified).
SELECT MAX(your_column) FROM your_table GROUP BY TRUNC(your_column, 'MM')
How do I make a SOQL query like this?
SELECT id FROM Account WHERE LastActivityDate = 30_DAYS_AGO
This produces an error:
MALFORMED_QUERY:
Account WHERE LastActivityDate = 30_DAYS_AGO
^
SELECT id FROM Account WHERE LastActivityDate = LAST_N_DAYS:30
As you're doing this from apex, you can calculate the date in apex, then bind that into your query, e.g.
date d = system.today().addDays(-30);
Account [] acc= [select id from account where createdDate = :d];
Select Id from Account Where LastActivityDate = N_DAYS_AGO:30
LAST_WEEK and LAST_MONTH are also easy and work well.
SELECT id FROM Account WHERE LastActivityDate > LAST_MONTH
For more data look at this link: http://www.salesforce.com/us/developer/docs/officetoolkit/Content/sforce_api_calls_soql_select_dateformats.htm
The page of SOQL date functions appears to have moved here: https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_select_dateformats.htm
To clarify, SOQL allows a date field (e.g. LastActivityDate) to be compared against a range of dates using a comparison operator. So "LastActivityDate = LAST_MONTH" is equivalent to saying that the date is greater than or equal to the start of the first day of the previous month AND less than or equal to the end of the last day.
Since it is 30 days ago, you an use this -
SELECT ID FROM Account WHERE LastActivityDate < LAST_N_DAYS:30
Your query time period lies in the Date literals provided SFDC its best to use
it as the time specified is a broad number , , you just need to provide the no of days and accordingly use the operator which is '=' ,'>' or '<'
LAST_N_DAYS:n LAST_N_WEEKS:n LAST_N_MONTHS:n LAST_N_YEAR:n
NEXT_N_DAYS:n NEXT_N_WEEKS:n NEXT_N_MONTHS:n NEXT_N_YEAR:n
your query would look simpler if you just provided the no of days / month which
it falls in .
SELECT id FROM Account WHERE LastActivityDate = LAST_N_MONTHS:1
or
SELECT id FROM Account WHERE LastActivityDate = LAST_N_DAYS:30
Thanks,
OQ.