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

“Distinct” column in SQL query results Expand / Collapse
Author
Message
Posted Wednesday, July 21, 2010 5:10 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Sunday, November 30, 2014 7:17 AM
Points: 173, Visits: 612
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
Post #956241
Posted Wednesday, July 21, 2010 5:13 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Today @ 4:21 AM
Points: 73, Visits: 213
I didn't get ur requirement...
Post #956243
Posted Wednesday, July 21, 2010 5:16 AM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Thursday, September 16, 2010 7:13 AM
Points: 315, Visits: 93
Plz re-check your post...
and come with more clarity plz...


Praveen Goud
Post #956246
Posted Wednesday, July 21, 2010 5:21 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Sunday, November 30, 2014 7:17 AM
Points: 173, Visits: 612
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.
Post #956250
Posted Wednesday, July 21, 2010 5:42 AM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Thursday, September 16, 2010 7:13 AM
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
Post #956263
Posted Wednesday, July 21, 2010 5:45 AM


SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Monday, October 27, 2014 11:41 PM
Points: 95, 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!
Post #956265
Posted Wednesday, July 21, 2010 6:48 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Sunday, November 30, 2014 7:17 AM
Points: 173, Visits: 612
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
Post #956306
Posted Wednesday, July 21, 2010 8:22 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Friday, December 12, 2014 10:09 AM
Points: 2,876, Visits: 5,201
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!"
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Post #956382
Posted Wednesday, July 21, 2010 8:32 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Sunday, November 30, 2014 7:17 AM
Points: 173, Visits: 612
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
Post #956401
Posted Wednesday, July 21, 2010 11:35 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 7:27 AM
Points: 35,769, Visits: 32,437
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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #956574
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse