Get all column entries for each unique id in a table - sql

I have a table structured something like this:
profile | tag
-------------------------
p1 | tag1
p1 | tag2
p1 | tag3
p2 | tag1
p2 | tag3
I want to run a query that returns something like:
tag1, tag2, tag3
tag1, tag3
Basically, for each unique profile, a list of the tags that exist in the table.
Is that possible, or do I need to have a unique column in my result for each tag I care about?
Thanks!

This returns what you want:
SELECT array_agg(tag)
FROM table1
GROUP BY profile
Or :
SELECT string_agg(tag, ',')
FROM table1
GROUP BY profile
string_agg let's you specify what delimiter you want and ORDER the results in each row.
SQLFIDDLE DEMO

Related

How can I aggregate values inside a jsonb array using SQL

My data looks something like this:
tags | fullName
----------------------------------------------+------------------------------------------
["tag1", "tag2"] | John
["tag3", "tag1"] | Jane
["tag1", "tag3"] | Bob
tags is a jsonb type and fullName a text in a postgres database
What I'm struggling to do is, create a view such as
tags | count
----------------------------------------------+------------------------------------------
tag1 | 3
tag2 | 1
tag3 | 2
You may use the jsonb_array_elements function to expand the row as array elements before grouping and counting.
See example and working fiddle below.
SELECT
tag_names as tag,
COUNT(1) as count
FROM (
SELECT
jsonb_array_elements(tags) as tag_names
FROM
my_table
) t
GROUP BY tag_names;
tag
count
tag1
3
tag2
1
tag3
2
View on DB Fiddle
Or shorter
SELECT
jsonb_array_elements(tags) as tag,
COUNT(1) as count
FROM
my_table
GROUP BY
tag
View on DB Fiddle

Aggregate rows according to JSON array content

I have a PSQL table with json tags, that are always strings stored in a json array :
id | tags (json)
--------------------------
1 | ["tag1", "tag2"]
2 | ["tag12", "tag2"]
122 | []
I would like to query, for instance, the count of entries in the table containing each tag.
For instance, I'd like to get :
tag | count
--------------------------
tag1 | 1
tag2 | 2
tag12 | 1
I tried
SELECT tags::text AS tag, COUNT(id) AS cnt FROM my_table GROUP BY tag;
but if does not work, since it gives
tag | cnt
--------------------------
["tag1", "tag2"] | 1
["tag12", "tag2"] | 1
I guess I need to get the list of all tags in an inner query, and then for each tag count the rows that contain this tag, but I can't find how to do that. Can you help me with that ?
Use json[b]_array_elements_text() and a lateral join to unnest the array:
select x.tag, count(*) cnt
from mytable t
cross join lateral json_array_elements_text(t.tags) as x(tag)
group by x.tag

Sql: Merge rows with same id and multiple attributes

I have a SQL table that looks like this:
Name | Attributes
-----------------
Toto | Attr1
Toto | Attr2
Titi | Attr1
and I would like a SQL request to merge the rows with attributes "Attr1" AND "Attr2" to have this table:
Name
----
Toto
How can I do this? Thank you.
If you want names that have both attributes, you can use group by:
select name
from t
where attributes in ('Attr1', 'Att2')
group by name
having count(distinct attributes) = 2;

SQL Query wordpress

SQL query
find me all the post tags with the proper count for all the posts under the current category?
example
culture ( culture is the current category name and it has 3 posts associated with it )
culture
post1 post 1 happens to be associated with tag1, tag3
post2 post 2 happens to be associated with tag1
post3 post 3 happens to be associated with tag1, tag4
As a result of the SQL query I'm looking for, I want to get a list as follows
culture
tag1 (3 posts)
tag3 ( 1 post )
tag4 ( 1 post )
and when you click tag1, we get only those (3) posts who have been classified under culture category and that has been tagged as tag1.
I'd like to get this data on the fly when I'm serving the culture page? I know it's an expensive query for run time purposes, but if you leave this aspect aside, how do you do it? will I have to use the wp_query?
Something like this should do the trick:
mysql> select category.category, count(posts.id) from posts join category on
posts.category=category.id group by posts.category;
+----------+-----------------+
| category | count(posts.id) |
+----------+-----------------+
| tag1 | 3 |
| tag2 | 2 |
| tag3 | 1 |
+----------+-----------------+
3 rows in set (0.01 sec)

SQL query: get tag id and number of tag occurences

I think, the answer to my question is rather simple, but I just can't figure it out at the moment. I experimented with DISTINCT() and COUNT(), but had no success, although I know, that I did it somehow once before. So:
I have three tables: article, tag, and article_tag. The last one is simply two columns with article_id and tag_id building a unique key.
Now I want a single SELECT statement that delivers me the tag_id and a count of how many times it appears in the article_tag table. Something like
| tag_id | count |
+---------+---------+
| 1 | 14 |
| 2 | 3 |
| 3 | 34 |
+---------+---------+
from a query like:
SELECT tag_id, COUNT(something) AS count
FROM article_tag
Could someone tell me, how I could get this result? I guess, I should stop coding when it's tending towards 0:00 o'clock...
SELECT tag_id,
COUNT(article_id) AS article_count
FROM article_tags
GROUP BY tag_id
Try: select tag_id, count(article_id) from article_tag group by tag_id
select tag_id, count(*) from article_tag group by tag_id order by tag_id;
You can fool around with the order by as well. To see which tags have the most references:
select tag_id, count(*) from article_tag group by tag_id order by count(*);