After starting with XML columns as holders for key/value type of data I would like to know if PostgreSQL 'LIKE' and 'DISTINCT' could be used in such data what are my needs.
In previous question I post example table which covers needs of those topic too.
So, such query...
SELECT myindex, xpath('/setup/node()/text()', description) FROM temp1
gives wanted results by extracting all data from all nodes of stored XML's.
"{mydatabase,127.0.0.1,john,4424}"
"{herdatabase,127.0.0.1,saly,5432}"
Now I am try to filter results by value on key :
SELECT myindex, xpath('/setup/node()/text()', description) FROM temp1
WHERE (xpath('/setup/DBUSER/text()', description))::TEXT[] = '{saly}'::TEXT[];
And get expected result:
"{herdatabase,127.0.0.1,saly,5432}"
1) Here is problem that I don't know how to use LIKE instead of '='.
SELECT myindex, xpath('/setup/node()/text()', description) FROM temp1
WHERE (xpath('/setup/DBUSER/text()', description))::TEXT[] LIKE 'aly'::TEXT[];
2) Better to say I would like that my 'LIKE' search under all usable data, like this:
SELECT myindex, xpath('/setup/node()/text()', description) FROM temp1
WHERE (xpath('/setup/node()/text()', description))::TEXT[] LIKE 'aly'::TEXT[];
But that also don't work.
3) And to be more precise in real world here is need to filter usable data from XML with DISTINCT criteria since there will be a same data more times. Like this:
SELECT DISTINCT xpath('/setup/node()/text()', description) FROM temp1
WHERE (xpath('/setup/node()/text()', description))::TEXT[] LIKE 'aly'::TEXT[];
I have described functionality now but with messy data and text column type and to get more organized data I would like to switch to XML.
Please help to get answers as needed.
EDIT Postgres 9.3, windows 7. Data for this is here: PostgreSQL, using xml
This is my query:
SELECT LAST(myindex), LAST(description), content::text, LAST(content)
FROM (SELECT myindex, description,
(xpath('/setup/node()/text()', description)) AS content FROM temp1) AS alias
WHERE content::text ILIKE '%127%'
GROUP BY content::text;
By trying to make query by myself I see that is better to use group by than distinct.
It is a bit slow but workable.
And some experience thought, is those query OK?