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


distinct columns


distinct columns

Author
Message
scottichrosaviakosmos
scottichrosaviakosmos
Old Hand
Old Hand (316 reputation)Old Hand (316 reputation)Old Hand (316 reputation)Old Hand (316 reputation)Old Hand (316 reputation)Old Hand (316 reputation)Old Hand (316 reputation)Old Hand (316 reputation)

Group: General Forum Members
Points: 316 Visits: 691
i have a table with no unique fields but and when i use distinct for column1 then it gives 3000 rows or values but when i use distinct for all the fields then it gives 7000 rows. I want all rows related to the column one ie. i want to get distict all rows on basis of coloumn1 ie 3000 rows of all fields.
currently i m useing query :
select distinct * from table1 where column1 in (select distinct column1 from table 1).
but this is giving 7000 rows but dustinct column1 is only 3000 rows.
LutzM
LutzM
SSC-Insane
SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)

Group: General Forum Members
Points: 22485 Visits: 13559
It's not clear to me what you're looking for.
What rows do you want to return?
Simple scenario:
col1 col2
A1 1
A1 2
A2 3
A2 4
What would be your expected output?



Lutz
A pessimist is an optimist with experience.

How to get fast answers to your question
How to post performance related questions
Links for Tally Table , Cross Tabs and Dynamic Cross Tabs , Delimited Split Function
v.neelimaprasanna31
v.neelimaprasanna31
Forum Newbie
Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)

Group: General Forum Members
Points: 5 Visits: 1
the question was not cleared....clarify what u want
sqlnaive
sqlnaive
SSCertifiable
SSCertifiable (6.2K reputation)SSCertifiable (6.2K reputation)SSCertifiable (6.2K reputation)SSCertifiable (6.2K reputation)SSCertifiable (6.2K reputation)SSCertifiable (6.2K reputation)SSCertifiable (6.2K reputation)SSCertifiable (6.2K reputation)

Group: General Forum Members
Points: 6247 Visits: 2774
Pretty simple actually.

Distinct column1 will give the distinct values within the column1
Distinct * will give the distinct rows within the table.

Say this is the content of table1

Column1     Column2
1001 A
1001 B
1001 C
1002 A
1002 B
1003 C
1003 C


Select Distinct column1 from table1 will give following result:

Column1
1001
1002
1003


Distinct * from table1 will give following result:

Column1       Column2
1001 A
1001 B
1001 C
1002 A
1002 B
1003 C


As only <1003>,<C> is the row (*) which is duplicate.
mahmad_khoja
mahmad_khoja
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1273 Visits: 83
CREATE TABLE TmpTable (Col1 varchar(10),Col2 int)

SELECT Col1,Col2 FROM TmpTable

Col1 Col2
A 10
A 11
B 12
B 8
B 15
C 18
C 24
C 28
C 35

SELECT DISTINCT Col1 FROM TmpTable

Col1
A
B
C

SELECT DISTINCT Col1,Col2 FROM TmpTable

Col1 Col2
A 10
A 11
B 8
B 12
B 15
C 18
C 24
C 28
C 35

USE BELOW FOR YOUR PURPOSE

SELECT * FROM
(
SELECT
ROW_NUMBER() OVER ( PARTITION BY Col1 ORDER BY Col2 desc ) TCOL
, Col1
, Col2
FROM TmpTable
) A
WHERE TCOL = 1

TCOL Col1 Col2
1 A 11
1 B 15
1 C 35

Modify the query as per your requirement.
dwain.c
dwain.c
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: 16885 Visits: 6431
Firstly, in a query as follows there is no need for the second DISTINCT:


select distinct * from table1 where column1 in (select distinct column1 from table 1).




Secondly, you may be able to GROUP BY column1, if the result you want returned for column2 is either the MIN or the MAX value in the rows that match the grouped column.


My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
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