1

I have two tables:

CREATE TABLE stores (
stores_id varchar PRIMARY KEY,
owner_id varchar
);

CREATE TABLE sets (
sets_id varchar PRIMARY KEY,
stores_id varchar not null,
owner_id varchar not null,
item_id varchar not null,
);

How do I make a request that shows the number of items on the sets in stores? With selection by owner.

For example:

select
stores.*,
count(sets.item_id)
from stores
LEFT OUTER JOIN sets on stores.owner_id = sets.owner_id
where
stores.owner_id = 'e185775fc4f5'
GROUP BY stores.owner_id;

Thank you.

2
  • 1
    And what is the issue with your query? Commented Jun 6, 2018 at 11:53
  • Please edit your question and add some sample data and the expected output based on that data. Formatted text please, no screen shots. (edit your question - do not post code or additional information in comments) Commented Jun 6, 2018 at 12:04

2 Answers 2

1

I think you'd need to join on both the store and the owner, then COUNT(DISTINCT item_id)

select
st.owner_id, 
st.stores_id, 
count(distinct se.item_id)
  from stores st left join
    sets se
       on st.owner_id = se.owner_id
       and st.stores_id = se.stores_id
           group by st.owner_id, st.stores_id;

That will give a table that shows the owner, the store, then the number of items

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

1 Comment

Yes, its work! That i was add where st.owner_id = 'e185775fc4f5'
1

Is this what you want?

select st.stores_id, count(se.item_id)
from stores st left join
     sets se
     on st.owner_id = se.owner_id
where st.owner_id = 'e185775fc4f5'
group by st.stores_id;

1 Comment

album_id | count --------------+------- 3c70f26b3123 | 14 abad4544b213 | 14 c51acaf5091a | 14 e86229e70d84 | 14 (4 rows) Thus, the same quantity is obtained.

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.