Skip to content

how to sort by group and count element of array #54534

Closed

Description

@rfyiamcool

summary

I want to get output by element count of array 😅 I tried for a long time and didn't know how to write this sql. Please help me.

source data

select  * from app_log_local;

app=x1, number_keys = [k1]
app=x1, number_keys = [k1, k11]
app=x1, number_keys = [k1]

app=x2, number_keys= [k2]
app=x2, number_keys= [k2]

expected result:

app=x1, [k1=3, k11=1]
app=x2, [k2=2]

The following sql output results are out of order.

SELECT
    app,
    arraySort(x -> x, arrayZip((sumMap(number_keys, arrayResize(CAST([], 'Array(UInt64)'), length(number_keys), 20)) AS x).1, x.2)) AS res
FROM app_log_local
WHERE timestamp > '2023-09-12 08:00:00'
GROUP BY app

Query id: 1da30869-c3b2-4ee3-adc3-288d318d48f8

┌─app────────────────────────────────────┬─res─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ channelz                               │ [('bytes_received',8660),('bytes_sent',3660),('duration',3660),('response_code',3660)]                                                                                                                                                                     │
│ cafeteria-sandbox                      │ [('bytes_received',560),('bytes_sent',460),('duration',460),('response_code',860)]                                                                                                                                                                         │
│ meta                                   │ [('bytes_received',6100),('bytes_sent',6100),('duration',6100),('response_code',6100)]                                                                                                                                                                     │
│ order20-adapter                        │ [('at',5100),('bytes_received',11540),('bytes_sent',11540),('duration',11540),('httpCode',5100),('response_code',11540),('timeCost',5100),('ts',7640)]                                                                                                     │
│ gpt-code-manager                       │ [('bytes_received',5100),('bytes_sent',5100),('duration',5100),('response_code',5700)]                                                                                                                                                                     │
│ logistics21                            │ [('bytes_received',4220),('bytes_sent',4520),('duration',4520),('response_code',4020)]

Activity

  1. changed the title [-]how to count by group element of array[/-] [+]how to sort by group and count element of array[/+] on Sep 12, 2023
  2. UnamedRus commented on Sep 12, 2023

    @UnamedRus
    Contributor
     arraySort(x -> (x.2), arrayZip(untuple(sumMap(number_keys, replicate(1, number_keys))))) AS res
    
  3. den-crane commented on Sep 12, 2023

    @den-crane
    Contributor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions