Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


“Distinct” column in SQL query results


“Distinct” column in SQL query results

Author
Message
Nickson Asiimwe
Nickson Asiimwe
SSC-Enthusiastic
SSC-Enthusiastic (173 reputation)SSC-Enthusiastic (173 reputation)SSC-Enthusiastic (173 reputation)SSC-Enthusiastic (173 reputation)SSC-Enthusiastic (173 reputation)SSC-Enthusiastic (173 reputation)SSC-Enthusiastic (173 reputation)SSC-Enthusiastic (173 reputation)

Group: General Forum Members
Points: 173 Visits: 665
I have the following data

COL1         COL2 
1    0TY/OK
1    0TY/OK
1    0TY/OK   
1    0TY/OK   
1    0TY/OK
2    2KP/L   
2    2KP/L
2    2KP/L   
2    2KP/L
2    2KP/L    
3    7U5/2M
3    7U5/2M   
3    7U5/2M   
3    7U5/2M


And would like a query to give out the below output

COL1         COL2        COL3
1    0TY/OK 0TY/OK
1    0TY/OK 2KP/L   
1    0TY/OK    7U5/2M
1    0TY/OK   
1    0TY/OK
2    2KP/L   
2    2KP/L
2    2KP/L   
2    2KP/L
2    2KP/L    
3    7U5/2M
3    7U5/2M   
3    7U5/2M   
3    7U5/2M


I want COL3 to return the distinct values of COL2

Using
SELECT COL1, COL2, DISTINCT COL2 AS COL3 FROM MYTable

does not work is SQL SERVER
sumit.agrawal
sumit.agrawal
Valued Member
Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)

Group: General Forum Members
Points: 73 Visits: 220
I didn't get ur requirement...
Praveen Goud Kotha
Praveen Goud Kotha
Old Hand
Old Hand (315 reputation)Old Hand (315 reputation)Old Hand (315 reputation)Old Hand (315 reputation)Old Hand (315 reputation)Old Hand (315 reputation)Old Hand (315 reputation)Old Hand (315 reputation)

Group: General Forum Members
Points: 315 Visits: 93
Plz re-check your post...
and come with more clarity plz...

Praveen Goud
Nickson Asiimwe
Nickson Asiimwe
SSC-Enthusiastic
SSC-Enthusiastic (173 reputation)SSC-Enthusiastic (173 reputation)SSC-Enthusiastic (173 reputation)SSC-Enthusiastic (173 reputation)SSC-Enthusiastic (173 reputation)SSC-Enthusiastic (173 reputation)SSC-Enthusiastic (173 reputation)SSC-Enthusiastic (173 reputation)

Group: General Forum Members
Points: 173 Visits: 665
I have made some clarification.

I basically want COL3 to return the Distinct values of COL2, (remove duplicates from COL2 and return the unique values in COL3. Hope am much clear now.
Praveen Goud Kotha
Praveen Goud Kotha
Old Hand
Old Hand (315 reputation)Old Hand (315 reputation)Old Hand (315 reputation)Old Hand (315 reputation)Old Hand (315 reputation)Old Hand (315 reputation)Old Hand (315 reputation)Old Hand (315 reputation)

Group: General Forum Members
Points: 315 Visits: 93
What you are going to achieve with this..??
Can i know where you are going to implement(practically) this..?

Praveen Goud
shield_21
shield_21
SSC-Enthusiastic
SSC-Enthusiastic (103 reputation)SSC-Enthusiastic (103 reputation)SSC-Enthusiastic (103 reputation)SSC-Enthusiastic (103 reputation)SSC-Enthusiastic (103 reputation)SSC-Enthusiastic (103 reputation)SSC-Enthusiastic (103 reputation)SSC-Enthusiastic (103 reputation)

Group: General Forum Members
Points: 103 Visits: 396
Hi there,

I hope this helps..


DECLARE @x TABLE (col1 INT, col2 VARCHAR(10))
INSERT INTO @x
   SELECT 1,'0TY/OK' UNION ALL
   SELECT 1,'0TY/OK' UNION ALL
   SELECT 1,'0TY/OK' UNION ALL
   SELECT 1,'0TY/OK' UNION ALL
   SELECT 1,'0TY/OK' UNION ALL
   SELECT 2,'2KP/L' UNION ALL
   SELECT 2,'2KP/L' UNION ALL
   SELECT 2,'2KP/L' UNION ALL
   SELECT 2,'2KP/L' UNION ALL
   SELECT 2,'2KP/L' UNION ALL
   SELECT 3,'7U5/2M' UNION ALL
   SELECT 3,'7U5/2M' UNION ALL
   SELECT 3,'7U5/2M' UNION ALL
   SELECT 3,'7U5/2M'

;WITH col1_and_col2 AS
(
   SELECT ROW_NUMBER() OVER (ORDER BY col1,col2) AS row_num, col1, col2
   FROM @x
)
,col3 AS
(
   SELECT col2, ROW_NUMBER() OVER (ORDER BY col1,col2) AS row_num
   FROM @x
   GROUP BY col1,col2
)
SELECT a.col1,a.col2,b.col2 AS col3
FROM col1_and_col2 a
LEFT JOIN col3 b on a.row_num=b.row_num



Cheers!
Nickson Asiimwe
Nickson Asiimwe
SSC-Enthusiastic
SSC-Enthusiastic (173 reputation)SSC-Enthusiastic (173 reputation)SSC-Enthusiastic (173 reputation)SSC-Enthusiastic (173 reputation)SSC-Enthusiastic (173 reputation)SSC-Enthusiastic (173 reputation)SSC-Enthusiastic (173 reputation)SSC-Enthusiastic (173 reputation)

Group: General Forum Members
Points: 173 Visits: 665
Praveen Goud Kotha (7/21/2010)
What you are going to achieve with this..??
Can i know where you are going to implement(practically) this..?


I have an SSRS report which takes a value in COL2 as a parameter. Values that will be returned in COL2 are unknown at report design time. The available values for this report parameter should be all the possible (DISTINCT) values that are returned in COL2, so the parameter selection area (prompt) in the report should be a dropdownlist. Unfortunately, COL2 returns duplicate values(see my original post for the example), now COL3 is the solution to the problem of duplicates in the parameter prompt dropdownlist(Instead of providing a parameter dropdownlist with 100 duplicate options, if i return distinct values of COL2 AS COL3, and set my parameter source of values to COL3) i can reduce the available parameter options to say 10 instead of 100 duplicate values.

I would have added a different query to retrieve distinct values of COL2 and then set it to be the source of my parameters available values ,but apparently, SSRS 2005 does not support multiple datasets in a single report.

There is possibly a better way way to accomplish this, am not sure and would like to hear your opinion. thanks
Eugene Elutin
Eugene Elutin
Hall of Fame
Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)

Group: General Forum Members
Points: 3040 Visits: 5478
Have you tried to use the column from the DataSet used for report to populate the Paramaeter ListBox? Does it works at all?
I suggets you creating separate DataSet as SELEct DISTINCT Col2 FROM YoutrTable, and then use it in Available Values for your report Parameter.

_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Nickson Asiimwe
Nickson Asiimwe
SSC-Enthusiastic
SSC-Enthusiastic (173 reputation)SSC-Enthusiastic (173 reputation)SSC-Enthusiastic (173 reputation)SSC-Enthusiastic (173 reputation)SSC-Enthusiastic (173 reputation)SSC-Enthusiastic (173 reputation)SSC-Enthusiastic (173 reputation)SSC-Enthusiastic (173 reputation)

Group: General Forum Members
Points: 173 Visits: 665
Eugene Elutin (7/21/2010)
Have you tried to use the column from the DataSet used for report to populate the Paramaeter ListBox? Does it works at all?


Yes i have done that and it woks, but the parameter dropdownlist has duplication values, and thats what i want to overcome.

Eugene Elutin (7/21/2010)
I suggets you creating separate DataSet as SELEct DISTINCT Col2 FROM YoutrTable, and then use it in Available Values for your report Parameter.


Now thats where the problem is. Can i have two completely seperate SQL queries in a single report dataset? I have tried adding one query just after the other and it cause an error.

Do you know how to define two completely separate queries in a single SQL SERVER 2005 reporting services report??.
I hear support for multiple datasets is only avaialable in sql server 2008
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)

Group: General Forum Members
Points: 44964 Visits: 39862
Praveen Goud Kotha (7/21/2010)
What you are going to achieve with this..??
Can i know where you are going to implement(practically) this..?


Praveen,

Please change your avatar. I made that avatar and I'd like to keep it for exclusive use. Thanks.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
     Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is usually not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
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