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 |
|
deepak23
Starting Member
8 Posts |
Posted - 2010-10-18 : 18:07:02
|
| Hi,I am trying to generate a random number based on a field.Below is a table I am trying to populate:FutureSegment CurrentSegment random snapshotdt today aging11 11 1 10/4/2010 10/13/2010 811 11 1 10/5/2010 10/13/2010 711 11 1 10/6/2010 10/13/2010 611b1a 11 1 10/7/2010 10/13/2010 511b1a 11b1a 2 10/8/2010 10/13/2010 411b1a 11b1a 2 10/9/2010 10/13/2010 311 11b1a 2 10/10/2010 10/13/2010 311 11 3 10/11/2010 10/13/2010 3x 11 3 10/12/2010 10/13/2010 2So if currentsegment = futuresegment then the random number should be the same as previous else it should incrementedAny suggestions on how I should go about?? |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-10-19 : 13:02:29
|
| what column determines the order in your table? ie. on what basis you determine previous record?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
deepak23
Starting Member
8 Posts |
Posted - 2010-10-20 : 00:02:32
|
| Its based on CurrentSegment, SnapshotDt. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-10-20 : 12:44:59
|
| are you using sql 2005?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
deepak23
Starting Member
8 Posts |
Posted - 2010-10-20 : 12:47:48
|
| Yes I am using SQL Server |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-10-20 : 12:52:03
|
quote: Originally posted by deepak23 Yes I am using SQL Server
I was asking on version. what doesSELECT @@VERSIONreturn?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
weisseb
Starting Member
8 Posts |
Posted - 2010-10-22 : 11:21:52
|
I assume that snapshotdt is the primary key and that it alone determines the order in your table (it is unique in the sample data). The solution below assumes you're using 2005 or later, and is based on Itzik Ben-Gan's work, see [url]http://www.sqlmag.com/article/sql-server/solution-to-the-t-sql-puzzle-grouping-consecutive-rows-with-a-common-element.aspx[/url]. I use a table @table that contains the data in your post. A proposed solution is as below:declare @table table( FutureSegment varchar(50), CurrentSegment varchar(50), random int, snapshotdt datetime, today datetime, aging int, primary key (snapshotdt))insert into @table values ('11', '11', 1, '10/4/2010', '10/13/2010', 8)insert into @table values ('11', '11', 1, '10/5/2010', '10/13/2010', 7)insert into @table values ('11', '11', 1, '10/6/2010', '10/13/2010', 6)insert into @table values ('11b1a', '11', 1, '10/7/2010', '10/13/2010', 5)insert into @table values ('11b1a', '11b1a', 2, '10/8/2010', '10/13/2010', 4)insert into @table values ('11b1a', '11b1a', 2, '10/9/2010', '10/13/2010', 3)insert into @table values ('11', '11b1a', 2, '10/10/2010', '10/13/2010', 3)insert into @table values ('11', '11', 3, '10/11/2010', '10/13/2010', 3)insert into @table values ('x', '11', 3, '10/12/2010', '10/13/2010', 2)select *, dense_rank() over (order by minsnapshotdtInSection) as Resultfrom( select *, rn - rnWithinCurrentSegment as WeakSectionNumber, min(snapshotdt) over ( partition by CurrentSegment, rn - rnWithinCurrentSegment ) as minsnapshotdtInSection from ( select *, row_number() over (order by snapshotdt) as rn, row_number() over ( partition by CurrentSegment order by snapshotdt ) as rnWithinCurrentSegment from @table ) as t1)as t2order by snapshotdtWorking from the inside out, t1 adds two columns to @table, one is rn that numbers the rows sorted by snapshotdt, the other, rnWithinCurrentSegment, does the same, but starts all over from 1 within each unique value of CurrentSegment. Subtracting these two guarentees a unique constant value within consecutive rows with the same CurrentSegment. I've named this column WeakSectionNumber, because two sections with different CurrentSegment values may be assigned the same section number, hence 'weak'. The column WeakSectionNumber is for debugging purposes only, I use the subtraction literally instead.Partitioning by the pair (CurrentSegment, WeakSectionNumber) we get unique (strong) segments, which allow us to find the minimum snapshotdt within each. Ultimately the dense_rank() used on the minimum snapshotdt in each section produces the requested ranks. |
 |
|
|
deepak23
Starting Member
8 Posts |
Posted - 2010-10-22 : 11:56:00
|
| Thanks for your response. I can kind of understand what you are telling. Will be looking into it more detail. That was just a snapshot of a single loan number. Basically I have 1.3 million such loans and each loan has been in our database for 50 days, moving in and out of segments. So trying to find the latest entry date into the segment. So i thought by grouping, I will be able to get that data. So the primary key is the loannum and SnapshotDt together. Hope this helps tp understand my problem better.Thanks again for your response. |
 |
|
|
weisseb
Starting Member
8 Posts |
Posted - 2010-10-23 : 06:05:01
|
| That would change the over clauses above considerably.Would it be possible for you to post an example where you atleast use two different loannums with several snapshotdts within each loannum (perhaps with overlapping snapshotdts in different loannums). And if you can provide the desired output/result value as you did last time. I don't quite get the 50 days and loans moving in and out of segments, so an example would be of great help. |
 |
|
|
deepak23
Starting Member
8 Posts |
Posted - 2010-10-23 : 15:36:44
|
| This is the tableDimSetId SnapshotDt LoanNum CurrentSegment155 6/16/2010 1123776 11b157 6/17/2010 1123776 11b159 6/20/2010 1123776 11b161 6/21/2010 1123776 11b163 6/22/2010 1123776 11b165 6/23/2010 1123776 11b167 6/24/2010 1123776 11b169 6/27/2010 1123776 11b171 6/28/2010 1123776 11b173 6/29/2010 1123776 11b175 6/30/2010 1123776 11b177 7/1/2010 1123776 11b179 7/4/2010 1123776 11b181 7/6/2010 1123776 11b183 7/7/2010 1123776 11b185 7/8/2010 1123776 11b187 7/11/2010 1123776 11b189 7/12/2010 1123776 11b191 7/13/2010 1123776 11b193 7/14/2010 1123776 11b195 7/15/2010 1123776 11b197 7/18/2010 1123776 11b199 7/19/2010 1123776 11b201 7/20/2010 1123776 11b203 7/21/2010 1123776 11b205 7/22/2010 1123776 11b207 7/25/2010 1123776 11b209 7/26/2010 1123776 11b211 7/27/2010 1123776 11b213 7/28/2010 1123776 11b215 7/29/2010 1123776 11b217 7/31/2010 1123776 11b219 8/2/2010 1123776 11b221 8/3/2010 1123776 11b223 8/4/2010 1123776 11b225 8/5/2010 1123776 11b227 8/8/2010 1123776 11b229 8/9/2010 1123776 11b231 8/10/2010 1123776 11b233 8/11/2010 1123776 11b235 8/12/2010 1123776 11b237 8/15/2010 1123776 11b239 8/16/2010 1123776 11b241 8/17/2010 1123776 11b2.1243 8/18/2010 1123776 11b2.1245 8/19/2010 1123776 11b2.1247 8/22/2010 1123776 11b2.1249 8/23/2010 1123776 11b2.1251 8/24/2010 1123776 11b2.1253 8/25/2010 1123776 11b2.1255 8/26/2010 1123776 11b2.1257 8/29/2010 1123776 11b2.1259 8/30/2010 1123776 11b2.1261 8/31/2010 1123776 11b2.1263 9/1/2010 1123776 11b2.1265 9/2/2010 1123776 11b2.1267 9/5/2010 1123776 11b2.1269 9/7/2010 1123776 11b2.1271 9/8/2010 1123776 11b2.1273 9/9/2010 1123776 11b2.1275 9/12/2010 1123776 11b2.1277 9/13/2010 1123776 11b2.1279 9/14/2010 1123776 11b2.1281 9/15/2010 1123776 11b2.1283 9/16/2010 1123776 11b2.1285 9/19/2010 1123776 11b2.1287 9/20/2010 1123776 11b2.1289 9/21/2010 1123776 11b2.1291 9/22/2010 1123776 11b2.1293 9/23/2010 1123776 11b2.1295 9/26/2010 1123776 11b2.1297 9/27/2010 1123776 11b2.1299 9/28/2010 1123776 11b2.1301 9/29/2010 1123776 11b2.1303 9/30/2010 1123776 11b305 10/3/2010 1123776 11b307 10/4/2010 1123776 11b309 10/5/2010 1123776 11b311 10/6/2010 1123776 11b313 10/7/2010 1123776 11b315 10/10/2010 1123776 11b317 10/12/2010 1123776 11b319 10/13/2010 1123776 11b321 10/14/2010 1123776 11b323 10/17/2010 1123776 11b325 10/18/2010 1123776 11b327 10/19/2010 1123776 11b329 10/20/2010 1123776 11b331 10/21/2010 1123776 11b155 6/16/2010 1123830 9c157 6/17/2010 1123830 9c159 6/20/2010 1123830 9c161 6/21/2010 1123830 9c163 6/22/2010 1123830 9c165 6/23/2010 1123830 9c167 6/24/2010 1123830 9c169 6/27/2010 1123830 9c171 6/28/2010 1123830 9c173 6/29/2010 1123830 9c175 6/30/2010 1123830 9c177 7/1/2010 1123830 9c179 7/4/2010 1123830 9c181 7/6/2010 1123830 9c183 7/7/2010 1123830 9c185 7/8/2010 1123830 9c187 7/11/2010 1123830 9c189 7/12/2010 1123830 9c191 7/13/2010 1123830 9c193 7/14/2010 1123830 9c195 7/15/2010 1123830 9c197 7/18/2010 1123830 9c199 7/19/2010 1123830 9c201 7/20/2010 1123830 9c203 7/21/2010 1123830 9c205 7/22/2010 1123830 9c207 7/25/2010 1123830 9c209 7/26/2010 1123830 9c211 7/27/2010 1123830 9c213 7/28/2010 1123830 9c215 7/29/2010 1123830 9c217 7/31/2010 1123830 9c219 8/2/2010 1123830 9c221 8/3/2010 1123830 9c223 8/4/2010 1123830 9c225 8/5/2010 1123830 9c227 8/8/2010 1123830 9c229 8/9/2010 1123830 9c231 8/10/2010 1123830 9c233 8/11/2010 1123830 9c235 8/12/2010 1123830 9c237 8/15/2010 1123830 9c239 8/16/2010 1123830 9c241 8/17/2010 1123830 9c243 8/18/2010 1123830 9c245 8/19/2010 1123830 9c247 8/22/2010 1123830 9c249 8/23/2010 1123830 9c251 8/24/2010 1123830 9c253 8/25/2010 1123830 9c1c255 8/26/2010 1123830 9c1c257 8/29/2010 1123830 9c1c259 8/30/2010 1123830 9c1c261 8/31/2010 1123830 9c1c263 9/1/2010 1123830 9c1c305 10/3/2010 1123830 9c307 10/4/2010 1123830 9c309 10/5/2010 1123830 9c311 10/6/2010 1123830 9c313 10/7/2010 1123830 9c315 10/10/2010 1123830 9c317 10/12/2010 1123830 9c319 10/13/2010 1123830 9c321 10/14/2010 1123830 9c323 10/17/2010 1123830 9c325 10/18/2010 1123830 9c327 10/19/2010 1123830 9c329 10/20/2010 1123830 9c331 10/21/2010 1123830 9cI am trying to calculate the aging of a loan from the date it entered the current segment. I was initially doing it by calculating Mindate and Maxdate. But I get an error for Mindate when a Loan Comes back into the same segment after few days.So for Loan Num 1123830 the aging in 9c as of 10/21/2010 should be - 14(10/21 -10/3). IS this clear? |
 |
|
|
weisseb
Starting Member
8 Posts |
Posted - 2010-10-24 : 07:08:11
|
Okay, I thought the value you requested was the random column in your original post, I misunderstood. Here's what you want (I think):select *, rn - rnWithinCurrentSegment as WeakSectionNumber, row_number() over ( partition by LoanNum, CurrentSegment, rn - rnWithinCurrentSegment order by SnapshotDt ) as agingfrom( select *, row_number() over (partition by LoanNum order by SnapshotDt) as rn, row_number() over ( partition by LoanNum, CurrentSegment order by SnapshotDt ) as rnWithinCurrentSegment from @TableWithLoanNum) as t1order by LoanNum, SnapshotDt As before rn - rnWithinCurrentSegment gives a unique value within each "run"/section of CurrentSegment (with constant CurrentSegment). But we need to include LoanNum in the partitions, so that row_number() starts from 1 for each new LoanNum.We now partition this result for each LoanNum, CurrentSegment and rn - rnWithinCurrentSegment. These partitions are your "runs". Within each such partition we use row_number() and order it by SnapshotDt. So the first row in a section is numbered 1, the second 2 and so forth. This is exactly the aging (in number of "row"-days, 14 days in your example).If you want the actual number of days (i.e. 18 days in your example), instead of the aging column, use min(SnapshotDt) over ( partition by LoanNum, CurrentSegment, rn - rnWithinCurrentSegment ) as minSnapshotDtInSection This will give you the minimum SnapshotDt within each section, and from an "outer" query use datediff(day, minSnapshotDtInSection, SnapshotDt) and you'll have the actual number of days.Hope that it helps you and that I understood you correctly this time. |
 |
|
|
deepak23
Starting Member
8 Posts |
Posted - 2010-10-25 : 10:14:11
|
| Thanks a lot for that script. it takes a lot of time to run the query when I am looking at a population over 11million. Anyway we could make the performance better? |
 |
|
|
weisseb
Starting Member
8 Posts |
Posted - 2010-10-26 : 04:53:33
|
| 11 million is not a lot, yet quite a bit. It's one of those in between numbers. Make sure you have "proper" indexes on the table. Especially an index on LoanNum and CurrentSegment if you haven't that already. |
 |
|
|
|
|
|
|
|