| Author |
Topic |
|
soori457
Yak Posting Veteran
85 Posts |
Posted - 2010-02-25 : 05:57:28
|
| Hi All,How to get distinct rows from table without using distinct keywordAny one please tell me,,,Thanks in AdvanceSureshkumarSuresh Kumar |
|
|
senthil_nagore
Master Smack Fu Yak Hacker
1007 Posts |
Posted - 2010-02-25 : 06:00:32
|
| Use Group bySenthil.C------------------------------------------------------[Microsoft][ODBC SQL Server Driver]Operation canceledhttp://senthilnagore.blogspot.com/ |
 |
|
|
soori457
Yak Posting Veteran
85 Posts |
Posted - 2010-02-25 : 06:17:19
|
| Thanks for your reply Me.SenthilHow to get distinct rows based on a single column, when used distinct keyword in select query with retrieving multiple columnsex: select distinct colA, colB, colC from tablenow I want to fetch distinct rows from the table based on colA, but not on colB and colC.Suresh Kumar |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-02-25 : 06:24:55
|
| Post some sample data with expected resultMadhivananFailing to plan is Planning to fail |
 |
|
|
soori457
Yak Posting Veteran
85 Posts |
Posted - 2010-02-25 : 06:48:05
|
| here is the sample data colA colB colC101 1 A102 2 B102 3 C103 4 D104 5 E104 6 F104 7 G105 8 Hexpected o/pcolA colB colC101 1 A102 2 B103 4 D104 5 E105 8 HSuresh Kumar |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-02-25 : 07:36:11
|
| select colA,min(colB) as colB,min(colC) as colC from your_tablegroup by colAMadhivananFailing to plan is Planning to fail |
 |
|
|
soori457
Yak Posting Veteran
85 Posts |
Posted - 2010-02-25 : 08:20:28
|
| Hi MadhivananThe query is working for me. But I did not understand what the query actually do. Can you please explain me the query.Suresh Kumar |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-02-25 : 08:47:13
|
quote: Originally posted by soori457 Hi MadhivananThe query is working for me. But I did not understand what the query actually do. Can you please explain me the query.Suresh Kumar
For each value of colA, take minimum values for colB and colCMadhivananFailing to plan is Planning to fail |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-02-25 : 09:00:41
|
quote: Originally posted by madhivanan select colA,min(colB) as colB,min(colC) as colC from your_tablegroup by colAMadhivananFailing to plan is Planning to fail
wont work correctly unless colB and colC always goes in a sequential manner. If not in sequence, then the value returned may not be the ones in same row.To be on safer side, this can be usedSELECT ColA,ColB,ColCFROM(SELECT ColA,ColB,ColC,ROW_NUMBER() OVER (PARTITION BY ColA ORDER BY colB ASC) AS SeqFROM Table)tWHERE Seq=1 ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
soori457
Yak Posting Veteran
85 Posts |
Posted - 2010-02-25 : 09:09:35
|
| Ohh..Actually am looking for rows with distinct colAFor each value of colA, I should get the exact row, but not the minimum values of colB and colC. Otherwise I'll get the wrong dataSuresh Kumar |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-02-25 : 09:13:58
|
quote: Originally posted by soori457 Ohh..Actually am looking for rows with distinct colAFor each value of colA, I should get the exact row, but not the minimum values of colB and colC. Otherwise I'll get the wrong dataSuresh Kumar
try mine and let me know if that will suit your need------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
soori457
Yak Posting Veteran
85 Posts |
Posted - 2010-02-25 : 10:27:14
|
Thanks Visakh16 and Madhivanan for your queries.Visakh, Your query is working and am able to get the exact records now.But am getting the result as like we print, but not as a result set.how can we get the result setThankyouquote: Originally posted by visakh16
quote: Originally posted by soori457 Ohh..Actually am looking for rows with distinct colAFor each value of colA, I should get the exact row, but not the minimum values of colB and colC. Otherwise I'll get the wrong dataSuresh Kumar
try mine and let me know if that will suit your need------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
Suresh Kumar |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-02-25 : 10:29:43
|
quote: Originally posted by soori457 Thanks Visakh16 and Madhivanan for your queries.Visakh, Your query is working and am able to get the exact records now.But am getting the result as like we print, but not as a result set.how can we get the result setThankyouquote: Originally posted by visakh16
quote: Originally posted by soori457 Ohh..Actually am looking for rows with distinct colAFor each value of colA, I should get the exact row, but not the minimum values of colB and colC. Otherwise I'll get the wrong dataSuresh Kumar
try mine and let me know if that will suit your need------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
Suresh Kumar
what you mean by 'the result as like we print, but not as a result set'?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
soori457
Yak Posting Veteran
85 Posts |
Posted - 2010-02-25 : 10:39:07
|
| Means, the result am getting as not in table format. Am getting the result as below format in Results pane.colA colB colB----------- ---------------- --------------------------22017 737493 test 22687 737423 Need help designing a BJT Suresh Kumar |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-02-25 : 11:02:42
|
quote: Originally posted by soori457 Means, the result am getting as not in table format. Am getting the result as below format in Results pane.colA colB colB----------- ---------------- --------------------------22017 737493 test 22687 737423 Need help designing a BJT Suresh Kumar
thats because you've selected results as text option. just change option to results to grid and see------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
soori457
Yak Posting Veteran
85 Posts |
Posted - 2010-02-25 : 12:45:09
|
| can you please tell me how to change the results option to gridSuresh Kumar |
 |
|
|
Sachin.Nand
2937 Posts |
Posted - 2010-02-25 : 13:09:24
|
| Just press Cntrl+D before executing the query.PBUH |
 |
|
|
|