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 2005 Forums
 Transact-SQL (2005)
 Update query - subsections

Author  Topic 

sadpanda
Starting Member

4 Posts

Posted - 2010-10-25 : 11:08:04
Hello,
I'm working on an a update query that has me stumped. The main table involved will need to have records removed from the table. However, if the records are deleted, orphans will appear in another table.

I've been given another table that shows which records will be deleted. If a record is to be deleted, it will have it's ID "wrapped" into a record that will continue to exist. Here's an example below.

I have placed a new column in the table called InstallationID. So, for subsection 04000, I would have a value of 60 that would need to appear for records 77,80 and 95. For subsection 05000, I would need a value of 96 to show up for the InstallationID for 97, 98 and 99.

ID GroupID InstallationID Action Description
60 04000 Null Existing Facility One
77 04000 Null Delete Null
80 04000 Null Delete Null
95 04000 Null Delete Null
96 05000 Null Existing Facility Two
97 05000 Null Delete Null
98 05000 Null Delete Null
99 05000 Null Delete Null

My table has over 10,000 records so I was wondering how could I structure a query to work with each subsection to update the InstallationID?

Thanks for the help.

yosiasz
Master Smack Fu Yak Hacker

1635 Posts

Posted - 2010-10-25 : 11:14:43
1. so you have an update query that deletes records? a bit confusing, why does an update query need to delete records, smells like bad design
2. what the heck does InstallationID do for you in the update query
3. what is this table where you added InstallationID , is it an audit table?

If you don't have the passion to help people, you have no passion
Go to Top of Page

sadpanda
Starting Member

4 Posts

Posted - 2010-10-25 : 11:23:28
1. The update query won't be deleting any records. It will be updating the InstallationID for a section. The Installation ID that I want for a section would be tagged as 'Existing'.

2. InstallationID is a more descriptive column I've created for where I want to store the ID value. So, I'm trying to get a record that's tagged as "Existing" to be the master record that would update the InstallationID column for records that are tagged "Deleted".

3. Refer to 2.

Again, my initial query is only concerned with upating records. No deletions will be occurring...
Go to Top of Page

sadpanda
Starting Member

4 Posts

Posted - 2010-10-25 : 11:34:14
I think this might help as to what I'm trying to do.

Original table data.

ID GroupID InstallationID Action Description
60 04000 Null Existing Facility One
77 04000 Null Delete Null
80 04000 Null Delete Null
95 04000 Null Delete Null
96 05000 Null Existing Facility Two
97 05000 Null Delete Null
98 05000 Null Delete Null
99 05000 Null Delete Null


Final result of what I'd like to see the data updated to.

ID GroupID InstallationID Action Description
60 04000 Null Existing Facility One
77 04000 60 Delete Null
80 04000 60 Delete Null
95 04000 60 Delete Null
96 05000 Null Existing Facility Two
97 05000 96 Delete Null
98 05000 96 Delete Null
99 05000 96 Delete Null
Go to Top of Page

Ifor
Aged Yak Warrior

700 Posts

Posted - 2010-10-25 : 12:15:19
[code]
-- *** Test Data ***
CREATE TABLE #t
(
ID int NOT NULL
,GroupID varchar(20) NOT NULL
,InstallationID int NULL
,[Action] varchar(20) NOT NULL
,[Description] varchar(20) NULL
)
INSERT INTO #t
SELECT 60, '04000', Null, 'Existing', 'Facility One'
UNION ALL SELECT 77, '04000', Null, 'Delete', Null
UNION ALL SELECT 80, '04000', Null, 'Delete', Null
UNION ALL SELECT 95, '04000', Null, 'Delete', Null
UNION ALL SELECT 96, '05000', Null, 'Existing', 'Facility Two'
UNION ALL SELECT 97, '05000', Null, 'Delete', Null
UNION ALL SELECT 98, '05000', Null, 'Delete', Null
UNION ALL SELECT 99, '05000', Null, 'Delete', Null
-- *** End Test Data ***

UPDATE D
SET InstallationID = MinGroupID
FROM
(
SELECT ID, GroupID, InstallationID
,MIN(ID) OVER (PARTITION BY GroupID) AS MinGroupID
FROM #t
) D
WHERE ID <> MinGroupID

SELECT * FROM #t
[/code]
Go to Top of Page

sadpanda
Starting Member

4 Posts

Posted - 2010-10-25 : 13:12:19
Thanks for the response back Ifor. I see that you are going for the minimium id for a group which is a great idea.

The solution that ended up working for me was to create a new table and place the records marked as "Existing" in it. From there, I could join back to the table using the GroupID column. After joining my new table with the old table, I could then update the InstallationID column with the appropriate value.

I guess the lesson learned here is to break things out into multiple tables to make things easier.

OriginalTable:
ID GroupID InstallationID Action Description
60 04000 Null Existing Facility One
77 04000 Null Delete Null
80 04000 Null Delete Null
95 04000 Null Delete Null
96 05000 Null Existing Facility Two
97 05000 Null Delete Null
98 05000 Null Delete Null
99 05000 Null Delete Null

NewTable:
ID GroupID InstallationID Action Description
60 04000 Null Existing Facility One
96 05000 Null Existing Facility Two

Update query:
UPDATE OriginalTable
SET InstallationID = NewTable.ID
FROM NewTable RIGHT OUTER JOIN
OriginalTable ON NewTable.GroupID = OriginalTable.GroupID
WHERE (OriginalTable.Action = N'Delete') AND (NewTable.Action = 'Existing')
Go to Top of Page

jcelko
Esteemed SQL Purist

547 Posts

Posted - 2010-10-26 : 13:11:42
quote:
The solution that ended up working for me was to create a new table and place the records [sic: rows are not records] marked as "Existing" in it.


Create a VIEW instead. It will always be correct. You are still thinking in terms of redundant PHYSICAL data storage (i.e. records and working files) and not the more abstract SQL model.

--CELKO--
Books in Celko Series for Morgan-Kaufmann Publishing
Analytics and OLAP in SQL
Data and Databases: Concepts in Practice
Data, Measurements and Standards in SQL
SQL for Smarties
SQL Programming Style
SQL Puzzles and Answers
Thinking in Sets
Trees and Hierarchies in SQL
Go to Top of Page
   

- Advertisement -