| Author |
Topic |
|
azmi
Starting Member
37 Posts |
Posted - 2006-03-16 : 21:00:08
|
| Hi all,I need your expertise and assistant to retrieve lastest bal_qty.I had create sproc to retrieve all the 'REC' doc but still hanging to retrive the latest bal_qty. There is some post input data for better understanding./*SAMPLE DATA*/sl_no doc_dt doc_type doc_no ref_doc_no bal_qty2 2006-02-07 REC 06RME000591 06EMR000772 16.000000003 2006-02-07 STR 06TRM000080 NULL 10.000000004 2006-02-08 STR 06TRM000084 NULL 5.00000000*5 2006-02-10 REC 06RME000686 06EMR000881 20.000000006 2006-02-10 STR 06TRM000092 NULL 10.000000007 2006-02-13 STR 06TRM000095 NULL 00000000*8 2006-02-14 REC 06RME000766 06EMR000971 15.000000009 2006-02-15 STR 06TRM000103 NULL 3.00000000*10 2006-02-17 REC 06RME000878 06EMR001120 18.0000000011 2006-02-17 STR 06TRM000111 NULL 8.0000000012 2006-02-18 STR 06TRM000115 NULL 3.00000000*13 2006-02-21 REC 06RME000947 06EMR001254 23.0000000014 2006-02-21 STR 06TRM000125 NULL 13.0000000015 2006-02-23 STR 06TRM000137 NULL 12.00000000*16 2006-02-24 REC 06RME001057 06EMR001370 30.00000000*17 2006-02-24 REC 06RME001058 06EMR001371 35.0000000018 2006-02-24 STR 06TRM000138 NULL 23.0000000019 2006-02-27 STR 06TRM000149 NULL 11.00000000*20 2006-02-28 REC 06RME001149 06EMR001528 31.0000000021 2006-02-28 STR 06TRM000152 NULL 26.0000000022 2006-03-02 STR 06TRM000159 NULL 20.00000000*23 2006-03-03 REC 06RME001266 06EMR001662 35.0000000024 2006-03-04 STR 06TRM000168 NULL 29.0000000025 2006-03-06 STR 06TRM000172 NULL 27.00000000*26 2006-03-07 REC 06RME001375 06EMR001819 42.0000000027 2006-03-07 STR 06TRM000176 NULL 30.0000000028 2006-03-10 STR 06TRM000190 NULL 20.00000000*29 2006-03-11 REC 06RME001488 06EMR001956 30.0000000030 2006-03-13 STR 06TRM000199 NULL 18.00000000*31 2006-03-14 REC 06RME001596 06EMR002078 33.00000000*REMARKS*-LATEST BALANCEfor your information fig. with mark * is the latest bal. Below is the result what i'am looking for./*RESULT*/SL_NO DOC_DT DOC_TYPE DOC_NO REF_DOC_NO BAL_QTY 2 2006-02-07 REC 06RME000591 06EMR000772 5.00000000 5 2006-02-10 REC 06RME000686 06EMR000881 0.000000008 2006-02-14 REC 06RME000766 06EMR000971 3.0000000010 2006-02-17 REC 06RME000878 06EMR001120 3.0000000013 2006-02-21 REC 06RME000947 06EMR001254 12.0000000016 2006-02-24 REC 06RME001057 06EMR001370 30.0000000017 2006-02-24 REC 06RME001058 06EMR001371 11.0000000020 2006-02-28 REC 06RME001149 06EMR001528 20.0000000023 2006-03-03 REC 06RME001266 06EMR001662 27.0000000026 2006-03-07 REC 06RME001375 06EMR001819 20.0000000029 2006-03-11 REC 06RME001488 06EMR001956 18.0000000031 2006-03-14 REC 06RME001596 06EMR002078 33.00000000Sory cause the data that i post not properly alignment. I had setting properly before preview and look nice, but when i preview the msg it's still not properly alignment.Anywhere i hope u can understand my problem..Thanks in advance. |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-03-16 : 21:09:29
|
quote: Sory cause the data that i post not properly alignment. I had setting properly before preview and look nice, but when i preview the msg it's still not properly alignment
refer to Forum FAQ http://www.sqlteam.com/forums/faq.aspHow do you identify the related records ?2 2006-02-07 REC 06RME000591 06EMR000772 16.000000003 2006-02-07 STR 06TRM000080 NULL 10.000000004 2006-02-08 STR 06TRM000084 NULL 5.00000000*5 2006-02-10 REC 06RME000686 06EMR000881 20.000000006 2006-02-10 STR 06TRM000092 NULL 10.000000007 2006-02-13 STR 06TRM000095 NULL 00000000* Like SL_NO 2, 3, 4 are related. 5, 6, 7 are related. KH |
 |
|
|
azmi
Starting Member
37 Posts |
Posted - 2006-03-16 : 22:57:27
|
| The sample data is taken from stock movement report.I still thinks how to indentify the record. According to the user, they want to retrieve REC doc only but the bal_qty will be the last before next item receive(REC doc) comes. What can i say is, bal_qty for SL_NO 2 will be next REC (SL_NO 5) - 1 = 4 that equal to 5. Any ideas...2 2006-02-07 REC 06RME000591 06EMR000772 5.000000005 2006-02-10 REC 06RME000686 06EMR000881 00000000 |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-03-16 : 23:26:58
|
as per your specified logic.select *, bal = (select top 1 bal_qty from yourtable x where x.sl_no >= t.sl_no and x.sl_no < isnull((select min(sl_no) from yourtable y where y.sl_no > t.sl_no and doc_type = 'REC'), t.sl_no + 1) order by doc_dt desc) from yourtable twhere doc_type = 'REC' KH |
 |
|
|
azmi
Starting Member
37 Posts |
Posted - 2006-03-17 : 04:45:23
|
| Thanks for your feedback. I try your logic first and let you know the status. Have a nice day... |
 |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2006-03-17 : 13:30:01
|
Hi all,Here's an alternative method which runs about 3 times faster on my machine and with azmi's sample data. I've provided a working example with both methods so you can see for yourself. I think the performance benefit will increase as the data size increases too, although I haven't tested that.azmi - Out of interest, how's the performance comparison for you (on your sample data and on your full data)?khtan - There might be a small bug with your method when the last record is 'STR' - easy to fix though, I'm sure Thanks!--This SQL script is safe to run--create and populate temporary tablecreate table #t (sl_no int CONSTRAINT pk PRIMARY KEY CLUSTERED, doc_dt smalldatetime, doc_type varchar(3), doc_no varchar(20), ref_doc_no varchar(20), bal_qty int)insert #t select 2, '20060207', 'REC', '06RME000591', '06EMR000772', 16union all select 3, '20060207', 'STR', '06TRM000080', 'NULL', 10union all select 4, '20060208', 'STR', '06TRM000084', 'NULL', 5union all select 5, '20060210', 'REC', '06RME000686', '06EMR000881', 20union all select 6, '20060210', 'STR', '06TRM000092', 'NULL', 10union all select 7, '20060213', 'STR', '06TRM000095', 'NULL', 0union all select 8, '20060214', 'REC', '06RME000766', '06EMR000971', 15union all select 9, '20060215', 'STR', '06TRM000103', 'NULL', 3union all select 10, '20060217', 'REC', '06RME000878', '06EMR001120', 18union all select 11, '20060217', 'STR', '06TRM000111', 'NULL', 8union all select 12, '20060218', 'STR', '06TRM000115', 'NULL', 3union all select 13, '20060221', 'REC', '06RME000947', '06EMR001254', 23union all select 14, '20060221', 'STR', '06TRM000125', 'NULL', 13union all select 15, '20060223', 'STR', '06TRM000137', 'NULL', 12union all select 16, '20060224', 'REC', '06RME001057', '06EMR001370', 30union all select 17, '20060224', 'REC', '06RME001058', '06EMR001371', 35union all select 18, '20060224', 'STR', '06TRM000138', 'NULL', 23union all select 19, '20060227', 'STR', '06TRM000149', 'NULL', 11union all select 20, '20060228', 'REC', '06RME001149', '06EMR001528', 31union all select 21, '20060228', 'STR', '06TRM000152', 'NULL', 26union all select 22, '20060302', 'STR', '06TRM000159', 'NULL', 20union all select 23, '20060303', 'REC', '06RME001266', '06EMR001662', 35union all select 24, '20060304', 'STR', '06TRM000168', 'NULL', 29union all select 25, '20060306', 'STR', '06TRM000172', 'NULL', 27union all select 26, '20060307', 'REC', '06RME001375', '06EMR001819', 42union all select 27, '20060307', 'STR', '06TRM000176', 'NULL', 30union all select 28, '20060310', 'STR', '06TRM000190', 'NULL', 20union all select 29, '20060311', 'REC', '06RME001488', '06EMR001956', 30union all select 30, '20060313', 'STR', '06TRM000199', 'NULL', 18union all select 31, '20060314', 'REC', '06RME001596', '06EMR002078', 33--khtan's methodselect *, bal = (select top 1 bal_qty from #t x where x.sl_no >= t.sl_no and x.sl_no < isnull((select min(sl_no) from #t y where y.sl_no > t.sl_no and doc_type = 'REC'), t.sl_no + 1) order by doc_dt desc) from #t twhere doc_type = 'REC'--alternative methodselect e.*, f.bal_qty as latest_bal_qty from ( select from_sl_no, max(c.sl_no) as to_sl_no from ( select max(a.sl_no) as from_sl_no, b.sl_no from #t a inner join #t b on a.sl_no <= b.sl_no and a.doc_type = 'REC' group by b.sl_no ) c group by from_sl_no ) d inner join #t e on d.from_sl_no = e.sl_no inner join #t f on d.to_sl_no = f.sl_no--tidy updrop table #t Ryan Randallwww.monsoonmalabar.comIdeas are easy. Choosing between them is the hard part. |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-03-17 : 19:55:51
|
"khtan - There might be a small bug with your method when the last record is 'STR' - easy to fix though, I'm sure "Thanks for pointing that out. KHChoice is an illusion, created between those with power, and those without.Concordantly, while your first question may be the most pertinent, you may or may not realize it is also the most irrelevant |
 |
|
|
azmi
Starting Member
37 Posts |
Posted - 2006-03-17 : 23:58:45
|
| I already test the script, both script working, that's what i'am looking for. Anywhere guys thanks for your ideas.. |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-03-18 : 00:08:28
|
Better use Ryan's script. Mine has a bug handling the last record KHChoice is an illusion, created between those with power, and those without.Concordantly, while your first question may be the most pertinent, you may or may not realize it is also the most irrelevant |
 |
|
|
azmi
Starting Member
37 Posts |
Posted - 2006-03-19 : 19:39:39
|
| yup, i use Ryan's script for my sproc. Thanks. |
 |
|
|
|
|
|