Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
svsv01
Starting Member
5 Posts |
Posted - 2008-03-06 : 22:01:05
|
| Hi,I need some help to solve the following query.Consider the following tableNAME OTHERNAME---------------------APPLE-----AAAPPLE-----AAAPPLE-----AAORANGE-----OOORANGE-----O1GRAPE-----G1GRAPE-----G1GRAPE-----G2GRAPE-----G3MANGO-----M1MANGO-----M2MANGO-----M3OUTPUT should be----------------ORANGE-----OOORANGE-----O1GRAPE-----G1GRAPE-----G2GRAPE-----G3MANGO-----M1MANGO-----M2MANGO-----M3Advance Thanks for your help.Sri |
|
|
dataguru1971
Master Smack Fu Yak Hacker
1464 Posts |
Posted - 2008-03-06 : 22:04:26
|
[code]Create Table #t ([name] char(6) not null, [other] char(2) not null)Insert Into #tSelect 'APPLE','AA' UNION ALLSelect 'APPLE','AA' UNION ALLSelect 'APPLE','AA' UNION ALLSelect 'ORANGE','OO' UNION ALLSelect 'ORANGE','O1' UNION ALLSelect 'GRAPE','G1' UNION ALLSelect 'GRAPE','G1' UNION ALLSelect 'GRAPE','G2' UNION ALLSelect 'GRAPE','G3' UNION ALLSelect 'MANGO','M1' UNION ALLSelect 'MANGO','M2' UNION ALLSelect 'MANGO','M3'Select #t.[Name],#t.OtherFROM #t Group by #t.[Name], #t.Otherhaving Count(*) = 1drop table #t/*ResultsGRAPE G2GRAPE G3MANGO M1MANGO M2MANGO M3ORANGE O1ORANGE OO[/code]Do you need to exclude APPLE?you don't really specify your requirements, but that should do it.Edited to show specific requested results. Poor planning on your part does not constitute an emergency on my part. |
 |
|
|
svsv01
Starting Member
5 Posts |
Posted - 2008-03-06 : 22:28:24
|
| Thanks for a quick reply. in your output you didn't grt GRAPE-----G1. I will put my question again. Basically get all the NAME which has more than one OTHERNAME. APPLE needs to be excluded as it has same NAME and same OTHERNAME.NAME OTHERNAME---------------------APPLE-----AAAPPLE-----AAAPPLE-----AAORANGE-----OOORANGE-----O1GRAPE-----G1GRAPE-----G1GRAPE-----G2GRAPE-----G3MANGO-----M1MANGO-----M2MANGO-----M3OUTPUT 1 should be----------------ORANGE-----OOORANGE-----O1GRAPE-----G1GRAPE-----G2GRAPE-----G3MANGO-----M1MANGO-----M2MANGO-----M3OUTPUT 2 should be----------------ORANGEGRAPEMANGO |
 |
|
|
dataguru1971
Master Smack Fu Yak Hacker
1464 Posts |
Posted - 2008-03-06 : 22:38:40
|
You still need to clarify, as your description of what is needed, your sample data, and your expected output don't correspond.You eliminate all of the APPLE with duplicate values APPLE-----AAAPPLE-----AAAPPLE-----AA But you don't elimate both GRAPE meeting the logically equivalent criteria.GRAPE-----G1GRAPE-----G1 Therefore, my code provides you with a working solution to eliminate where there are duplicates based on [Name] and Duplicate [Other]here it is again, this time with a results showing the duplicate counts based on your provided sample. Note that the 3rd sample still provides expected results because there ARE values of GRAPE returned in the 1st criteria which have unique and only 1 count of [Other]Create Table #t ([name] char(6) not null, [other] char(2) not null)Insert Into #tSelect 'APPLE','AA' UNION ALLSelect 'APPLE','AA' UNION ALLSelect 'APPLE','AA' UNION ALLSelect 'ORANGE','OO' UNION ALLSelect 'ORANGE','O1' UNION ALLSelect 'GRAPE','G1' UNION ALLSelect 'GRAPE','G1' UNION ALLSelect 'GRAPE','G2' UNION ALLSelect 'GRAPE','G3' UNION ALLSelect 'MANGO','M1' UNION ALLSelect 'MANGO','M2' UNION ALLSelect 'MANGO','M3'Select Count(*),#t.[Name],#t.OtherFROM #t Group by #t.[Name], #t.Other/*Results3 APPLE AA2 GRAPE G11 GRAPE G21 GRAPE G31 MANGO M11 MANGO M21 MANGO M31 ORANGE O11 ORANGE OO*/Select #t.[Name],#t.OtherFROM #t Group by #t.[Name], #t.Otherhaving Count(*) = 1/*ResultsGRAPE G2GRAPE G3MANGO M1MANGO M2MANGO M3ORANGE O1ORANGE OO */Select Distinct v.Name FROM (Select #t.[Name],#t.Other FROM #t Group by #t.[Name], #t.Other having Count(*) = 1) v/*ResultsGRAPE MANGO ORANGE*/drop table #t Poor planning on your part does not constitute an emergency on my part. |
 |
|
|
svsv01
Starting Member
5 Posts |
Posted - 2008-03-06 : 22:45:20
|
| Hi, Thanks a lot for a very prompt reply.I need to get GRAPE----G1 also. The reason is GRAPE has OTHERNAME G1, G2 & G3. APPLE is eleminated because APPLE has one and only other name AA. please let me know if you need more info. Again thankf for your help. |
 |
|
|
dataguru1971
Master Smack Fu Yak Hacker
1464 Posts |
Posted - 2008-03-06 : 23:15:45
|
sloppy, but this does it.Create Table #t ([name] char(6) not null, [other] char(2) not null)Insert Into #tSelect 'APPLE','AA' UNION ALLSelect 'APPLE','AA' UNION ALLSelect 'APPLE','AA' UNION ALLSelect 'ORANGE','OO' UNION ALLSelect 'ORANGE','O1' UNION ALLSelect 'GRAPE','G1' UNION ALLSelect 'GRAPE','G1' UNION ALLSelect 'GRAPE','G2' UNION ALLSelect 'GRAPE','G3' UNION ALLSelect 'MANGO','M1' UNION ALLSelect 'MANGO','M2' UNION ALLSelect 'MANGO','M3'Select Distinct #t.[name],#t.[other]from #T INNER JOIN (Select name,Count(name) as Ct FROM (Select DISTINCT name, Other FROM #t) a group by name) BON #t.[name] = b.nameWhere b.CT > 1/*resultsGRAPE G1GRAPE G2GRAPE G3MANGO M1MANGO M2MANGO M3ORANGE O1ORANGE OO*/drop table #t Poor planning on your part does not constitute an emergency on my part. |
 |
|
|
ranganath
Posting Yak Master
209 Posts |
Posted - 2008-03-07 : 05:28:21
|
| Hi,Try with the followingSELECT DISTINCT a.[name], a.otherFROM #t aINNER JOIN (SELECT DISTINCT name ,other, RANK() OVER (partition BY name ORDER BY other Desc ) AS cnt FROM #t ) bON a.name = b.name WHERE b.cnt > 1 OR SELECT DISTINCT a.[name], a.otherFROM #t aINNER JOIN (SELECT DISTINCT name , COUNT (DISTINCT Other) AS cnt FROM #t GROUP BY name ) bON a.name = b.name WHERE b.cnt > 1 |
 |
|
|
svsv01
Starting Member
5 Posts |
Posted - 2008-03-07 : 07:08:59
|
quote: Originally posted by dataguru1971 sloppy, but this does it.Create Table #t ([name] char(6) not null, [other] char(2) not null)Insert Into #tSelect 'APPLE','AA' UNION ALLSelect 'APPLE','AA' UNION ALLSelect 'APPLE','AA' UNION ALLSelect 'ORANGE','OO' UNION ALLSelect 'ORANGE','O1' UNION ALLSelect 'GRAPE','G1' UNION ALLSelect 'GRAPE','G1' UNION ALLSelect 'GRAPE','G2' UNION ALLSelect 'GRAPE','G3' UNION ALLSelect 'MANGO','M1' UNION ALLSelect 'MANGO','M2' UNION ALLSelect 'MANGO','M3'Select Distinct #t.[name],#t.[other]from #T INNER JOIN (Select name,Count(name) as Ct FROM (Select DISTINCT name, Other FROM #t) a group by name) BON #t.[name] = b.nameWhere b.CT > 1/*resultsGRAPE G1GRAPE G2GRAPE G3MANGO M1MANGO M2MANGO M3ORANGE O1ORANGE OO*/drop table #t Poor planning on your part does not constitute an emergency on my part.
The query was simply Great. It worked. Thanks for all your help. The reply was so timely and very helpful. |
 |
|
|
svsv01
Starting Member
5 Posts |
Posted - 2008-03-07 : 08:04:39
|
quote: Originally posted by ranganath Hi,Try with the followingSELECT DISTINCT a.[name], a.otherFROM #t aINNER JOIN (SELECT DISTINCT name ,other, RANK() OVER (partition BY name ORDER BY other Desc ) AS cnt FROM #t ) bON a.name = b.name WHERE b.cnt > 1 OR SELECT DISTINCT a.[name], a.otherFROM #t aINNER JOIN (SELECT DISTINCT name , COUNT (DISTINCT Other) AS cnt FROM #t GROUP BY name ) bON a.name = b.name WHERE b.cnt > 1
Thank for the help. The query works. |
 |
|
|
|
|
|
|
|