0

How to convert the Oracle code below to BigQuery?

max(to_date(BIC_GM_AGCPOAODS00_BO_VW.BOUND_DATE,'yyyymmdd'))

When I try this code:

SELECT A._BIC_GCISBN,
       max(cast(A.BOUND_DATE as date),'yyyymmdd')
FROM `BIC_GM_AGCPOAODS00_BO_VW`  A
WHERE A._BIC_ZC2GRIRIN  = 'G' AND A._BIC_ZCLOEKZ  = ' '  
GROUP BY A._BIC_GCISBN

I am get the error:

No matching signature for aggregate function MAX for argument types: DATE, STRING. Supported signature: MAX(ANY) at [15:2]

4
  • 2
    Why are you storing DATE values as varchar? That's a really bad idea Commented Aug 22, 2019 at 7:11
  • I have no idea on how to store. Please help me with solution Commented Aug 22, 2019 at 7:13
  • @Tejash Can anyone help me please Commented Aug 22, 2019 at 11:22
  • @HereGoes Can anyone help me Commented Aug 22, 2019 at 11:23

1 Answer 1

1

In BigQuery, you want parse_date():

SELECT A._BIC_GCISBN,
       MAX(PARSE_DATE('%Y%m%d', A.BOUND_DATE))
FROM `BIC_GM_AGCPOAODS00_BO_VW`A
WHERE A._BIC_ZC2GRIRIN  = 'G' AND A._BIC_ZCLOEKZ  = ' '  
GROUP BY A._BIC_GCISBN;

You should be storing the value using date, but sometimes we don't have control over how data is stored.

EDIT:

Given your data format, you can also write this as:

SELECT A._BIC_GCISBN,
       PARSE_DATE('%Y%m%d', MAX(A.BOUND_DATE))
FROM `BIC_GM_AGCPOAODS00_BO_VW`A
WHERE A._BIC_ZC2GRIRIN  = 'G' AND A._BIC_ZCLOEKZ  = ' '  
GROUP BY A._BIC_GCISBN;

I prefer the first version because it generalizes readily to any data format. This might be more efficient.

Sign up to request clarification or add additional context in comments.

6 Comments

do you think that using PARSE_DATE('%Y%m%d', MAX(A.BOUND_DATE)) instead will make a difference in terms of performance / resources?
@MikhailBerlyant . . . Given the overhead inherent in a BQ query, I would be surprised if any difference were noticeable. The question is whether max on a string with 8 characters (10 bytes) is faster or slower than conversion to 8 bytes with a presumed binary format. I don't know. But, I do know that parsing first is more general. It will work regardless of the date format.
i will try to "research" on this - will share result if any :o) we are moving from on-demand to flat-rate , so my interest now is not just performance wise but slots wise. what the difference "slots consumption" wise when you run such queries on big data scale
@MikhailBerlyant . . . I would be surprised if a scalar function like this would affect slots. Cost-based optimizers do not usually take scalar functions into account; but then again, there are not many databases that do dynamic partitioning. Perhaps Google will be smart about this. We are also moving to slots, so I supposed I'm interested in the answer.
Just to clarify - this was not a criticism about this answer! Not at all! Meantime, just did very quick test - on average for relatively simple query with around 1 sec execution duration: MAX(DATE()) consumes twice more slots vs. DATE(MAX()) - around 1 slot during that 1 sec vs 0.5 slots - which is also consistent with execution plan. Obviously absolutely negligible difference for single query. Trying to understand how this type of "differences" can affect effectiveness of flat rate usage on massive scale. most likely not much - but want to have better insight on this ...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.