Home > Enterprise >  question sql phoenix : for each different value of column A, get first result of column B?
question sql phoenix : for each different value of column A, get first result of column B?

Time:09-28

Hello I'm learning SQL recently and I'm struggled with this pb for hours:
for each different value of column A, I want to get first result of column B
did someone know how to do this?

Here's an example

person account profile_pic
snoopy snoppy_main path/snoopy/000001
snoopy snoppy_twitter path/snoopy/000003
snoopy snoppy_twitter path/snoopy/000004
snoopy snoppy_twitter path/snoopy/000005
mickey mickey_insta path/mickey/001007
mickey mickey_insta path/mickey/001008
teddy teddy_global path/teddy/002009

and here's the result I want to have

person account profile_pic
snoopy snoppy_main path/snoopy/000001
snoopy snoppy_twitter path/snoopy/000003
mickey mickey_insta path/mickey/001007
teddy teddy_global path/teddy/002009

CodePudding user response:

You may use the MIN function to achieve your desired results. Eg

SELECT
    person,
    account,
    MIN(profile_pic) as profile_pic
FROM
   your_table
GROUP BY
   person,
   account

Let me know if this works for you.

  • Related