Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
mike123
Master Smack Fu Yak Hacker
1462 Posts |
Posted - 2007-09-07 : 18:03:06
|
| Hi,I have a query that brings back results according to a column "genderID" This is either a "1" or a "2".Is there a way to write the query so every 4th record is a 1, and every other record is a 2 ? I don't even know where to start on something like this, if its even possible (without creating a massive mess)Thanks again!mike123 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-09-07 : 19:06:43
|
Please post some sample data and the expected result. What do you mean by 4th record ? What is the sequence of ordering of record ? KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
mike123
Master Smack Fu Yak Hacker
1462 Posts |
Posted - 2007-09-07 : 20:37:54
|
hi Khtan,Here is some sample data, basically it is doing an order by on "DATE DESC". It is returning 4 genderID =2's and then a genderID =1 for every fifth record. It's a bit difficult to explain, so hopefully you follow me :)thanks very much!!mike123SELECT userID,genderID from tblUserDetails ORDER BY ????????? date DESCThe results could look like thisuserID / genderID / date114 / 2 / 09-15-2006113 / 2 / 09-14-2006112 / 2 / 09-13-2006111 / 2 / 09-12-2006103 / 1 / 09-04-2006110 / 2 / 09-11-2006109 / 2 / 09-10-2006108 / 2 / 09-09-2006107 / 2 / 09-08-2006102 / 1 / 09-03-2006tblUserDetails Sample DatauserID / genderID / date100 / 1 / 09-01-2006101 / 1 / 09-02-2006102 / 1 / 09-03-2006103 / 1 / 09-04-2006104 / 2 / 09-05-2006105 / 2 / 09-06-2006106 / 2 / 09-07-2006107 / 2 / 09-08-2006108 / 2 / 09-09-2006109 / 2 / 09-10-2006110 / 2 / 09-11-2006111 / 2 / 09-12-2006112 / 2 / 09-13-2006113 / 2 / 09-14-2006114 / 2 / 09-15-2006CREATE TABLE [dbo].[tblUserDetails]( [UserID] [int] IDENTITY(1,1) NOT NULL, [GenderID] [tinyint] NULL, [Date] [datetime] NULL ) |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-09-07 : 22:37:41
|
[code]CREATE TABLE [dbo].[#tblUserDetails]( [UserID] [int] NOT NULL, [GenderID] [tinyint] NULL, [Date] [datetime] NULL)INSERT INTO #tblUserDetailsSELECT 100 , 1 , '09-01-2006' UNION ALLSELECT 101 , 1 , '09-02-2006' UNION ALLSELECT 102 , 1 , '09-03-2006' UNION ALLSELECT 103 , 1 , '09-04-2006' UNION ALLSELECT 104 , 2 , '09-05-2006' UNION ALLSELECT 105 , 2 , '09-06-2006' UNION ALLSELECT 106 , 2 , '09-07-2006' UNION ALLSELECT 107 , 2 , '09-08-2006' UNION ALLSELECT 108 , 2 , '09-09-2006' UNION ALLSELECT 109 , 2 , '09-10-2006' UNION ALLSELECT 110 , 2 , '09-11-2006' UNION ALLSELECT 111 , 2 , '09-12-2006' UNION ALLSELECT 112 , 2 , '09-13-2006' UNION ALLSELECT 113 , 2 , '09-14-2006' UNION ALLSELECT 114 , 2 , '09-15-2006'SELECT *, CASE WHEN GenderID = 2 THEN seq + seq / 4 WHEN GenderID = 1 THEN (seq + 1) * 5 END AS new_seqFROM( SELECT UserID, GenderID, [Date], seq = row_number() OVER (PARTITION BY GenderID ORDER BY [Date] DESC) - 1 FROM #tblUserDetails) aORDER BY new_seq, seq/*UserID GenderID Date seq new_seq ----------- -------- ------------ ------ ----------- 114 2 2006-09-15 0 0 113 2 2006-09-14 1 1 112 2 2006-09-13 2 2 111 2 2006-09-12 3 3 103 1 2006-09-04 0 5 110 2 2006-09-11 4 5 109 2 2006-09-10 5 6 108 2 2006-09-09 6 7 107 2 2006-09-08 7 8 102 1 2006-09-03 1 10 106 2 2006-09-07 8 10 105 2 2006-09-06 9 11 104 2 2006-09-05 10 12 101 1 2006-09-02 2 15 100 1 2006-09-01 3 20 (15 row(s) affected)*/DROP TABLE #tblUserDetails[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
jen
Master Smack Fu Yak Hacker
4110 Posts |
Posted - 2007-09-08 : 14:01:11
|
| why is the gender value dependent on the ordering of rows?just curious--------------------keeping it simple... |
 |
|
|
mike123
Master Smack Fu Yak Hacker
1462 Posts |
Posted - 2007-09-08 : 21:28:03
|
| hey khtan, thanks very much !! I will try this out , and post results :)jen, I want to bring back a mixture of male and female profiles on this query, with more females ..I figure this would be a decent way to do it , open to any other suggestions as well |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-09-09 : 01:59:08
|
maybe for some matching services ? with gender ratio of 1:5 ? KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
|
|
|
|
|