| Author |
Topic |
|
mahdi87_gh
Yak Posting Veteran
72 Posts |
Posted - 2010-04-04 : 07:09:19
|
| hii have a table named images[id (int), pic (image)]i want to write a query that return 3 record by random and also a record with id=1results must be in order by idit means that the first row have id=1 and 3 random records****<< I Love MTN.SH >>**** |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2010-04-04 : 07:15:35
|
[code]select top 1 id, pic from images where id = 1union allselect id, pic from (select top 3 id, pic from images where id <> 1 order by newid()) a[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
mahdi87_gh
Yak Posting Veteran
72 Posts |
Posted - 2010-04-04 : 07:32:27
|
| error:ORDER BY items must appear in the select list if the statement contains a UNION, INTERSECT or EXCEPT operator.****<< I Love MTN.SH >>**** |
 |
|
|
mahdi87_gh
Yak Posting Veteran
72 Posts |
Posted - 2010-04-04 : 07:35:59
|
| i tried this, it wroks,but i think there is a better way to solve the problemdrop table #icreate table #i(id int)insert into #i select id from images where id<>1 order by newid()select * from images g inner join (select 1 as id unionselect * from #i)son g.id=s.idorder by g.id****<< I Love MTN.SH >>**** |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2010-04-04 : 08:01:48
|
quote: Originally posted by mahdi87_gh error:ORDER BY items must appear in the select list if the statement contains a UNION, INTERSECT or EXCEPT operator.****<< I Love MTN.SH >>****
edited my post KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
mahdi87_gh
Yak Posting Veteran
72 Posts |
Posted - 2010-04-04 : 08:13:19
|
| thanks khtani appreciate your answer in msdn forum****<< I Love MTN.SH >>**** |
 |
|
|
mahdi87_gh
Yak Posting Veteran
72 Posts |
Posted - 2010-04-04 : 08:56:57
|
| hi againthe code works with my sql server (local on my computer)but it does'nt work with the sql server on my web serveri think their sql server 2005 is not updated.error:ORDER BY items must appear in the select list if the statement contains a UNION operator.do you know how to change the query so it can work on the server?****<< I Love MTN.SH >>**** |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2010-04-04 : 09:02:12
|
my edited query should work on SQL 2005. I think the SQL Server on the web server is still on SQL 2000tryselect top 1 id, pic from #images where id = 1union allselect id, pic from ( select top 3 id, pic, nid = newid() from #images where id <> 1 order by nid) a KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
mahdi87_gh
Yak Posting Veteran
72 Posts |
Posted - 2010-04-04 : 09:08:07
|
| it worksthanks again very very muchno,its sql server 2005 but i think it's not updated****<< I Love MTN.SH >>**** |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2010-04-04 : 09:13:33
|
i don't get that error on my SQL 2005 or even in the compatibility level 80. Only in SQL 2000 KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-04-05 : 03:39:34
|
quote: Originally posted by mahdi87_gh hii have a table named images[id (int), pic (image)]i want to write a query that return 3 record by random and also a record with id=1results must be in order by idit means that the first row have id=1 and 3 random records****<< I Love MTN.SH >>****
Try this tooselect top 4 id from your_tableorder by case when id=1 then 1 else 2 end,newid()MadhivananFailing to plan is Planning to fail |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2010-04-05 : 05:18:22
|
quote: Originally posted by khtan i don't get that error on my SQL 2005 or even in the compatibility level 80. Only in SQL 2000
So much for Compatibility mode then ... Dunno why folk "trust" to that ... if you gotta do a regression test anyway might as well do it with "native" compatibility mode on a new version.Sorry, didn't mean to hijack the thread </rant> |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-04-05 : 06:40:59
|
quote: Originally posted by Kristen
quote: Originally posted by khtan i don't get that error on my SQL 2005 or even in the compatibility level 80. Only in SQL 2000
So much for Compatibility mode then ... Dunno why folk "trust" to that ... if you gotta do a regression test anyway might as well do it with "native" compatibility mode on a new version.Sorry, didn't mean to hijack the thread </rant>
I hope my solution will work in all versions properlyMadhivananFailing to plan is Planning to fail |
 |
|
|
|