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
 SQL Server Development (2000)
 Stored Procedure Loops

Author  Topic 

oahu9872
Posting Yak Master

112 Posts

Posted - 2005-09-01 : 11:40:39
I'm trying to set up a sql statement that will edit my data in the following way:

Original >>

Zone City Event
1 Chicago Parade
1 Boston Concert
1 Detroit Carnival
2 Cincinnati Parade
2 Louisville Concert

What I would like it to do >>

Zone City Event
1 Chicago Parade
null Boston Concert
null Detroit Carnival
2 Cincinnati Parade
null Louisville Concert

So what I need is for it to check to see if the previous record's zone = the current record's zone, and if so set it to null, and if it is a new zone, then show it.

Thanks

X002548
Not Just a Number

15586 Posts

Posted - 2005-09-01 : 12:09:02
The physical order of data in a database is meaningless.

You have to Order by Something. Plus what you're trying to do, doesn't make much sense.

Here's soemthing to chew on though


USE Northwind
GO

SET NOCOUNT ON
CREATE TABLE myTable99(Zone int, City varchar(25), Event varchar(25))
GO

INSERT INTO myTable99(Zone, City, Event)
SELECT 1, 'Chicago', 'Parade' UNION ALL
SELECT 1, 'Boston', 'Concert' UNION ALL
SELECT 1, 'Detroit', 'Carnival' UNION ALL
SELECT 2, 'Cincinnati', 'Parade' UNION ALL
SELECT 2, 'Louisville', 'Concert'
GO

SELECT Zone, City, Event FROM (
SELECT Zone, City, Event, Zone AS OldZone
FROM myTable99 o
WHERE EXISTS ( SELECT *
FROM myTable99 i
GROUP BY Zone
HAVING o.City = MIN(i.City))
UNION ALL
SELECT Null AS Zone, City, Event, Zone AS OldZone
FROM myTable99 o
WHERE EXISTS ( SELECT *
FROM myTable99 i
GROUP BY Zone
HAVING o.City > MIN(i.City))
) AS XXX
ORDER BY OldZone, City
GO

SET NOCOUNT OFF
DROP TABLE myTable99
GO





Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx
Go to Top of Page

oahu9872
Posting Yak Master

112 Posts

Posted - 2005-09-01 : 12:28:11
that works great, thanks alot.
Go to Top of Page
   

- Advertisement -