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 |
reflex2dotnet
Yak Posting Veteran
99 Posts |
Posted - 2007-04-27 : 13:20:43
|
Hi Friends I am trying to do one thing in my SP It seems some trickyI am not getting an idea whether to do it in SP, or in codebehindYour valuable suggestions and ideas will be really helpfulI have a SP, which returns this 10 9 8 7 6 3 1 What i want to do is, to find the difference of 2 rows(eg 10-9) and if it is 1, then go to next result When it comes to 6, next diff is not 1, so i want to discard all the data after 6 so my final resultset will be 10 9 8 7 6 How can i do that What is the best way to do this?Can anyone plese help meThanks |
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2007-04-27 : 13:43:12
|
What results should be returned from this:109876321 ??is everything after the six still discarded, or do we show 3,2,1 now since there is a difference of 1 between all 3 rows?- Jeffhttp://weblogs.sqlteam.com/JeffS |
 |
|
reflex2dotnet
Yak Posting Veteran
99 Posts |
Posted - 2007-04-27 : 13:47:08
|
No, i was trying to discard everything after 6so rows will be 109876total 5 rowsCan we do this in SP, itself?Thanks |
 |
|
dinakar
Master Smack Fu Yak Hacker
2507 Posts |
Posted - 2007-04-27 : 13:49:10
|
You could do it either way.declare @t table (rowid int identity(1,1), col1 int, col2 int)insert into @t select 10 ,0 union allselect 9 ,0 union allselect 8 ,0 union allselect 7 ,0 union allselect 6 ,0 union allselect 3 ,0 union allselect 1 ,0 select * from @tupdate tset t.col2 = t.col1 - (select top 1 t2.col1 from @t t2 where t2.rowid > t.rowid order by t2.rowid asc)from @t tselect * from @tdelete from @t where rowid > (Select min(rowid) + 1 from @t where col2 > 1)select * from @t Dinakar NethiSQL Server MVP************************Life is short. Enjoy it.************************http://weblogs.sqlteam.com/dinakar/ |
 |
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2007-04-27 : 14:19:23
|
This is a little ugly, but it should work, all in 1 SELECT:-- sample data:declare @t table (x int)insert into @t select 10 union allselect 9 union allselect 8 union allselect 7 union allselect 6 union allselect 3 union allselect 2 union allselect 1 -- solution:select t.* from @t tinner join (select max(x) x from @t a where not exists (select * from @t b where a.x =b.x+1)) t2on t.x >= t2.x - Jeffhttp://weblogs.sqlteam.com/JeffS |
 |
|
reflex2dotnet
Yak Posting Veteran
99 Posts |
Posted - 2007-04-27 : 14:20:55
|
Thanks for the reply.I messed up, and i am not getting an idea how to proceedBelow is the SP whcih returns the resultsetSELECT Sum(dbo.test1.quantity) as [QTY], datepart(month,test2.orderdate), datepart(YEAR,test2.orderdate), (dbo.GetMonthofGivenDate(test2.orderdate) + ' ' + right(datepart(year,test2.orderdate),2))as [MONTH]FROM dbo.test1 INNER JOIN dbo.Test2 on dbo.test1.OrderID=dbo.Test2.OrderID Where (dbo.Test2.OrderDate Between convert(varchar, (getdate()-365), 1) AND convert(varchar, getdate(), 1)) AND dbo.test1.ID=@KeyGroup BY dbo.test1.ID, datepart(year,test2.orderdate), datepart(month,test2.orderdate), dbo.GetMonthofGivenDate(test2.orderdate), right(datepart(year,test2.orderdate),2), (dbo.GetMonthofGivenDate(test2.orderdate)+' '+right(datepart(year,test2.orderdate),2))Order by dbo.test1.ID, datepart(year,test2.orderdate) desc, datepart(month,test2.orderdate) desc and the resultgrid is something like this153--- 4---- 2007----APR 07241 3 2007 MAR 07315 2 2007 FEB 07251 1 2007 JAN 07188 12 2006 DEC 06172 11 2006 NOV 06What i am trying to do is, to limit upto this, as the remaining has diff more than 1153 4 2007 APR 07241 3 2007 MAR 07315 2 2007 FEB 07251 1 2007 JAN 07And also i am trying to get the average of 153, 241,315,251Can you please help me in this?Thanks a lot |
 |
|
dinakar
Master Smack Fu Yak Hacker
2507 Posts |
Posted - 2007-04-27 : 14:35:22
|
If you are trying to check the difference between values for datetime types, you should do it a litte differently.Dinakar NethiSQL Server MVP************************Life is short. Enjoy it.************************http://weblogs.sqlteam.com/dinakar/ |
 |
|
reflex2dotnet
Yak Posting Veteran
99 Posts |
Posted - 2007-04-27 : 14:38:34
|
I think, i have created a confusion for youSorry for thatWas worried how to post my code, so tried to explain in a textual style |
 |
|
dinakar
Master Smack Fu Yak Hacker
2507 Posts |
Posted - 2007-04-27 : 15:01:26
|
So do you still need just the difference in values or do you need to find the diff in month values?Dinakar NethiSQL Server MVP************************Life is short. Enjoy it.************************http://weblogs.sqlteam.com/dinakar/ |
 |
|
reflex2dotnet
Yak Posting Veteran
99 Posts |
Posted - 2007-04-27 : 15:26:50
|
I am trying to see the difference in valuesnot the month values4-3 ----> 1no date differencesAs it is a datetime field, is there any problem in getting that?do i need to change? |
 |
|
dinakar
Master Smack Fu Yak Hacker
2507 Posts |
Posted - 2007-04-27 : 19:21:16
|
try the code I posted. Insert the result of your SELECT query into a table variable and see how it works out.. declare @t table (rowid int identity(1,1), col1 int, col2 int, col3 int, col4 varchar(5), col6 varchar(2), col7 int)insert into @t select 153, 4, 2007, 'APR', '07',0 union allselect 241, 3 ,2007 ,'MAR' ,'07',0 union allselect 315, 2 ,2007 ,'FEB' ,'07',0 union allselect 251, 1 ,2007 ,'JAN' ,'07',0 union allselect 188, 12, 2006,'DEC' ,'06',0 union allselect 172, 11, 2006 ,'NOV' ,'06' ,0select * from @tupdate tset t.col7 = abs(t.col2 - (select top 1 t2.col2 from @t t2 where t2.rowid > t.rowid order by t2.rowid asc))from @t tselect * from @tdelete from @t where rowid > (Select min(rowid) from @t where col7 > 1)select * from @t Dinakar NethiSQL Server MVP************************Life is short. Enjoy it.************************http://weblogs.sqlteam.com/dinakar/ |
 |
|
reflex2dotnet
Yak Posting Veteran
99 Posts |
Posted - 2007-04-30 : 09:20:42
|
Thanks DinakarPefect. IT worked, and it is giving me the expected result.But this is extracitng from the result set, ie. we need the resultset for further filteration.How can i do so that, the resultset will be the last filtered one.ie, now i have to manually enter the result to the table variable.How can i do so that, the result set of first qeury getting inserted into table variable, and i will be geting the filtered output, directly from SPThank you very much for your help |
 |
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2007-04-30 : 09:52:35
|
Did you even try the code I posted? It's just a single SELECT. No need to move things around into table variables or temp tables or anything.- Jeffhttp://weblogs.sqlteam.com/JeffS |
 |
|
reflex2dotnet
Yak Posting Veteran
99 Posts |
Posted - 2007-04-30 : 10:30:45
|
I tried to do your query, earlier.But it was not giving any result.I think, i dont know how to use your idea properlythat why i messed, and ended in nothingTo be honest, i didnt get what you mean by that select statemnt,confused where shal i put that part in my SP?? now alsoThank you for all your support |
 |
|
dinakar
Master Smack Fu Yak Hacker
2507 Posts |
Posted - 2007-04-30 : 11:55:19
|
quote: Originally posted by reflex2dotnet Thanks DinakarPefect. IT worked, and it is giving me the expected result.But this is extracitng from the result set, ie. we need the resultset for further filteration.How can i do so that, the resultset will be the last filtered one.ie, now i have to manually enter the result to the table variable.How can i do so that, the result set of first qeury getting inserted into table variable, and i will be geting the filtered output, directly from SPThank you very much for your help
welcome. Declare @table2 table (columns...)insert into @table2 (columns...)select <columns...> from @table1 Dinakar NethiSQL Server MVP************************Life is short. Enjoy it.************************http://weblogs.sqlteam.com/dinakar/ |
 |
|
reflex2dotnet
Yak Posting Veteran
99 Posts |
Posted - 2007-05-01 : 09:37:58
|
quote: declare @t table (rowid int identity(1,1), col1 int, col2 int, col3 int, col4 varchar(5), col6 varchar(2), col7 int)insert into @t select 153, 4, 2007, 'APR', '07',0 union allselect 241, 3 ,2007 ,'MAR' ,'07',0 union allselect 315, 2 ,2007 ,'FEB' ,'07',0 union allselect 251, 1 ,2007 ,'JAN' ,'07',0 union allselect 188, 12, 2006,'DEC' ,'06',0 union allselect 172, 11, 2006 ,'NOV' ,'06' ,0select * from @tupdate tset t.col7 = abs(t.col2 - (select top 1 t2.col2 from @t t2 where t2.rowid > t.rowid order by t2.rowid asc))from @t tselect * from @tdelete from @t where rowid > (Select min(rowid) from @t where col7 > 1)select * from @t
ThanksI got it workedBut can you please explain what is the logic behind, so that, we can learn frm thati am confused for the 3,4 resultsetsif you can explain the logic behind this, it wil be a great help for newbies like meThanks |
 |
|
dinakar
Master Smack Fu Yak Hacker
2507 Posts |
Posted - 2007-05-01 : 12:37:20
|
hmm..let me think how i did it....i created a table with an extra column with default 0. then for each row, I am subtracting the value in col2 - (get the next value for the same column). The rowid column here is the key without which we cannot get the next value. for each row we know the rowid, so using a subquery we can find a value in the following row by using the rowid. for rowid1=1, i need to get the next rowid immediately >1. so i use an order by with top 1 to get the value in rowid = 2. once all the values are updated, i am just deleting the rows from the table where the difference (the value we calculated previously) is > 1. hope that was clear.Dinakar NethiSQL Server MVP************************Life is short. Enjoy it.************************http://weblogs.sqlteam.com/dinakar/ |
 |
|
reflex2dotnet
Yak Posting Veteran
99 Posts |
Posted - 2007-05-01 : 14:42:31
|
Thank you very much for your great mind to explainWill be a great help for newbies like meThanks to SQLTeam members!! |
 |
|
|
|
|
|
|