Пользователь пока ничего не рассказал о себе

Наибольший вклад в теги

Все теги (4)

Лучшие ответы пользователя

Все ответы (4)
  • Как с помощью SQL запроса выбрать из строки значения и посчитать их уникальное количество?

    @DKSoft
    Можно попробовать так
    with a as  #преобоазуем в json массив
    (select id,concat('[',replace(descr,'}{','},{'),']') as d1
     from x1
    )
    ,b as ( # массив в таблицу
    select id,JSON_EXTRACT(d1, '$[1]') as d2
     from a
    union all
    select id,JSON_EXTRACT(d1, '$[2]') as d2
     from a
    union all
    select id,JSON_EXTRACT(d1, '$[3]') as d2
     from a
    union all
    select id,JSON_EXTRACT(d1, '$[4]') as d2
     from a
    union all
    select id,JSON_EXTRACT(d1, '$[5]') as d2
     from a
    union all
    select id,JSON_EXTRACT(d1, '$[6]') as d2
     from a
    union all
    select id,JSON_EXTRACT(d1, '$[7]') as d2
     from a
    union all
    select id,JSON_EXTRACT(d1, '$[8]') as d2
     from a
    union all
    select id,JSON_EXTRACT(d1, '$[9]') as d2
     from a
    union all
    select id,JSON_EXTRACT(d1, '$[10]') as d2
     from a
    )
    -- подсчет
    select id,count(distinct JSON_VALUE(d2,'$.DeviceID')) as count
    from b
    where b.d2 is not null
    group by id

    если в поле descr больше 10 элементиов то надо добавить в b допролнительные union all
    Ответ написан
    Комментировать