I am trying to calculate the difference between the Maxtime and Mintime however, I think the issue is with converting the 'timestamp' to a date that I can take the difference of. Any help in creating a column that takes the difference of days between these two columns would be so helpful! Thanks!!!
SELECT user,
Min(convert(timestamp,"%m/%d/%Y")) as Mintime,
Max(convert(timestamp,"%m/%d/%Y")) as Maxtime,
( Min(timestamp)-Max(timestamp)) AS Expr1
FROM table1
GROUP BY user
There is no convert function in Access. So you are getting the error.
If you want to convert timestamp to a date you can use the datevalue function in Access.
Related
I am trying to use HAVING after a GROUP BY but I get this error:
SELECT list expression references sales_order.SalesOrderDateTime
which is neither grouped nor aggregated at [2:8]
The field is in the SELECT list, and GROUP BY, but it is part of a CAST because I do not want the timestamp part of the datetime. It works without the CAST on another table where the format is already YYYY-MM-DD. It really should work here as well. Is there a way around this?
I tried using STRING FORMAT 'YYYY-MM-DD' but that does not work either.
SELECT
CAST(sales_order.SalesOrderDateTime AS DATE) as created_at,
sales_order.SalesOrderNo as order_id,
sales_order.SellToEmail as customer_email,
sum(sales_order_item.OriginalPrice)*100 as total_amount_cents,
max(sales_order.TotalDiscountAmount)*100 as discount_amount_cents
FROM `my_dw.external_datamart_1.SalesOrder_view`
WHERE CAST(sales_order.SalesOrderDateTime AS DATE) >= '2022-01-01'
GROUP BY CAST(sales_order.SalesOrderDateTime AS DATE), sales_order.SalesOrderNo, sales_order.SellToEmail
-- This does not work with CAST
HAVING sum(sales_order_item.OriginalPrice) > max(sales_order.TotalDiscountAmount)
LIMIT 1000
)
A CTE works of course, but I would like to avoid that if possible.
You can try this:
group by 1,2,3
instead of using full expressions.
I face the same issue time to time in my complex queries. I think it's a bug and can be reported here https://issuetracker.google.com/
I was wondering if there is a way to find the 'median' date in PostgreSQL. The goal is to obtain, according to the median, the date that is in the middle of all the dates.
I tried following modified median function:
select
percentile_cont(0.5) within group (order by date)
from cte
By trying to do so I get the following error message:
SQL Error [42883]: ERROR: function percentile_cont(numeric, timestamp without time zone) does not exist
Hint: No function matches the given name and argument types. You might need to add explicit type casts.
Position: 12
As dates are not supported, I was wondering if there is another way to calculate the median value of a date.
Thank you for any inputs!
You can cast the date value to an integer and then use it for getting the median value using the percentile_cont function.
Like so,
SELECT
percentile_cont(0.5) within group (ORDER by cast(extract(epoch from dateCol1) as integer))
FROM table1
The above gives the median date, but in numeric value, to convert it back to a date type, use the to_timestamp function like so ,
select to_timestamp(1638662400)::date
#gives 2021-12-05
I'm trying to generate a query that will display the date difference between a date stored in a column and the current date, but i'm getting the error:
"Cannot call method "getTime" of null."
What function can I use to calculate this date difference in Google Cloud SQL?
Current code:
SELECT date, DATEDIFF(date, CURRENT_DATE()) AS daysLeft
FROM table;
Are you using a mysql instance on Google Cloud. If so try the following
SELECT date, DATEDIFF(date, CURDATE()) AS daysLeft
FROM table;
All that changed is CURDATE() instead of CURRENT_DATE()
Turns out, the error was in another part of the code. The originally posted SQL query works perfectly fine, the problem was the model was casting the result as a date variable instead of an int.
The DATEDIFF function requires three parameters.
SELECT date, DATEDIFF(day, date, CURRENT_DATE()) AS daysLeft
FROM table;
This should return your result in days left.
The query I'm running as a test is:
SELECT
UNIX_DATE(created_utc)
FROM `fh-bigquery.reddit_comments.2017_08`
But I keep getting this error:
Error: No matching signature for function UNIX_DATE for argument types:
INT64. Supported signature: UNIX_DATE(DATE) at [2:3]
I checked the datatype for the created_utc field and it's an integer. Casting and whatnot won't work either.
Would really appreciate any help. Thanks!
You should use TIMESTAMP_SECONDS() instead
#standardSQL
SELECT
TIMESTAMP_SECONDS(created_utc)
FROM `fh-bigquery.reddit_comments.2017_08`
LIMIT 5
Then you can use DATE() if you need date only
DATE(TIMESTAMP_SECONDS(created_utc))
UNIX_DATE() takes a String.
And DATE_FROM_UNIX_DATE() takes an INT64. SQL has a legacy problem of thinking of time ("date") in DAYS and not SECONDS like Unix. Thus:
SELECT DATE_FROM_UNIX_DATE(CAST(created_utc/86400 as INT64))
FROM `fh-bigquery.reddit_comments.2017_08`
I'm trying to create a custom query in Tableau to use on Google's BigQuery. The goal is to have an offset parameter in Tableau that changes the offsets used in a date based WHERE clause.
In Tableau it would look like this:
SELECT
DATE_ADD(UTC_USEC_TO_MONTH(CURRENT_DATE()),<Parameters.Offset>-1,"MONTH") as month_index,
COUNT(DISTINCT user_id, 1000000) as distinct_count
FROM
[Orders]
WHERE
order_date >= DATE_ADD(UTC_USEC_TO_MONTH(CURRENT_DATE()),<Parameters.Offset>-12,"MONTH")
AND
order_date < DATE_ADD(UTC_USEC_TO_MONTH(CURRENT_DATE()),<Parameters.Offset>-1,"MONTH")
However, BigQuery always returns an error:
Error: DATE_ADD 2nd argument must have INT32 type.
When I try the same query in the BigQuery editor using simple arithmetic it fails with the same error.
SELECT
DATE_ADD(UTC_USEC_TO_MONTH(CURRENT_DATE()),5-3,"MONTH") as month_index,
FROM [Orders]
Any workaround for this? My only option so far is to make multiple offsets in Tableau, it seems.
Thanks for the help!
I acknowledge that this is a hole in functionality of DATE_ADD. It can be fixed, but it will take some time until fix is rolled into production.
Here is a possible workaround. It seems to work if the first argument to DATE_ADD is a string. Then you can truncate the result to a month boundary and convert it from a timestamp to a string.
SELECT
FORMAT_UTC_USEC(UTC_USEC_TO_MONTH(DATE_ADD(CURRENT_DATE(),5-3,"MONTH"))) as month_index;