| Author |
Topic |
|
rajasekhar857
Constraint Violating Yak Guru
396 Posts |
Posted - 2009-09-08 : 04:41:57
|
| Select * from (SELECT MED_NAME AS MEDICATION,ETC,generic_drug_name_override,Row_number() OVER (PARTITION BY MED_NAMEORDER BY MED_MEDID_DESC ,med_routed_med_id_desc ) rnoFROM EMRMedicationsTPLkupWHERE UPPER(MED_NAME) = UPPER('Aspirin')AND STATUS = 'A' )where rno = 1error:Msg 156, Level 15, State 1, Line 10Incorrect syntax near the keyword 'where'. |
|
|
Nageswar9
Aged Yak Warrior
600 Posts |
Posted - 2009-09-08 : 04:44:21
|
| Hi, U missed the alias name for derived table :Select * from (SELECT MED_NAME AS MEDICATION,ETC,generic_drug_name_override,Row_number() OVER (PARTITION BY MED_NAMEORDER BY MED_MEDID_DESC ,med_routed_med_id_desc ) rnoFROM EMRMedicationsTPLkupWHERE UPPER(MED_NAME) = UPPER('Aspirin')AND STATUS = 'A' ) AS twhere rno = 1 |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2009-09-08 : 04:44:46
|
give an alias to the derived table... No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2009-09-08 : 04:45:24
|
 No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
rajasekhar857
Constraint Violating Yak Guru
396 Posts |
Posted - 2009-09-08 : 04:51:57
|
| hi same query not working in sql server 2000.how can i overcome this |
 |
|
|
senthil_nagore
Master Smack Fu Yak Hacker
1007 Posts |
|
|
rajasekhar857
Constraint Violating Yak Guru
396 Posts |
Posted - 2009-09-08 : 04:57:44
|
| can you give me query for this please |
 |
|
|
senthil_nagore
Master Smack Fu Yak Hacker
1007 Posts |
Posted - 2009-09-08 : 05:52:21
|
quote: Originally posted by rajasekhar857 can you give me query for this please
Try like thisSELECT top 1 MED_NAME AS MEDICATION,ETC,generic_drug_name_overrideFROM EMRMedicationsTPLkupWHERE UPPER(MED_NAME) = UPPER('Aspirin')AND STATUS = 'A' ORDER BY MED_MEDID_DESC ,med_routed_med_id_descSenthil.C------------------------------------------------------[Microsoft][ODBC SQL Server Driver]Operation canceledhttp://senthilnagore.blogspot.com/ |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2009-09-08 : 06:00:34
|
senthil nagoreso you are not using your own blogspot-solution?The given query returns only one row.The original query can give more rows but without "duplicates". No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
senthil_nagore
Master Smack Fu Yak Hacker
1007 Posts |
Posted - 2009-09-08 : 06:16:58
|
quote: Originally posted by webfred senthil nagoreso you are not using your own blogspot-solution?The given query returns only one row.The original query can give more rows but without "duplicates". No, you're never too old to Yak'n'Roll if you're too young to die.
Order by multiple column is not possible in that query!Senthil.C------------------------------------------------------[Microsoft][ODBC SQL Server Driver]Operation canceledhttp://senthilnagore.blogspot.com/ |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2009-09-08 : 07:41:39
|
I'm really not sure, but what about:create table #EMRMedicationsTPLkup(MED_NAME varchar(255),ETC varchar(255),generic_drug_name_override varchar(255),MED_MEDID_DESC int,med_routed_med_id_desc int,STATUS char(1))insert #EMRMedicationsTPLkupselect 'Aspirin', 'etc 1', 'generic 1',11,7,'A' union allselect 'Aspirin', 'etc 2', 'generic 5',41,20,'A' union allselect 'Aspirin', 'etc 3', 'generic 7',101,33,'A'-- old select using row_number()Select * from (SELECT MED_NAME AS MEDICATION,ETC,generic_drug_name_override,Row_number() OVER (PARTITION BY MED_NAMEORDER BY MED_MEDID_DESC ,med_routed_med_id_desc ) rnoFROM #EMRMedicationsTPLkupWHERE UPPER(MED_NAME) = UPPER('Aspirin')AND STATUS = 'A' ) AS twhere rno = 1-- new select without row_number()select * from(SELECT MED_NAME AS MEDICATION,ETC,generic_drug_name_override,(select count(*) from #EMRMedicationsTPLkup t2 WHERE t2.MED_NAME <= t.MED_NAME AND t2.MED_MEDID_DESC <= t.MED_MEDID_DESC AND t2.ETC <= t.ETC AND t2.generic_drug_name_override <= t.generic_drug_name_override AND t2.med_routed_med_id_desc <= t.med_routed_med_id_desc AND t2.MED_NAME = 'Aspirin') as rnofrom #EMRMedicationsTPLkup t)dtwhere rno = 1drop table #EMRMedicationsTPLkup No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
|