| Author |
Topic |
|
pcpham
Starting Member
10 Posts |
Posted - 2010-06-02 : 02:40:15
|
| Hi,I am trying to retrieve only rows 2 and 3 as Col3 occurs twice "b" but Col2 values are not the same ("a" and "d"). Where as row 7 and 8 should be ignored as the values in Col2 are the same.[Row]: [Col1], [Col2], [Col3], [Col4]1: 1, a, b, c2: 1, d, b, e3: 1, f, g, h4: 1, i, j, k5: 2, h, i, j6: 2, k, l, m7: 3, n, o, p8: 3, n, o, p9: 4, a, b, cThanks in advanced,Paula |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2010-06-02 : 03:33:56
|
Why not rows 1 and 3, instead of rows 2 and 3?What are the business rules for this? N 56°04'39.26"E 12°55'05.63" |
 |
|
|
pcpham
Starting Member
10 Posts |
Posted - 2010-06-02 : 06:29:55
|
| Col2 represents school name. Col3 represents schoolID. The both should be unique but data error has pointed the same schoolID to two different school names. So I need to retrieve this.Thanks.... |
 |
|
|
vaibhavktiwari83
Aged Yak Warrior
843 Posts |
Posted - 2010-06-02 : 06:38:25
|
| Thats correct but which school name you want to retrieve a or d thats what Peso asked why 2 and 3 instead of 1 and 3.Vaibhav TTo walk FAST walk ALONE To walk FAR walk TOGETHER |
 |
|
|
pcpham
Starting Member
10 Posts |
Posted - 2010-06-02 : 07:19:47
|
| Ok let me try and make the data more realistic. For every schoolid there supposed to be a unique schoolname except one of the records SchoolID3 has two different Schoolnames when they are only supposed to be 1. So I need to retrieve the records where the SchoolID has been repeated more than once with a different SchoolName assigned. In this case rows 3 and 4 has SchoolID3 belonging to two different Schools i.e. SchoolName3 and SchoolName4. It doesn't matter if the SchoolID occurs more than once as long as it has the same School name like rows 5 and 6. The query should be only interested in retrieving SchoolIDs that have been repeated but with different school names assigned.1: SchoolID1, SchoolName1, Student12: SchoolID2, SchoolName2, Student23: SchoolID3, SchoolName3, Student44: SchoolID3, SchoolName4, Student55: SchoolID4, SchoolName5, Student66: SchoolID4, SchoolName5, Student7Thanks,Paula |
 |
|
|
GopiMuluka
Starting Member
12 Posts |
Posted - 2010-06-02 : 07:37:15
|
| Check this queryDECLARE @Table TABLE ( SNo INT, SchoolID VARCHAR(50), SchoolName VARCHAR(50), StudentID VARCHAR(50))INSERT INTO @TableSELECT 1,'SchoolID1','SchoolName1','Student1' UNIONSELECT 2,'SchoolID2','SchoolName2','Student2' UNIONSELECT 3,'SchoolID3','SchoolName3','Student4' UNIONSELECT 4,'SchoolID3','SchoolName4','Student5' UNIONSELECT 5,'SchoolID4','SchoolName5','Student6' UNIONSELECT 6,'SchoolID4','SchoolName5','Student7' UNIONSELECT 7,'SchoolID3','SchoolName333','Student10' SELECT DISTINCT A.* FROM @Table AINNER JOIN @Table B ON A.SchoolID=B.SchoolID AND A.SchoolName<>B.SchoolName |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2010-06-02 : 07:47:28
|
Using Gopi's data (Thanks!) if you just want to know the SchoolID where there exist one or more different SchoolName values then:SELECT SchoolIDFROM @TableGROUP BY SchoolIDHAVING COUNT(DISTINCT SchoolName) > 1 (this should perform faster then Gopi's query if the table is large) and if you need to see the individual rows (to fix them) then JOIN that back to the original table:SELECT *FROM @Table AS T JOIN ( SELECT SchoolID FROM @Table GROUP BY SchoolID HAVING COUNT(DISTINCT SchoolName) > 1 ) AS X ON X.SchoolID = T.SchoolID |
 |
|
|
Ifor
Aged Yak Warrior
700 Posts |
Posted - 2010-06-02 : 07:54:44
|
Or, with one table scan:SELECT SchoolID, SchoolName, StudentIDFROM( SELECT * ,MIN(SchoolName) OVER (PARTITION BY SchoolID) AS MinName ,MAX(SchoolName) OVER (PARTITION BY SchoolID) AS MaxName FROM @Table) DWHERE MinName <> MaxName [Edit] ps You should check to see which option is most efficient with your data. [/Edit] |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2010-06-02 : 08:11:19
|
SELECT SchoolIDFROM @TableGROUP BY SchoolIDHAVING MIN(SchoolName) < MAX(SchoolName) N 56°04'39.26"E 12°55'05.63" |
 |
|
|
|
|
|