2

i need help concerning a sql query. first of all, i have a database with the following structure (example):

ID              NAME              VALUE
123           ABC_A            Text Text Text
123           ABC_A            Some more Text
123           ABC_A            Even more Text
123           ABC_B            some other text

now, i want to get all the different values of rows with the name "ABC_A". i tried to get those via group by and having, without success.

3
  • what is the result (literally) of your desired query? Text Text Text, Some more Text and some other text? Commented Sep 25, 2012 at 12:33
  • it should be "text text text", "some more text" and "Even more text" Commented Sep 25, 2012 at 12:35
  • then i guess it should be easy enough, see my answer below. Commented Sep 25, 2012 at 12:36

4 Answers 4

4

IS this what you want?

SELECT DISTINCT Value
FROM tableName
WHERE ID = 123 AND Name = 'ABC_A'

but if the value of the ID and Name are unique then you can omit distinct (to avoid overkilling the server)

SELECT Value
FROM tableName
WHERE ID = 123 AND Name = 'ABC_A'
Sign up to request clarification or add additional context in comments.

Comments

2

Additional to John, if you use the keyword Distinct you onle get DIFFERENT Values, therefore

SELECT DISTINCT Value
FROM tableName
WHERE ID = 123 AND
        Name = 'ABC_A'

Comments

2

i want to get all the different values of rows with the name "ABC_A"

This would be for example:

SELECT value, count(value) FROM tbl WHERE name = 'ABC_A' GROUP BY value;

If you do not need the count of times one value appears, remove it, or use DISTINCT:

SELECT DISTINCT value FROM tbl WHERE name = 'ABC_A';

If you want the different values of rows by ID also,

SELECT value, count(value)
    FROM tbl
    WHERE id = 123 AND name = 'ABC_A'
    GROUP BY value;

Or if you want "ALL" the different values (with duplicates too) remove the GROUP BY (and you no longer can use the count(), which would be always 1):

SELECT value FROM tbl WHERE id = 123 AND name = 'ABC_A';

Comments

2

The GROUP BY statement is used in conjunction with the aggregate functions to group the result-set by one or more columns.
In above case why dont you use simple where clause

select * from <tableName> where name ='ABC_A'

1 Comment

That would be all right if he wanted "all" values; but if he wanted each value only once, no duplicates, then either GROUP or DISTINCT are needed.

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.