Re: [GENERAL] Hierarchical Query Question (PHP)

2015-10-30 Thread Jason O'Donnell
David,

Does wrapping the transaction with BEGIN; COMMIT; work as you would expect?


$sql = "BEGIN; with recursive hier(taxon,parent_id) as (
  select m.taxon, null::integer
  from gz_life_mammals m
  where taxon='Mammalia' --<< substitute me
  union all
  select m.taxon, m.parent_id
  from hier, gz_life_mammals m
  where m.parent=hier.taxon
)
select tax_rank(parent_id),
   count(*) num_of_desc
from   hier
where  parent_id is not null
group by parent_id
order by parent_id; COMMIT;";

On Thu, Oct 29, 2015 at 8:18 PM, David Blomstrom 
wrote:

> Can anyone tell me how to write the query described @
> http://stackoverflow.com/questions/33402831/count-descendants-in-hierarchical-query
> ?
>
> The answer's very thorough, but I don't know how to string two queries and
> a function together like that. This doesn't work:
>
> $sql = "select * from gz_life_mammals;";
>
> create function tax_rank(id integer) returns text as $$
> select case id
>  when 1 then 'Classes'
>  when 2 then 'Orders'
>  when 3 then 'Families'
>  when 4 then 'Genera'
>  when 5 then 'Species'
>end;
> $$ language sql;
>
> $sql = "with recursive hier(taxon,parent_id) as (
>   select m.taxon, null::integer
>   from gz_life_mammals m
>   where taxon='Mammalia' --<< substitute me
>   union all
>   select m.taxon, m.parent_id
>   from hier, gz_life_mammals m
>   where m.parent=hier.taxon
> )
> select tax_rank(parent_id),
>count(*) num_of_desc
> from   hier
> where  parent_id is not null
> group by parent_id
> order by parent_id;";
>
> Thanks.
>
>


-- 
Jason O'Donnell
Crunchy Data Solutions


Re: [GENERAL] How to search a string inside a json structure

2015-11-02 Thread Jason O'Donnell
Sami,

What version of postgres are you using?

There's some examples using GIN indexes for searching jsonb objects in the
wiki:
https://wiki.postgresql.org/wiki/What's_new_in_PostgreSQL_9.4#JSONB_Binary_JSON_storage

Hope that helps,

On Mon, Nov 2, 2015 at 4:09 PM, Sami Pietilä  wrote:

> Hi,
>
> I have a database with jsonb type of columns. Colums contain complex json
> structures. I would like to get all rows which contain a json where any of
> json's values matches to a given string (like %hello%).
>
> How to create a postgre sql query to do this?
>
> I guess postgre should traverse though each json structures while finding
> the string.
>
> Thanks
>



-- 
Jason O'Donnell
Crunchy Data Solutions