0

I have table with multiple rows in two columns.

# Column_A # Column B
# 1        # photo01
# 1        # photo02
# 1        # photo03
# 2        # video01
# 2        # video02
# 3        # music01
# 3        # music02
# 3        # music03

So when i'm using SELECT DISTINCT Column_A i have 3 records: 1, 2, 3. When i'm using SELECT DISTINCT Column_A, Column_B i have all records. I want not duplicated rows from Column_A with first row from Column_B, 1:1.

SELECT DISTINCT ID_product, (SELECT photos FROM my_table) 
FROM my_table
ORDER BY ID_product

Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

Expected result:

# Column_A # Column B
# 1        # photo01
# 2        # video01
# 3        # music01

How can I do it?

1 Answer 1

3

Use aggregation:

select column_a, min(column_b)
from t
group by column_a;

If you want an arbitrary match, you can use window functions stead:

select column_a, column_b
from (select t.*,
             row_number() over (partition by column_a order by newid()) as seqnum
      from t
     ) t
where seqnum = 1;
Sign up to request clarification or add additional context in comments.

Comments

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.