| Author |
Topic |
|
ultraman
Starting Member
18 Posts |
Posted - 2004-11-11 : 13:21:57
|
| Hi !I have a table with 3 field (temp table, no PK). It looks like this :col1|col2|col3001|A|.087001|B|.032001|C|.345002|A|.324002|B|.724003|A|.088003|C|.899001|A|.087I'd like to retreive the lines that have the same values for col1 and col2 since I'd like to copy the data in another table where those 2 fields are the PK. I know it's something with the COUNT() function and an aggregate, but I can't figure exactly how... Can someone help me ?--------------Ultraman |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2004-11-11 : 13:30:04
|
| select col1,col2from yourtablegroup by col1,col2do you need to include any values from col3? i.e., the MIN or the MAX or the SUM or something like that?- Jeff |
 |
|
|
Seventhnight
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2004-11-11 : 14:54:16
|
Select col1, col2, count(*) from yourTable Group By col1, col2 Having count(*)>1Corey |
 |
|
|
ultraman
Starting Member
18 Posts |
Posted - 2004-11-12 : 10:06:48
|
| Thanks guys, that was too easy.... Now I have some bigger problems. I want to copy the rows from this tables with duplicates into a table where the first 2 cols are the key. That means I have to filter duplicates--------------Ultraman |
 |
|
|
Seventhnight
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2004-11-12 : 10:09:32
|
if the remaining columns are the exact same as in your example:col1|col2|col3001|A|.087001|B|.032001|C|.345002|A|.324002|B|.724003|A|.088003|C|.899001|A|.087Then this should work for youInsert into yourNewTableSelect distint co1, col2, col3 from yourOldTableCorey |
 |
|
|
ultraman
Starting Member
18 Posts |
Posted - 2004-11-12 : 10:24:44
|
| Well that's my problem, from what I see the col3 can have 2 distinct values. That's an error in the business process I think. Anyway if col3 have different values, it's impossible to determine which one to take. It would be just a random pick....Thanks again, you've been very helpful.--------------Ultraman |
 |
|
|
ultraman
Starting Member
18 Posts |
Posted - 2004-11-12 : 10:44:03
|
| Another question would be : how do I get only the records that have the same 2 first col but different col3 ?--------------Ultraman |
 |
|
|
Seventhnight
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2004-11-12 : 11:36:35
|
[code]Select col1, col2, col3From yourTable AInner Join ( Select col1, col2, count(*) from yourTable Group By col1, col2 Having count(*)>1 ) BOn A.col1 = B.col1and A.col2 = B.col2Group By A.col1, A.col2, A.col3Having count(*)=1[/code]Corey |
 |
|
|
ultraman
Starting Member
18 Posts |
Posted - 2004-11-12 : 11:54:06
|
| Oh man I'm impressed ! You're very good, it worked exactly as I want ! This is a very powerful language but it can be quite complex for a begginer.--------------Ultraman |
 |
|
|
Seventhnight
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2004-11-12 : 13:57:41
|
Well I thank your for the compliment Upon rereading the last query, I think a simplier query might be more reliable.The last query I provided could provide the wrong result with the following result setcol1 col2 col3001 A .087001 A .023001 A .087 the query would return only the middle rowcol1 col2 col3001 A .023 So try the following query and see if it will work for you:Select Distinct A.col1, A.col2, A.col3From yourTable AInner Join yourTable BOn A.col1 = B.col1and A.col2 = B.col2Where A.col3 <> B.col3 Corey |
 |
|
|
|