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 |
|
sunsanvin
Master Smack Fu Yak Hacker
1274 Posts |
Posted - 2007-06-12 : 01:46:00
|
| Dear experts,I've a long query, which is returning from_no and to_no as well as some other columns....the result is like this1001.0000 15003001.0000 35001.0000 010001.0000 10001.0000 10001.0000 10001.0000 10001.0000 10001.0000 10001001.0000 020001001.0000 020001001.0000 20001001.0000 20001001.0000 20001001.0000 20001001.0000 20002001.0000 30002001.0000 30002501.0000 035003001.0000 40003501.0000 065003501.0000 45004001.0000 050004001.0000 50005001.0000 60005001.0000 60006701.0000 077007001.0000 80007501.0000 0085007501.0000 085008001.0000 090008501.0000 0950010501.0000 1150012501.0000 1350013501.0000 1450014001.0000 1500024501.0000 2550034501.0000 35500104001.0000 105000135001.0000 136000155501.0000 156500157001.0000 1580003751.0000 48001001.0000 25003501.0000 50005001.0000 065006001.0000 750013001.0000 145001.0000 20001.0000 20001.0000 20001.0000 20001501.0000 35002001.0000 040002501.0000 45004001.0000 60004501.0000 65006001.0000 80007501.0000 95008001.0000 1000014501.0000 1650014501.0000 1650037501.0000 3950073001.0000 7500073001.0000 7500073001.0000 750001.0000 25001001.0000 035001001.0000 035002001.0000 45003501.0000 060007701.0000 102008501.0000 1100012001.0000 1450035001.0000 3750035501.0000 3800056501.0000 5900060001.0000 6250079501.0000 8200087501.0000 90000138801.0000 1413001.0000 01000393001.0000 39600072751.0000 7620032501.0000 3600024001.0000 2800043001.0000 47000146301.0000 1503001.0000 50002501.0000 075002501.0000 75003501.0000 085006001.0000 1100011501.0000 1650014001.0000 1900027001.0000 3200027501.0000 3250032501.0000 3750032501.0000 3750038001.0000 4300055501.0000 6050056501.0000 6150062501.0000 6750076201.0000 8120076501.0000 8150081201.0000 8620090001.0000 9500095001.0000 10000099501.0000 104500136001.0000 141000141301.0000 146300217501.0000 222500222501.0000 227500245801.0000 250800251501.0000 256500377001.0000 382000377001.0000 382000388001.0000 393000423501.0000 428500483501.0000 488500561001.0000 566000662501.0000 667500991001.0000 996000608001.0000 61400057201.0000 64700641001.0000 648500913301.0000 920800143601.0000 151100165001.0000 173500165001.0000 1735001.0000 610023001.0000 3300023001.0000 3300023001.0000 3300023001.0000 3300026001.0000 3600026001.0000 3600037501.0000 4750037501.0000 4750045501.0000 5550064701.0000 7470075001.0000 8500075001.0000 8500076001.0000 8600081501.0000 91500108601.0000 118600253601.0000 263600320013.0000 330012320501.0000 330500386001.0000 396000614001.0000 624000996001.0000 99990010001.0000 20000120001.0000 130000120001.0000 13000020001.0000 32500263601.0000 27610049001.0000 6400064001.0000 79000283001.0000 298000283001.0000 298000413001.0000 428000830001.0000 845000173501.0000 191000173501.0000 1910005001.0000 2500012001.0000 3200017001.0000 3700017001.0000 3700061001.0000 81000184001.0000 204000184001.0000 204000188001.0000 208000188001.0000 208000231001.0000 251000231001.0000 251000285601.0000 305600285601.0000 305600285601.0000 30560025001.0000 5000025001.0000 5000025001.0000 5000025001.0000 5000025001.0000 5000025001.0000 50000118601.0000 143600151101.0000 176100151101.0000 1761001.0000 300001.0000 30000966501.0000 999900966501.0000 9999001.0000 100001.0000 10000584501.0000 619500382001.0000 422000382001.0000 422000488501.0000 528500566001.0000 606000898001.0000 938000898001.0000 93800020001.0000 6660020001.0000 6660060001.0000 110000428001.0000 478000444001.0000 494000605501.0000 645000675001.0000 685500783401.0000 833400783401.0000 833400783401.0000 833400176101.0000 226100916501.0000 966500916501.0000 966500916501.0000 966500619501.0000 689500845001.0000 915000298001.0000 378000298001.0000 378000291501.0000 391500291501.0000 391500966501.0000 9999001.0000 66600752601.0000 9999001.0000 120000130001.0000 152700752601.0000 9999001.0000 152700now my problem is.........if the difference between the from_no and to_no is greater than 10,000 then for every 10,000 the record should be splitted into another record...suppose one record is 2,000 to 22,000 thenex: 2000-12000,12001-22000it should split the record like that....thank you in advance.... |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-06-12 : 02:52:54
|
[code]declare @table table( from_no int, to_no int)insert into @tableselect 2000, 22000 union allselect 3000, 23001 union allselect 3001, 3500select t.from_no, t.to_no, new_from = from_no + ((n * 10000) + 1), new_to = case when n < no_rows - 1 then from_no + ((n + 1) * 10000) else to_no endfrom ( select from_no, to_no, diff = to_no - from_no, no_rows = (to_no - from_no) / 10000 + case when (to_no - from_no) % 10000 <> 0 then 1 else 0 end from @table ) t left join ( select n = 0 union all select n = 1 union all select n = 2 union all select n = 3 union all select n = 4 ) n on n < no_rows[/code] KH |
 |
|
|
sunsanvin
Master Smack Fu Yak Hacker
1274 Posts |
Posted - 2007-06-12 : 03:01:33
|
| Thank you very much for the query KH,actually i've taken the values as an example.....but at runtime,i've to take the values from that table....and those both are varchar values...please consider me and tell me the query for the runtime valuesthank you very much |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-06-12 : 03:06:06
|
quote: but at runtime,i've to take the values from that table...
Just replace the @table with your query. Or insert the result of your query into a temp table or table variablequote: those both are varchar values...
well, then you have to convert it to numeric. use convert() KH |
 |
|
|
sunsanvin
Master Smack Fu Yak Hacker
1274 Posts |
Posted - 2007-06-12 : 04:09:50
|
| Dear KH,after replacing my query in the place of @table,I'm getting error.my query is like this.SELECT DISTINCT VIEWC.Org_Id Company_Id,VIEWC.Name Company_Name,VIEWB.Org_Id Branch_Id,VIEWB.Name Branch_Name,SALOD.COLUMN004 SO_Number,SALOD.COLUMN040 PO_No,SALOD.COLUMN041 PO_DATE , PROCG.COLUMN004 Job_Description,SALIT.COLUMN008 Totalnoof_Leaves,--DNDTL.COLUMN003EE Totalnoof_Books,SALPF.COLUMN001EE Serial_No, SALPF.COLUMN002EE From_No,SALPF.COLUMN003EE To_No,VIEW2.CODE Item_Code,VIEW2.NAME Item_Name,SALOD.COLUMN001 SO_Id,vcust1.column001 Customer_ID, VIEW1.column003 CUSTOMER_NAME,vcust1.column003 CUSTOMER_NAME1FROM QATABLE049 SALOD INNER JOIN QATABLE052 SALIT ON SALIT.COLUMN002=SALOD.COLUMN001INNER JOIN VITEM VIEW2 ON VIEW2.ITEM_ID=SALIT.COLUMN004INNER JOIN QATABLE038 VIEW1 ON VIEW1.COLUMN001=SALOD.COLUMN008LEFT JOIN YBTABLE079 PMSCI ON PMSCI.COLUMNB01=SALOD.COLUMN001LEFT JOIN YBTABLE080 PMSCH ON PMSCH.COLUMN002=PMSCI.COLUMN001LEFT JOIN YBTABLE052 PROCG ON PROCG.COLUMN007=PMSCH.COLUMN001--INNER JOIN MATABLE108 DNOTE ON DNOTE.COLUMNB01=SALOD.COLUMN001--INNER JOIN MATABLE110 DNDTL ON DNDTL.COLUMN002=DNOTE.COLUMN001INNER JOIN QATABLE143 SALPF ON SALPF.COLUMN004=SALOD.COLUMN001 AND SALPF.COLUMN005=SALIT.COLUMN004INNER join QATABLE038 vcust1 on vcust1.COLUMNB01=SALPF.COLUMNB03INNER JOIN vOrganization VIEWB ON VIEWB.ORG_ID = SALOD.COLUMN002 INNER JOIN vOrganization VIEWC ON VIEWC.Org_Id = VIEWB.Top_Parentwhere SALOD.COLUMN001='ab375a93-a0cb-494d-9601-e86838edfa4a' and vcust1.column001='01841c25-fc69-40b6-a55a-c547b40d8fd9' please guide meVinodEven you learn 1%, Learn it with 100% confidence. |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-06-12 : 04:15:14
|
try the temp table approachcreate table #temp (from_no int, to_no int)insert into #temp (from_no, to_no)select <from column>, <to column>from( <your query here>) t KH |
 |
|
|
sunsanvin
Master Smack Fu Yak Hacker
1274 Posts |
Posted - 2007-06-12 : 04:46:46
|
| Dear KH,now i'm getting the from_no,to_no only....is it not possible to get the result in the desierd result....means....to check the difference between the to numbers, and if it is greater than 10000, it has to take first 10000 , then second 10000....like thatVinodEven you learn 1%, Learn it with 100% confidence. |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-06-12 : 05:03:52
|
didn't my query did that ? KH |
 |
|
|
|
|
|
|
|