1

I want to get all array_agg() values with one matched value.

Like if in robotsession."Session_OS" there are multiple values ['Android', 'IOS', 'Windows']

In my current query it's giving ['Android'] only. But I want all.

My query is:

SELECT robotAds."Ad_ID",
    ARRAY_AGG(DISTINCT quote_literal(robotSession."Session_OS"))
    as Session_OS
    FROM robot__ads robotAds LEFT JOIN
    robot__session__scraper__data robotScraper ON
    robotScraper."adIDAdID" = robotAds."Ad_ID" LEFT JOIN
    robot__session_data robotSession ON robotSession."id" = robotScraper."sessionIDId" WHERE
    robotScraper."sessionIDId" IS NOT NULL
    AND robotsession."Session_OS" IN ('Android')
    GROUP BY robotAds."Ad_ID"

In the result of this query is "Session_OS" gives only 'Android' value.

And sample data of this query is like:

  id   | Session_OS
-------|-------------
 641   | {'Android'}
 642   | {'Android'}
 643   | {'Android'}

But I want all the values that are IN Session_Os Agregation if it matched any of one. like here in my query it is matching with 'Android':

  id   | Session_OS
-------|-------------
 641   | {'Android, 'IOS'}
 642   | {'Android, 'Windows'}
 643   | {'Android', 'IOS', 'Windows'}

How I can achieve this ??

UPDATE:

Full data, without this condition in query AND robotsession."Session_OS" IN ('Android')

  id   | Session_OS
-------|-------------
 641   | {'Android, 'IOS'}
 642   | {'Android, 'Windows'}
 643   | {'Android', 'IOS', 'Windows'}
 644   | {'IOS', 'Windows'}
 645   | {'IOS'}

But after adding that condition AND robotsession."Session_OS" IN ('Android') in query result is

   id  | Session_OS
-------|-------------
 641   | {'Android'}
 642   | {'Android'}
 643   | {'Android'}

But I want this. How I can do that?

  id   | Session_OS
-------|-------------
 641   | {'Android, 'IOS'}
 642   | {'Android, 'Windows'}
 643   | {'Android', 'IOS', 'Windows'}
5
  • a) Please minimize your example code to the absolut necessary parts which are relevant to describe your problem. I.e. I guess, it's not necessary to show us 3 or 4 joins. b) Please add some sample data and the expected output, so that we could reproduce your problem Commented Dec 14, 2020 at 14:14
  • Hi Thanks for quick reply. I have updated my question. Kindly check again. Commented Dec 14, 2020 at 15:26
  • what is quote_literal(). Please show the data before grouping Commented Dec 14, 2020 at 16:06
  • quote_literal() Is nothing. You can ignore it. It is just add quote ('') to each value. Commented Dec 14, 2020 at 16:18
  • Kindly check updated question. Commented Dec 14, 2020 at 16:34

1 Answer 1

2

demo:db<>fiddle

SELECT
    id,
    ARRAY_AGG(session_os)
FROM
    t
GROUP BY id
HAVING ARRAY_AGG(session_os) && ARRAY['Android']

Your problem is that you FIRST filter the records with session_os = Android. And afterwards you are aggregate these.

You have to aggregate first and then have a look into the array aggregate if Android is an element. This can be done using the HAVING clause and the && operator which returns true if two arrays contain the same element(s).

Sign up to request clarification or add additional context in comments.

6 Comments

Hi, Thanks This is working. There is one more issue. What if there is a null value in ARRAY_AGG(session_os) ?. It's throwing an error. ERROR: array must not contain nulls SQL state: 22004
Hi, Thank This is working. There is one more issue. What if there is a null value in ARRAY_AGG(session_os) ?. It's throwing an error. ERROR: array must not contain nulls SQL state: 22004
You can easily add a "WHERE session_os IS NOT NULL" filter before grouping: dbfiddle.uk/…
Why it's not allowing me to get NULL values as well ? If I am using "WHERE session_os IS NULL" and HAVING ARRAY_AGG(session_os) && ARRAY['Android']. It againg throwing same error. I can't do this. I can't get NULL values ?
See this dbfiddle.uk/… . My case is like this... in this case how I can get null values as well.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.