Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

query column with $partition

I have a table that contains details about some other partitioned tables as below:

Row    user_email      table_id           job_type
1     jack@test.com   product$20230310   Query
2     john@test.com   item$20230309      Query
3     jim@test.com    product$20230211   Query
4     jack@test.com   product$20220105   Query
5     Alex@test.com   item$20230310      Query

I would like to group by user_email and table_id but as partitions are different it will generate one row for each table/partition.

My query:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

SELECT
  user_email,
  table_id,
  COUNT(*) AS total
FROM
  TABLE
GROUP BY
  user_email,
  table_id

Current results:

Row    user_email      table_id           total
1     jack@test.com   product$20230310   1
2     john@test.com   item$20230309      1
3     jim@test.com    product$20230211   1
4     jack@test.com   product$20220105   1
5     Alex@test.com   item$20230310      1

Desired output:

Row    user_email      table_id           total
1     jack@test.com   product            2
2     john@test.com   item               1
3     jim@test.com    product            1
5     Alex@test.com   item               1

>Solution :

as you onoly want the first part of the string, you can use SPLT

but better would be not to save delimitated columns, why you need the values,. as every split take time and resource that you could spent elsewhere better

SELECT
  user_email,
  SPLIT(table_id, '$')[OFFSET(0)]
,
  COUNT(*) AS total
FROM
  TABLE
GROUP BY
  user_email,
  SPLIT(table_id, '$')[OFFSET(0)]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading