Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

how can i select distinct values from 1 column in my query Expand / Collapse
Author
Message
Posted Thursday, June 13, 2013 9:11 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, September 06, 2013 4:06 AM
Points: 27, Visits: 82
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.
Post #1463148
Posted Thursday, June 13, 2013 9:16 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 4:27 PM
Points: 2,763, Visits: 5,899
It's easy, but you need to define which value of db.myTable.FieldValue do you want when multiple values are available.


Luis C.
I am a great believer in luck, and I find the harder I work the more I have of it. Stephen Leacock

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1463150
Posted Thursday, June 13, 2013 9:28 AM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Today @ 8:56 AM
Points: 4,828, Visits: 11,178
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.

When you ask a question (and please do ask a question: "My T-SQL does not work" just doesn't cut it), please provide enough information for us to understand its context.
Post #1463156
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse