HI How can I retrieve only one row that has duplicates on not all columns? For example a table Company could look like this. Id Zip Address Revenue ---------------------------------------------------------- ------ 1 1 Street 1 1 000 000 2 2 Street 2 2 000 000 3 3 Street 3 3 000 000 4 1 Street 1 1 500 000 Company 1 and 4 have the same Address and I only want to retrieve one company at the same Address, the one with the highest revenue. Best regards Martin
Thanks for the fast answer, but i was a bit unclear, i need to fetch the id, and other columns from the table aswell. CREATE TABLE #Demo ( idNo int identity(1,1), colA int, colB int, colC int, colD int, colE int, colF int, colG int ) IF A and B are the same, only retrive the one with highest C Thanks Martin ********************************** [quoted text, click to view] >-----Original Message----- >Martin >CREATE TABLE #Demo ( > idNo int identity(1,1), > colA int, > colB int, > colC int >) > >INSERT INTO #Demo(colA,colB,colC) VALUES (1,6,1) >INSERT INTO #Demo(colA,colB,colC) VALUES (1,6,2) >INSERT INTO #Demo(colA,colB,colC) VALUES (2,4,1) >INSERT INTO #Demo(colA,colB,colC) VALUES (3,3,1) > > >SELECT colA,colB,max(colC)AS D FROM #Demo >GROUP BY colA,colB > >"Martin" <nospam@nospam.com> wrote in message >news:183301c38656$476f6ce0$3501280a@phx.gbl... >> HI >> >> How can I retrieve only one row that has duplicates on >> not all columns? >> For example a table Company could look like this. >> >> Id Zip Address Revenue >> ------------------------------------------------------- --- >> ------ >> 1 1 Street 1 1 000 000 >> 2 2 Street 2 2 000 000 >> 3 3 Street 3 3 000 000 >> 4 1 Street 1 1 500 000 >> >> >> Company 1 and 4 have the same Address and I only want to >> retrieve one company at the same Address, the one with >> the highest revenue. >> >> Best regards >> >> Martin >> > > >.
select * from #demo t1 where t1.c=(select max(c) from #demo t2 where t2.a=t1.a and t2.b=t1.b) -- -oj Rac v2.2 & QALite! http://www.rac4sql.net [quoted text, click to view] "Martin" <nospam@nospam.com> wrote in message news:160201c3865d$79b659a0$a401280a@phx.gbl... > Thanks for the fast answer, but i was a bit unclear, i > need to fetch the id, and other columns from the table > aswell. > > CREATE TABLE #Demo ( > idNo int identity(1,1), > colA int, > colB int, > colC int, > colD int, > colE int, > colF int, > colG int > ) > > IF A and B are the same, only retrive the one with > highest C > > Thanks > > Martin > ********************************** > > > >-----Original Message----- > >Martin > >CREATE TABLE #Demo ( > > idNo int identity(1,1), > > colA int, > > colB int, > > colC int > >) > > > >INSERT INTO #Demo(colA,colB,colC) VALUES (1,6,1) > >INSERT INTO #Demo(colA,colB,colC) VALUES (1,6,2) > >INSERT INTO #Demo(colA,colB,colC) VALUES (2,4,1) > >INSERT INTO #Demo(colA,colB,colC) VALUES (3,3,1) > > > > > >SELECT colA,colB,max(colC)AS D FROM #Demo > >GROUP BY colA,colB > > > >"Martin" <nospam@nospam.com> wrote in message > >news:183301c38656$476f6ce0$3501280a@phx.gbl... > >> HI > >> > >> How can I retrieve only one row that has duplicates on > >> not all columns? > >> For example a table Company could look like this. > >> > >> Id Zip Address Revenue > >> ------------------------------------------------------- > --- > >> ------ > >> 1 1 Street 1 1 000 000 > >> 2 2 Street 2 2 000 000 > >> 3 3 Street 3 3 000 000 > >> 4 1 Street 1 1 500 000 > >> > >> > >> Company 1 and 4 have the same Address and I only want > to > >> retrieve one company at the same Address, the one with > >> the highest revenue. > >> > >> Best regards > >> > >> Martin > >> > > > > > >. > >
Thanks, that's what I was looking for. Why aren't there any partial distinct func in SQL, in the case we get all if col A, B, and C are the same. DB could just use random to choose witch partial duplicate to return. [quoted text, click to view] >-----Original Message----- >select * >from #demo t1 >where t1.c=(select max(c) from #demo t2 where t2.a=t1.a and t2.b=t1.b) > >-- >-oj >Rac v2.2 & QALite! > http://www.rac4sql.net > > >"Martin" <nospam@nospam.com> wrote in message >news:160201c3865d$79b659a0$a401280a@phx.gbl... >> Thanks for the fast answer, but i was a bit unclear, i >> need to fetch the id, and other columns from the table >> aswell. >> >> CREATE TABLE #Demo ( >> idNo int identity(1,1), >> colA int, >> colB int, >> colC int, >> colD int, >> colE int, >> colF int, >> colG int >> ) >> >> IF A and B are the same, only retrive the one with >> highest C >> >> Thanks >> >> Martin >> ********************************** >> >> >> >-----Original Message----- >> >Martin >> >CREATE TABLE #Demo ( >> > idNo int identity(1,1), >> > colA int, >> > colB int, >> > colC int >> >) >> > >> >INSERT INTO #Demo(colA,colB,colC) VALUES (1,6,1) >> >INSERT INTO #Demo(colA,colB,colC) VALUES (1,6,2) >> >INSERT INTO #Demo(colA,colB,colC) VALUES (2,4,1) >> >INSERT INTO #Demo(colA,colB,colC) VALUES (3,3,1) >> > >> > >> >SELECT colA,colB,max(colC)AS D FROM #Demo >> >GROUP BY colA,colB >> > >> >"Martin" <nospam@nospam.com> wrote in message >> >news:183301c38656$476f6ce0$3501280a@phx.gbl... >> >> HI >> >> >> >> How can I retrieve only one row that has duplicates on >> >> not all columns? >> >> For example a table Company could look like this. >> >> >> >> Id Zip Address Revenue >> >> ---------------------------------------------------- --- >> --- >> >> ------ >> >> 1 1 Street 1 1 000 000 >> >> 2 2 Street 2 2 000 000 >> >> 3 3 Street 3 3 000 000 >> >> 4 1 Street 1 1 500 000 >> >> >> >> >> >> Company 1 and 4 have the same Address and I only want >> to >> >> retrieve one company at the same Address, the one with >> >> the highest revenue. >> >> >> >> Best regards >> >> >> >> Martin >> >> >> > >> > >> >. >> > > > >.
Hi, Does this work for you? select id,a.street,revenue from Company as a, ( select street from company group by street ) as b where a.street=b.street and revenue=(select max(revenue) from company where street=a.street) Thanx Deep [quoted text, click to view] >-----Original Message----- >HI > >How can I retrieve only one row that has duplicates on >not all columns? >For example a table Company could look like this. > >Id Zip Address Revenue >---------------------------------------------------------- >------ >1 1 Street 1 1 000 000 >2 2 Street 2 2 000 000 >3 3 Street 3 3 000 000 >4 1 Street 1 1 500 000 > > >Company 1 and 4 have the same Address and I only want to >retrieve one company at the same Address, the one with >the highest revenue. > >Best regards > >Martin > >.
Martin CREATE TABLE #Demo ( idNo int identity(1,1), colA int, colB int, colC int ) INSERT INTO #Demo(colA,colB,colC) VALUES (1,6,1) INSERT INTO #Demo(colA,colB,colC) VALUES (1,6,2) INSERT INTO #Demo(colA,colB,colC) VALUES (2,4,1) INSERT INTO #Demo(colA,colB,colC) VALUES (3,3,1) SELECT colA,colB,max(colC)AS D FROM #Demo GROUP BY colA,colB [quoted text, click to view] "Martin" <nospam@nospam.com> wrote in message news:183301c38656$476f6ce0$3501280a@phx.gbl... > HI > > How can I retrieve only one row that has duplicates on > not all columns? > For example a table Company could look like this. > > Id Zip Address Revenue > ---------------------------------------------------------- > ------ > 1 1 Street 1 1 000 000 > 2 2 Street 2 2 000 000 > 3 3 Street 3 3 000 000 > 4 1 Street 1 1 500 000 > > > Company 1 and 4 have the same Address and I only want to > retrieve one company at the same Address, the one with > the highest revenue. > > Best regards > > Martin >
<g> there is no such thing. However, you could try sqlwish@microsoft.com. If you have a strong business case, they might implement such. -- -oj RAC v2.2 & QALite! http://www.rac4sql.net [quoted text, click to view] "Martin" <nospam@nospam.com> wrote in message news:0d2f01c38667$9d528410$a001280a@phx.gbl... > Thanks, that's what I was looking for. > > Why aren't there any partial distinct func in SQL, in the > case we get all if col A, B, and C are the same. DB could > just use random to choose witch partial duplicate to > return. > > > >-----Original Message----- > >select * > >from #demo t1 > >where t1.c=(select max(c) from #demo t2 where t2.a=t1.a > and t2.b=t1.b) > > > >-- > >-oj > >Rac v2.2 & QALite! > > http://www.rac4sql.net > > > > > >"Martin" <nospam@nospam.com> wrote in message > >news:160201c3865d$79b659a0$a401280a@phx.gbl... > >> Thanks for the fast answer, but i was a bit unclear, i > >> need to fetch the id, and other columns from the table > >> aswell. > >> > >> CREATE TABLE #Demo ( > >> idNo int identity(1,1), > >> colA int, > >> colB int, > >> colC int, > >> colD int, > >> colE int, > >> colF int, > >> colG int > >> ) > >> > >> IF A and B are the same, only retrive the one with > >> highest C > >> > >> Thanks > >> > >> Martin > >> ********************************** > >> > >> > >> >-----Original Message----- > >> >Martin > >> >CREATE TABLE #Demo ( > >> > idNo int identity(1,1), > >> > colA int, > >> > colB int, > >> > colC int > >> >) > >> > > >> >INSERT INTO #Demo(colA,colB,colC) VALUES (1,6,1) > >> >INSERT INTO #Demo(colA,colB,colC) VALUES (1,6,2) > >> >INSERT INTO #Demo(colA,colB,colC) VALUES (2,4,1) > >> >INSERT INTO #Demo(colA,colB,colC) VALUES (3,3,1) > >> > > >> > > >> >SELECT colA,colB,max(colC)AS D FROM #Demo > >> >GROUP BY colA,colB > >> > > >> >"Martin" <nospam@nospam.com> wrote in message > >> >news:183301c38656$476f6ce0$3501280a@phx.gbl... > >> >> HI > >> >> > >> >> How can I retrieve only one row that has duplicates > on > >> >> not all columns? > >> >> For example a table Company could look like this. > >> >> > >> >> Id Zip Address Revenue > >> >> ---------------------------------------------------- > --- > >> --- > >> >> ------ > >> >> 1 1 Street 1 1 000 000 > >> >> 2 2 Street 2 2 000 000 > >> >> 3 3 Street 3 3 000 000 > >> >> 4 1 Street 1 1 500 000 > >> >> > >> >> > >> >> Company 1 and 4 have the same Address and I only > want > >> to > >> >> retrieve one company at the same Address, the one > with > >> >> the highest revenue. > >> >> > >> >> Best regards > >> >> > >> >> Martin > >> >> > >> > > >> > > >> >. > >> > > > > > > >. > >
Don't see what you're looking for? Try a search.
|