Author |
Topic |
m4tt
Starting Member
13 Posts |
Posted - 2008-03-04 : 08:41:06
|
Im trying to write a statement that only selects one of the possible rows that could be given.For example:Car ID: Colours Available:1 Blue1 Green1 BlackI want the statement to return: Car ID: Colours Available:1 Blue(Random Selection)Is this possible?Thanks |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-03-04 : 08:42:52
|
SELECT ID,MAX(Colours Available)FROM CarGROUP BY ID |
 |
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2008-03-04 : 08:42:58
|
[code]Select top 1 * from tableorder by newid()[/code]Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
m4tt
Starting Member
13 Posts |
Posted - 2008-03-04 : 09:05:08
|
harsh, you idea seems to work well for me, but how can i do it if i want results for more cars, eg;Car ID: Colours Available:1 Blue1 Green1 Black2 Blue2 RedResult:1 Black2 Blue |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-03-04 : 09:10:39
|
[code]SELECT t.ID ,t1.ColorFROM (SELECT DISTINCT ID FROM Car) tCROSS APPLY (SELECT TOP 1 Color FROM Car WHERE ID=t.ID ORDER BY NEWID())t1 [/code] |
 |
|
blindman
Master Smack Fu Yak Hacker
2365 Posts |
Posted - 2008-03-04 : 09:41:18
|
In SQLSVR 2005:select CarID, Colorfrom --Subquery (select ROW_NUMBER() OVER (PARTITION BY CarID ORDER BY NewID()) as ROWNUMBER, CarID, Color from Table1) Subquerywhere Subquery.ROWNUMBER = 1 e4 d5 xd5 Nf6 |
 |
|
blindman
Master Smack Fu Yak Hacker
2365 Posts |
Posted - 2008-03-04 : 09:51:36
|
...or even more obfuscatory better:;with SubQuery as(select ROW_NUMBER() OVER (PARTITION BY CarID ORDER BY NewID()) as ROWNUMBER, CarID, Colorfrom Table1)select CarID, Colorwhere ROWNUMBER = 1 e4 d5 xd5 Nf6 |
 |
|
megbot
Starting Member
17 Posts |
Posted - 2008-03-04 : 11:42:19
|
quote: Originally posted by harsh_athalye
Select top 1 * from tableorder by newid() Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED"
You can return however many you want. Select top 2 * from table, etc. http://msdn2.microsoft.com/en-us/library/ms189463.aspx:)Meg |
 |
|
m4tt
Starting Member
13 Posts |
Posted - 2008-03-04 : 12:03:32
|
Ive had a little bit of luck with getting newid() to work but its not giving me exatly what i want.The actual system im trying to build is a computerised personal trainer (for working out at the gym)Im trying to create somethine like this:Day: ExerciseOrder: Muscle: Exercise:1 1 Chest ChestEx11 2 Chest ChestEx21 3 Back BackEx41 4 Back Back Ex22 1 Legs LegsEx12 2 Legs LegsEx62 3 Arms ArmsEx42 4 Arms ArmsEx2My DB has: a table with the days:Day: ExerciseOrder: Muscle: 1 1 Chest 1 2 Chest 1 3 Back 1 4 Back 2 1 Legs 2 2 Legs 2 3 Arms 2 4 Arms And i have a table of exercises:Muscle: Exercise:Chest ChestEx1Chest ChestEx2Back BackEx1Back Back Ex2Legs LegsEx1Legs LegsEx2Arms ArmsEx1Arms ArmsEx2So i need the view to get the information from the table with the day, then for each row i need to select a random exercise that matches with the muscle.Any ideas? Many, Many, Many Thanks |
 |
|
blindman
Master Smack Fu Yak Hacker
2365 Posts |
Posted - 2008-03-04 : 12:15:33
|
If you are using 2005, my solutions should work for you.e4 d5 xd5 Nf6 |
 |
|
m4tt
Starting Member
13 Posts |
Posted - 2008-03-04 : 12:42:28
|
I am using 2005, i only have a tiny amount of experience with databases so am struggling a bit. Could you show me the statement i would need for these tables:Days:Day_NoEx_OrderMuscle_IDExercises:Muscle_IDExercies_IDthanks again! |
 |
|
blindman
Master Smack Fu Yak Hacker
2365 Posts |
Posted - 2008-03-04 : 13:06:04
|
[code];with RandomExercises as(select ROW_NUMBER() OVER (PARTITION BY Days.Day_No, Days.Ex_Order ORDER BY NewID()) as ROWNUMBER, Days.Day_No, Days.Ex_Order, Days.Muscle_ID, Exercises.Exercise_IDfrom Days inner join Exercises on Days.Muscle_ID = Exercises.Muscle_ID)select Day_No, Ex_Order, Muscle_ID, Exercise_IDfrom RandomExerciseswhere ROWNUMBER = 1[/code]e4 d5 xd5 Nf6 |
 |
|
m4tt
Starting Member
13 Posts |
Posted - 2008-03-04 : 14:01:57
|
Thanks for the replyIm getting the error: The OVER SQL construct or statement is not supported.The query still works, but i just get 1 line. I really need an exercise being randomly generated for each of the rows. Im not sure if the error is stopping that. Heres how i have the statement typed:WITH RandomExercises AS (SELECT ROW_NUMBER() OVER (PARTITION BY dbo.user_splits.Day_No, dbo.user_splits.Exercise_Order ORDER BY NewID()) AS ROWNUMBER, dbo.user_splits.Day_No, dbo.user_splits.Exercise_Order, dbo.user_splits.Muscle_Group_ID, dbo.users_exercises.R_Exercise_IDFROM dbo.user_splits INNER JOIN dbo.users_exercises ON dbo.user_splits.Muscle_Group_ID = dbo.users_exercises.Muscle_Group_ID) SELECT Day_No, Exercise_Order, Muscle_Group_ID, R_Exercise_ID FROM RandomExercises WHERE ROWNUMBER = 1 |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-03-04 : 14:14:08
|
If you really are using Microsoft SQL Server 2005, change your compatibility level to 90 for the current database. E 12°55'05.25"N 56°04'39.16" |
 |
|
m4tt
Starting Member
13 Posts |
Posted - 2008-03-04 : 14:39:30
|
I am def using 2005 and it is sent on level 90. |
 |
|
blindman
Master Smack Fu Yak Hacker
2365 Posts |
Posted - 2008-03-04 : 14:48:58
|
Post the DDL for your tables.e4 d5 xd5 Nf6 |
 |
|
m4tt
Starting Member
13 Posts |
Posted - 2008-03-04 : 15:00:57
|
ok, how do i get the dll? this is all still very new to me.thanks |
 |
|
m4tt
Starting Member
13 Posts |
Posted - 2008-03-05 : 05:03:00
|
I am now trying this statement but still only getting one line as a result that randomly generates the exercise.What can u put in to give all (dbo.user_splits.Exercise_Order, dbo.user_splits.User_ID, dbo.user_splits.Split_ID, dbo.user_splits.Day_No) and get last column randomly generating for all of themThanks for your help guys.Statement:SELECT dbo.user_splits.Exercise_Order, dbo.user_splits.User_ID, dbo.user_splits.Split_ID, dbo.user_splits.Day_No, (SELECT TOP (1) R_Exercise_ID FROM dbo.users_exercises ORDER BY NEWID()) AS Exercise, dbo.user_splits.Muscle_Group_IDFROM dbo.user_splits INNER JOIN dbo.users_exercises AS users_exercises_1 ON dbo.user_splits.Muscle_Group_ID = users_exercises_1.Muscle_Group_ID |
 |
|
elancaster
A very urgent SQL Yakette
1208 Posts |
Posted - 2008-03-05 : 05:11:44
|
how are you running the query? is it in a query window or with the query designer? I only ask coz a quick google of your error suggests that it may be a problem if you're trying to use the 'query designer'Em |
 |
|
m4tt
Starting Member
13 Posts |
Posted - 2008-03-05 : 05:21:24
|
im just using the view builder tool. |
 |
|
elancaster
A very urgent SQL Yakette
1208 Posts |
Posted - 2008-03-05 : 05:25:01
|
then perhaps that is the reason blindman's code didn't work for you. try it again in a blank query windowEm |
 |
|
Next Page
|