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.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Stored Procedure Help

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 tricky
I am not getting an idea whether to do it in SP, or in codebehind
Your valuable suggestions and ideas will be really helpful
I 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 me
Thanks

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2007-04-27 : 13:43:12
What results should be returned from this:

10
9
8
7
6
3
2
1

??

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?

- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

reflex2dotnet
Yak Posting Veteran

99 Posts

Posted - 2007-04-27 : 13:47:08
No, i was trying to discard everything after 6
so rows will be
10
9
8
7
6
total 5 rows
Can we do this in SP, itself?
Thanks
Go to Top of Page

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 all
select 9 ,0 union all
select 8 ,0 union all
select 7 ,0 union all
select 6 ,0 union all
select 3 ,0 union all
select 1 ,0
select * from @t

update t
set t.col2 = t.col1 - (select top 1 t2.col1 from @t t2 where t2.rowid > t.rowid order by t2.rowid asc)
from @t t
select * from @t

delete from @t where rowid > (Select min(rowid) + 1 from @t where col2 > 1)

select * from @t



Dinakar Nethi
SQL Server MVP
************************
Life is short. Enjoy it.
************************
http://weblogs.sqlteam.com/dinakar/
Go to Top of Page

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 all
select 9 union all
select 8 union all
select 7 union all
select 6 union all
select 3 union all
select 2 union all
select 1

-- solution:

select t.*
from @t t
inner join
(select max(x) x from @t a
where not exists (select * from @t b where a.x =b.x+1)) t2
on
t.x >= t2.x


- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

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 proceed
Below is the SP whcih returns the resultset
SELECT 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=@Key
Group 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 this

153--- 4---- 2007----APR 07
241 3 2007 MAR 07
315 2 2007 FEB 07
251 1 2007 JAN 07
188 12 2006 DEC 06
172 11 2006 NOV 06

What i am trying to do is, to limit upto this, as the remaining has diff more than 1

153 4 2007 APR 07
241 3 2007 MAR 07
315 2 2007 FEB 07
251 1 2007 JAN 07

And also i am trying to get the average of 153, 241,315,251

Can you please help me in this?
Thanks a lot
Go to Top of Page

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 Nethi
SQL Server MVP
************************
Life is short. Enjoy it.
************************
http://weblogs.sqlteam.com/dinakar/
Go to Top of Page

reflex2dotnet
Yak Posting Veteran

99 Posts

Posted - 2007-04-27 : 14:38:34
I think, i have created a confusion for you
Sorry for that
Was worried how to post my code, so tried to explain in a textual style
Go to Top of Page

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 Nethi
SQL Server MVP
************************
Life is short. Enjoy it.
************************
http://weblogs.sqlteam.com/dinakar/
Go to Top of Page

reflex2dotnet
Yak Posting Veteran

99 Posts

Posted - 2007-04-27 : 15:26:50
I am trying to see the difference in values
not the month values
4-3 ----> 1
no date differences
As it is a datetime field, is there any problem in getting that?
do i need to change?
Go to Top of Page

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 all
select 241, 3 ,2007 ,'MAR' ,'07',0 union all
select 315, 2 ,2007 ,'FEB' ,'07',0 union all
select 251, 1 ,2007 ,'JAN' ,'07',0 union all
select 188, 12, 2006,'DEC' ,'06',0 union all
select 172, 11, 2006 ,'NOV' ,'06' ,0

select * from @t

update t
set 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 t
select * from @t

delete from @t where rowid > (Select min(rowid) from @t where col7 > 1)

select * from @t


Dinakar Nethi
SQL Server MVP
************************
Life is short. Enjoy it.
************************
http://weblogs.sqlteam.com/dinakar/
Go to Top of Page

reflex2dotnet
Yak Posting Veteran

99 Posts

Posted - 2007-04-30 : 09:20:42
Thanks Dinakar
Pefect. 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 SP

Thank you very much for your help
Go to Top of Page

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.

- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

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 properly
that why i messed, and ended in nothing
To be honest, i didnt get what you mean by that select statemnt,
confused where shal i put that part in my SP?? now also

Thank you for all your support
Go to Top of Page

dinakar
Master Smack Fu Yak Hacker

2507 Posts

Posted - 2007-04-30 : 11:55:19
quote:
Originally posted by reflex2dotnet

Thanks Dinakar
Pefect. 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 SP

Thank you very much for your help


welcome.


Declare @table2 table (columns...)
insert into @table2 (columns...)
select <columns...> from @table1


Dinakar Nethi
SQL Server MVP
************************
Life is short. Enjoy it.
************************
http://weblogs.sqlteam.com/dinakar/
Go to Top of Page

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 all
select 241, 3 ,2007 ,'MAR' ,'07',0 union all
select 315, 2 ,2007 ,'FEB' ,'07',0 union all
select 251, 1 ,2007 ,'JAN' ,'07',0 union all
select 188, 12, 2006,'DEC' ,'06',0 union all
select 172, 11, 2006 ,'NOV' ,'06' ,0

select * from @t

update t
set 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 t
select * from @t

delete from @t where rowid > (Select min(rowid) from @t where col7 > 1)

select * from @t



Thanks
I got it worked
But can you please explain what is the logic behind, so that, we can learn frm that
i am confused for the 3,4 resultsets
if you can explain the logic behind this, it wil be a great help for newbies like me

Thanks
Go to Top of Page

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 Nethi
SQL Server MVP
************************
Life is short. Enjoy it.
************************
http://weblogs.sqlteam.com/dinakar/
Go to Top of Page

reflex2dotnet
Yak Posting Veteran

99 Posts

Posted - 2007-05-01 : 14:42:31
Thank you very much for your great mind to explain
Will be a great help for newbies like me
Thanks to SQLTeam members!!
Go to Top of Page
   

- Advertisement -