SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


how can i select distinct values from 1 column in my query


how can i select distinct values from 1 column in my query

Author
Message
mrichardson 57577
mrichardson 57577
SSC Rookie
SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)

Group: General Forum Members
Points: 27 Visits: 83
I would like to select distinct values from 1 column in my query.
If I was selecting just 1 column in a simple query it would probably work, but I've got a more complex query and having trouble with it.

here is my code:
SELECT     db.myTable.myFieldId, db.myFields.ModuleId, db.myTable.FieldValue
FROM db.myTable INNER JOIN
db.myFields ON db.myTable.myFieldId = db.myFields.myFieldId INNER JOIN
db.myRows ON db.myTable.myRowId = db.myRows.myRowId
WHERE (db.myFields.ModuleId = 1397) AND (db.myTable.myFieldId = 221)



The part I want to be distinct is this bit: db.myTable.FieldValue

If I insert the word 'distinct' before it, an error returns saying I can't do this.

thanks,
mark.
Luis Cazares
Luis Cazares
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16527 Visits: 19090
It's easy, but you need to define which value of db.myTable.FieldValue do you want when multiple values are available.


Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
Phil Parkin
Phil Parkin
SSCoach
SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)

Group: General Forum Members
Points: 18449 Visits: 20431
mrichardson 57577 (6/13/2013)
I would like to select distinct values from 1 column in my query.
If I was selecting just 1 column in a simple query it would probably work, but I've got a more complex query and having trouble with it.

here is my code:
SELECT     db.myTable.myFieldId, db.myFields.ModuleId, db.myTable.FieldValue
FROM db.myTable INNER JOIN
db.myFields ON db.myTable.myFieldId = db.myFields.myFieldId INNER JOIN
db.myRows ON db.myTable.myRowId = db.myRows.myRowId
WHERE (db.myFields.ModuleId = 1397) AND (db.myTable.myFieldId = 221)



The part I want to be distinct is this bit: db.myTable.FieldValue

If I insert the word 'distinct' before it, an error returns saying I can't do this.

thanks,
mark.


If your base data was like this:
(myFieldId, ModuleId, FieldValue)
(1,2,1)
(3,4,1)

and you want 'FieldValue' to be distinct, which version of myFieldId and moduleId would you want to display?


Help us to help you. For better, quicker and more-focused answers to your questions, consider following the advice in this link.

If the answer to your question can be found with a brief Google search, please perform the search yourself, rather than expecting one of the SSC members to do it for you.

Please surround any code or links you post with the appropriate IFCode formatting tags. It helps readability a lot.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search