PostgreSQL
 sql >> база данни >  >> RDS >> PostgreSQL

Postgres SQL заявка за обобщаване на данни за грешки

Надявам се да разбера вашата структурна заявка и таблица и заявката по-долу да ви помогнат.

Можете да видите моята структурна таблица и резултат от заявката:dbfiddle

Без агрегат:

with allow_category as (select unnest(array [
    'cpt',
    'drg',
    'modifiers',
    'secondaryDiagnosis',
    'principalDiagnosis',
    'secondaryProcedure',
    'observationhours',
    'dischargeStatus',
    'principalprocedure',
    'hcpcs',
    'professionalEvaluationManagement'
    ]::text[]) category)
select distinct ctextid,
                vbillid,
                initcap(kcy.key)                                                   as typeofcorrection,
                initcap(regexp_replace(kc.key, '([a-z])([A-Z])', '\1 \2', 'g'))    as errorpara,
                case when f ? 'from' then f -> 'from' ->> 'code' else '' end       as oldvalue,
                case when f ? 'to' then f -> 'to' ->> 'code' else f ->> 'code' end as newvalue
from qareport q
         cross join jsonb_each(q.codemap) as kc
         cross join jsonb_each(kc.value) as kcy
         join jsonb_array_elements(kcy.value) f on true
where kc.key in (select * from allow_category)
  and kcy.key in ('added', 'revised', 'removed')
  and (
        jsonb_array_length(kc.value -> 'added') > 0
        or jsonb_array_length(kc.value -> 'revised') > 0
        or jsonb_array_length(kc.value -> 'removed') > 0
    );

С агрегат:

with allow_category as (select unnest(array [
    'cpt',
    'drg',
    'modifiers',
    'secondaryDiagnosis',
    'principalDiagnosis',
    'secondaryProcedure',
    'observationhours',
    'dischargeStatus',
    'principalprocedure',
    'hcpcs',
    'professionalEvaluationManagement'
    ]::text[]) category)
select distinct ctextid,
                vbillid,
                initcap(kcy.key)                                                   as typeofcorrection,
                initcap(regexp_replace(kc.key, '([a-z])([A-Z])', '\1 \2', 'g'))    as errorpara,
                case when f ? 'from' then f -> 'from' ->> 'code' else '' end       as oldvalue,
                case
                    when f ? 'to' then f -> 'to' ->> 'code'
                    else string_agg(f ->> 'code', ',')
                         over (partition by ctextid, vbillid, kcy.key, kc.key) end as newvalue
from qareport q
         cross join jsonb_each(q.codemap) as kc
         cross join jsonb_each(kc.value) as kcy
         join jsonb_array_elements(kcy.value) f on true
where kc.key in (select * from allow_category)
  and kcy.key in ('added', 'revised', 'removed')
  and (
        jsonb_array_length(kc.value -> 'added') > 0
        or jsonb_array_length(kc.value -> 'revised') > 0
        or jsonb_array_length(kc.value -> 'removed') > 0
    );



  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Как да добавя 2 точки с разстояние между тях (SRID =32636)?

  2. Какъв е еквивалентът на MySQL на EXPLAIN ANALYZE на PostgreSQL

  3. Postgres пренаписва ли целия ред при актуализация?

  4. Как работи clock_timestamp() в PostgreSQL

  5. Asp.net MVC4, PostgreSQL и SimpleMembership