SOQL query WHERE Date = 30_days_ago? - salesforce

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.

Related

Calculate the duration between two dates based on holiday from calendar table

I have two tables : Calendar and Request_Stages:
I want to calculate the duration between DATE)FROM and DATE_TO for each request stage.
The desired result:
What I have tried:
SELECT Req.requestID, Req.STAGE_ID COUNT(DAT.holiday) as duration
FROM REQUEST Req
JOIN [dbo].[STG_ACCR_DATE] DAT
ON DAT.DATE >= Req.DATE_FROM
AND DAT.FDATE <= Req.DATE_TO
WHERE DAT.OFF_DAY = 0 --TO CALCULATE ONLY WORKING DAYS
GROUP BY Req.request_ID, Req.STAGE_ID
ORDER BY Req.request_ID, Req.STAGE_ID
The problem with my current result:
it doesn't SHOW the stages with zero working day, for example if a stage start date and end date are equal, the desired result is one '1' working day, but my query is returning zero '0' and doesn't show it in the results with this issue, stages records are lost.
Any suggestion to fix my query or new solution idea are appreciate it, probably I am thinking wrong, so any solution is welcome.
The posted query doesn't really match up with the problem as presented.
select *,
datediff(day, DATE_FROM, DATE_TO) + 1 - (
select count(*) from CALENDAR as c
where c.DATE between rs.DATE_FROM and rs.DATE_TO and c.IS_HOLIDAY = 1
) as DURATION
from REQUEST_STAGES as rs

How to use the datebucket filter

Trying to use the :datebucket filter but it doesn't seem to work.
select date, address from database.table where address = 'xyz' group by :datebucket(date)
This returns the error that date isn't in the group by statement, but it is. If it add it separately to the group by statement, it just groups by the individual date instead of respecting the date bucket selection.
Not finding anything in the Snowflake documentation about how this filter is suppose to work, just that it exists.
In this site: https://www.webagesolutions.com/blog/querying-data-in-snowflake was example like this about databucket function
SELECT COUNT(ORDER_DATE) as COUNT_ORDER_DATE, ORDER_DATE
FROM ORDERS
GROUP BY :datebucket(ORDER_DATE), ORDER_DATE
ORDER BY COUNT_ORDER_DATE DESC;
So could your query work if it was modified like this:
SELECT
date,
address
FROM
database.table
WHERE
address = 'xyz'
GROUP BY :datebucket(date), date
Datebucket is truncating the date, to buckets. But you have selected the raw date.
This is like grouping by decade '60,'70,'80 of what great years, but want the actual year.
SELECT column1 as year,
truncate(year,-1) as decade
FROM VALUES (1),(2),(3),(14),(15),(16),(27),(28),(29);
gives:
YEAR
DECADE
1
0
2
0
3
0
14
10
15
10
16
10
27
20
28
20
29
20
so if I try select
SELECT column1 as year
FROM VALUES (1),(2),(3),(14),(15),(16),(27),(28),(29)
GROUP BY truncate(year,-1)
ORDER BY 1;
gives the error
Error: 'VALUES.COLUMN1' in select clause is neither an aggregate nor in the group by clause. (line 15)
So if we move the decade into the selection, it makes sense:
SELECT truncate(column1,-1) as decade
FROM VALUES (1),(2),(3),(14),(15),(16),(27),(28),(29)
GROUP BY decade
ORDER BY 1;
and we get the
DECADE
0
10
20
So the problem is not :datebucket(date) but the fact while :datebucket(date) and date are related, from the perspective of GROUPING they are unrelated.
I've been trying to use datebucket(date) and daterange, and I also needed the results in a Snowflake graph.
It was a bit trick, because the value returned by datebucket(date) is actually a truncated date based on the selected date part. For that, I had to convert it to a char, and it worked!
select
to_char(:datebucket(start_time), 'YYYY.MM.DD # HH24') as start_time_bucket,
sum(credits_used) as credits_used
from snowflake.account_usage.warehouse_metering_history wmh
where
start_time = :daterange
group by :datebucket(start_time)
And if you're an ACCOUNTADMIN, you can now use the query to get the total credits usage by date :)
Last, to answer the main query by Tony, the query should be:
select date, address
from database.table
where address = 'xyz'
group by :datebucket(date), date, address
// or
select :datebucket(date), address
from database.table
where address = 'xyz'
group by :datebucket(date), address
Try adding the :datebucket(date) in the select part as well (not only in group by). Also, you will probably need an aggregate function for the field address (for example any_value(address):
select :datebucket(date), any_value(address)
from database.table
where address = 'xyz'
group by :datebucket(date)

How to do WHERE <before> an aggregate function (Postgres)

It's hard to explain from the title, but this is my SQL:
SELECT
SUM("payments"."amount"),
"invoices"."property_id"
FROM "payments"
JOIN "invoices"
ON "payments"."invoice_id" = "invoices"."id"
GROUP BY "property_id"
It returns the sum of all Payment records (amount column) for a particular Property (which is connected through it's invoices).
In other words:
Property has_many: :invoices
Invoice has_one: :payment
I'm trying to select payments between a particular date range though, but it has to happen "before" the aggregate function (so do the exact query above, but only for 2017-01-01 through 2017-02-01). The field would be generated_at on Payment
You are looking for a WHERE clause. (WHERE is executed before aggregation; HAVING is executed after.) Suggested date literals in PostgreSQL are ANSI standard DATE 'YYYY-MM-DD'. Date ranges are usually checked with >= start day and < end day + 1 (in order to deal properly with the time part if any).
SELECT
SUM(p.amount),
i.property_id
FROM payments p
JOIN invoices i ON p.invoice_id = i.id
WHERE p.generated_at >= DATE '2017-01-01'
AND p.generated_at < DATE '2017-02-02'
GROUP BY i.property_id;

Order by difference of days

I'm doing a simple query, a select on a table with ID, name and date of birth. I would like to sort of low to high depending on the days that there difference between now and the date of birth of the table. Any ideas? I'm new to SQL and I can not think how.
Table User
SELECT * FROM User ORDER BY
you can use like this
select * from user order by datediff(d,dateofbirth, getdate())

How to compare timestamp in sql server

Given the following table, how can I select the most recent timestamp from a given user?
Here is my table:
For instance, if USERID 5, I want to compare every USERID 5 timestamp and return the most recent timestamp.
If you want the latest timestamp for a user you can use the max function:
select userid, max(checktime)
from your_table
group by userid
where userid = 5
If you remove the where clause you'll get a list of all userid with their latest checktime.
Not sure if I understand correctly?
Maybe it is just as simple as that?
SELECT MAX(CHECKTIME) FROM Table WHERE UserID = 5
If not, you could google for OVER clause.

Resources