| Author |
Topic |
|
jjmusicpro
Yak Posting Veteran
79 Posts |
Posted - 2010-05-12 : 12:26:11
|
| I have a table of data such as belowTable (- = column)id/firstname/description/school/primary1111 - joe - test guy - 12 - no1111 - joe - test guy - 10 - no1111 - joe - test guy - 7 - no1112 - tom - test person - 8 - noIs there a way i can run a query to set the 'primary' column to 'yes' based on the lowest value out of all the uniq records for each person?So for example, it will grab all the 1111 id's, look at all the school values, the based on the lowest value, it will put a 'yes' in the primary column?Thanks guys! |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-05-12 : 12:28:23
|
| are you using SQL 2005?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
jjmusicpro
Yak Posting Veteran
79 Posts |
Posted - 2010-05-12 : 12:37:56
|
| SQL Server 2008 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-05-12 : 12:39:59
|
cool here you goUPDATE tSET t.primary='yes'FROM (SELECT ROW_NUMBER() OVER (PARTITION BY id,firstname,description ORDER BY school ASC) AS Seq,primary FROM Table)tWHERE t.Seq=1 ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
jjmusicpro
Yak Posting Veteran
79 Posts |
Posted - 2010-05-12 : 12:48:18
|
| Hummm that changed all my secondary_flag columns to 'yes'UPDATE sis_studentsSET sis_students.secondary_flag='yes'FROM (SELECT ROW_NUMBER() OVER (PARTITION BY external_id,school ORDER BY school ASC) AS Seq, secondary_flag FROM sis_students)tWHERE t.Seq=1It should do something like this below.Original Table1111 - joe - test guy - 12 - no1111 - joe - test guy - 10 - no1111 - joe - test guy - 7 - no1112 - tom - test person - 8 - no1112 - tom - test person - 55 - noConverted Table1111 - joe - test guy - 12 - no1111 - joe - test guy - 10 - no1111 - joe - test guy - 7 - yes1112 - tom - test person - 8 - yes1112 - tom - test person - 55 - no |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-05-12 : 12:50:03
|
quote: Originally posted by jjmusicpro Hummm that changed all my secondary_flag columns to 'yes'UPDATE sis_studentsSET sis_students.secondary_flag='yes'FROM (SELECT ROW_NUMBER() OVER (PARTITION BY external_id,school ORDER BY school ASC) AS Seq, secondary_flag FROM sis_students)tWHERE t.Seq=1
what as per sample data you told primary rite?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-05-12 : 12:51:18
|
quote: Originally posted by jjmusicpro Hummm that changed all my secondary_flag columns to 'yes'UPDATE sis_studentsSET sis_students.secondary_flag='yes'FROM (SELECT ROW_NUMBER() OVER (PARTITION BY external_id,school ORDER BY school ASC) AS Seq, secondary_flag FROM sis_students)tWHERE t.Seq=1It should do something like this below.Original Table1111 - joe - test guy - 12 - no1111 - joe - test guy - 10 - no1111 - joe - test guy - 7 - no1112 - tom - test person - 8 - no1112 - tom - test person - 55 - noConverted Table1111 - joe - test guy - 12 - no1111 - joe - test guy - 10 - no1111 - joe - test guy - 7 - yes1112 - tom - test person - 8 - yes1112 - tom - test person - 55 - no
please see my suggestion and one you used. there are lot of differences. either use as i suggested or give us correct query.------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
jjmusicpro
Yak Posting Veteran
79 Posts |
Posted - 2010-05-12 : 12:53:18
|
quote: Originally posted by visakh16
quote: Originally posted by jjmusicpro Hummm that changed all my secondary_flag columns to 'yes'UPDATE sis_studentsSET sis_students.secondary_flag='yes'FROM (SELECT ROW_NUMBER() OVER (PARTITION BY external_id,school ORDER BY school ASC) AS Seq, secondary_flag FROM sis_students)tWHERE t.Seq=1
what as per sample data you told primary rite?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
Yes may table layout is like below.table sis_studentsid (auto increment number)external_id (id of user)school (school number)secondary_flag (yes/no) by default these are all 'no' |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-05-12 : 12:57:23
|
| [code]UPDATE tSET t.secondary_flag='yes'FROM (SELECT ROW_NUMBER() OVER (PARTITION BY external_id ORDER BY school ASC) AS Seq, secondary_flagFROM sis_students)tWHERE t.Seq=1[/code]i hope your table structure wont change again!------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|