1

hi i have a table with 6 columns, amongst them the is a column called user_id which has many occurances in the table and also a column i.e laptops where it references the laptops that a user has brought for some action. This also have many occurances in the table.

ID  DATE        LAPTOP    USER_ID
1   1/1/2014    Acer      10
2   1/1/2014    Toshiba   2
3   1/1/2014    HP        5
4   1/1/2014    Acer      5
5   1/1/2014    Toshiba   5
6   1/1/2014    HP        2
7   2/1/2014    Lenovo    2
8   2/1/2014    Acer      10
9   2/1/2014    HP        2
10  2/1/2014    HP        5

i need to select the distinct laptop occurences for each user_id and not generally the distinct occurances frm the table so the desired result would be

LAPTOP    USER_ID
Acer      10
Toshiba   2
HP        2
Lenovo    2
HP        5
Acer      5
Toshiba   5

whatever i ve tried seems to be far from the desired result so far

8
  • Can you show us the SQL that you tried? Commented Nov 9, 2014 at 20:24
  • DISTINCT and CONCAT can be a good start. Commented Nov 9, 2014 at 20:25
  • 4
    Wouldn'tselect distinct laptop, user_id from tablegive you the desired result? Commented Nov 9, 2014 at 20:28
  • 3
    And that is exactly what you would get, distinct combinations of laptop and user_id. See sqlfiddle.com/#!2/340bc6/2 Commented Nov 9, 2014 at 20:31
  • 1
    @nikolas jpw's solution will work too, it will select distinct combination of laptop, user_id Commented Nov 9, 2014 at 20:32

1 Answer 1

2

you can use GROUP BY

SELECT LAPTOP, USER_ID FROM Table1 GROUP BY LAPTOP, USER_ID;

just to make the answer as complete as possible, I am adding jpw's solution that is a comment above.

SELECT DISTINCT laptop, user_id FROM table

Here's a GROUP BY fiddle

Here's jpw's DISTINCT fiddle

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

1 Comment

this workds as well..i guess it wasn't so complicated as initially thought...thank you very much

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.