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)
 SQL Query Help

Author  Topic 

dupati1
Posting Yak Master

123 Posts

Posted - 2005-06-16 : 14:25:25
Guys,

I have this sql query requirement that looks daunting to me...

Here is the situation...

I have the following table

EquipNum EquipStatus
_______________________
2346732049 Y
2346732050 Y
2346732051 Y
2346732052 N
2346732053 Y
2346732054 Y
2346732055 Y
2346732056 Y
2346732057 Y
---- and so on

Ok now i have a form with two fields (1)Enumber (2)Erange

The user enters the first 6 numerals of the Equipnumber in the Enumber field and enter the number of comsecutive numbers he/she wants in the Erange field...

For example...the user may enter 234673 and 3

it means that he wants all the 3 or more blocks of the consecutive numbers with the equipment numbers starting with 234673 and with EquipStatus as Y...

for the above data...the result should look like

EquipNum EquipStatus
_______________________
2346732049 Y
2346732050 Y
2346732051 Y

2346732053 Y
2346732054 Y
2346732055 Y
2346732056 Y
2346732057 Y

another example may be 234673 and 5 then the results should be

EquipNum EquipStatus
_______________________
2346732053 Y
2346732054 Y
2346732055 Y
2346732056 Y
2346732057 Y

can anyone help me writing this query...

Thanks

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2005-06-16 : 14:35:58
Please see this weblog which shows how to post your question with DDL and sample data:
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Tara
Go to Top of Page

dupati1
Posting Yak Master

123 Posts

Posted - 2005-06-16 : 14:52:33
Ok thanks...

here is the DDL of my table

CREATE EquipTable( EquipNum float(8), EquipStatus Char(1) )

Sample Data:
INSERT INTO EquipTable(EquipNum,EquipStatus) VALUES(2346732057,'Y')

I have tried something like this so far...

SELECT Equipnum, EquipStatus FROM EquipTable
WHERE Convert(varchar(10),Equipnum) LIKE '234673%' AND EquipStatus = Y
GROUP BY Equipnum, EquipStatus

and got stuck..dint know how to get the range of values...

Thanks
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2005-06-16 : 14:55:38
One row of sample data isn't enough for us to help out. But, I think you are wanting a streaks and runs solution:
http://www.sqlteam.com/item.asp?ItemID=12654

Tara
Go to Top of Page

dupati1
Posting Yak Master

123 Posts

Posted - 2005-06-16 : 15:18:42
I have tried something like this...

DECLARE @EquipNum Float(8,0)
DECLARE @Range Int

SET @EquipNum = 2346732049
SET @Range = 3

SELECT
Equipnum,
EquipStatus
FROM
EquipTable
WHERE
Equipnum BETWEEN @EquipNum AND @EquipNum + @Range
AND EquipStatus = 'Y'
GROUP BY
Equipnum,
EquipStatus
HAVING
(
SELECT
COUNT(Equipnum)
FROM
tblEquipment
WHERE
Equipnum BETWEEN @EquipNum AND @EquipNum + @Range
AND EquipStatus = 'A'
) = @Range

but it does not give me the results i want...i am trying to get blocks of data...

Thanks

Go to Top of Page
   

- Advertisement -