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 2008 Forums
 Transact-SQL (2008)
 Flag seperate Rows as being related based on flag

Author  Topic 

mtcoder
Starting Member

19 Posts

Posted - 2010-09-29 : 14:27:47
I have a table with raw data similar to.

ID | Permit# | LocationID | WorkType
1 349 1a Add room
2 350 2c replace windows
3 351 1a install footers
4 352 456c new construction
5 353 456c electric

What I need to do is take that data and pull it into another table but mark the records which have the same locationID as being tied. An example output would be

ID | Permit# | LocationID | WorkType | RelatedID
1 349 1a add room 1
2 350 2c replace... <null>
3 351 1a install... 1
4 352 456c new con... 2
5 353 456c electric 2

The reason is I send a work order to the locationID email. Instead of sending two emails to location 1a I would like to send 1 email with both permit numbers. So I need a way to know which records share a locationID. Now I know I don't need the relatedID field, but locationID is actually a complex string field which parses much slower then using an integer to show related fields. So its a "performance" gain to add relatedID column. I just am having a bit of a mental block on how to run through the table and mark the related records.

Any help is welcomed thank you

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2010-09-29 : 19:17:34
There might be a better way depending on what you really need as output. But, maybe you can make use of the Dense_Rank() function:
declare @T table(ID int, Permit# INT, LocationID varchar(20), WorkType varchar(20))
insert @T
values
(1, 349, '1a', 'Add room'),
(2, 350, '2c', 'replace windows'),
(3, 351, '1a', 'install footers'),
(4, 352, '456c', 'new construction'),
(5, 353, '456c', 'electric')

select
*,
dense_rank() over (order by LocationID) as RowRank
from @T
Go to Top of Page

lazycoder
Starting Member

12 Posts

Posted - 2010-09-30 : 06:35:12
You could also create a trigger to fill RelatedIDs automatically:
ALTER TRIGGER Work_Trigger1
ON dbo.[Work]
FOR INSERT, UPDATE
AS
IF UPDATE (LocationID)
begin
--clear RelatedIDs of rows which changed
UPDATE w
SET w.RelatedID = null
FROM [Work] w
WHERE w.ID in (SELECT i.ID FROM inserted i
INNER JOIN deleted d
ON i.ID = d.ID AND i.LocationID <> d.LocationID)

--set realted id's
UPDATE w
SET w.RelatedID = (SELECT top 1 isnull(w2.RelatedID, w2.ID)
FROM [Work] w2
WHERE w2.LocationID = w.LocationID
ORDER BY w2.RelatedID desc, w.ID)
FROM [Work] w
WHERE w.RelatedID is null
AND w.ID IN (SELECT ID FROM inserted)
end


-----------------
http://it.expertmonster.com/
Go to Top of Page

TimSman
Posting Yak Master

127 Posts

Posted - 2010-10-01 : 16:14:42
When does the email go out vs. when does the data get entered? In other words, if I enter a work order for a room at 0800, and another at 1500, when did the email for one (or both) get sent?

Couldn't you just group by the LoactionID?
Go to Top of Page

mtcoder
Starting Member

19 Posts

Posted - 2010-10-01 : 16:32:07
quote:
Originally posted by TimSman

When does the email go out vs. when does the data get entered? In other words, if I enter a work order for a room at 0800, and another at 1500, when did the email for one (or both) get sent?

Couldn't you just group by the LoactionID?



We get in the orders real time, and every night we process them and send the emails out. Grouping by LocationID wouldn't let me grab pull the individual record details out in full, unless I did some type of union on the grouped records. The above solutions seem to work, I just need to do some more bulk testing to validate its working in full.
Go to Top of Page
   

- Advertisement -